无法从Android中的ImageView将图像保存到图库

wb1gzix0  于 2022-12-28  发布在  Android
关注(0)|答案(2)|浏览(189)

在保存图像到画廊从webview面临的问题.....存储权限被授予,但它不工作....我已经尝试了所有我能做的...请帮助我
下面是代码

private void saveImageToExternalStorage(Bitmap finalBitmap) {
    String root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
    File myDir = new File(root + "/ahmed");
    myDir.mkdirs();
    Random generator = new Random();
    int n = 10000;
    n = generator.nextInt(n);
    String fname = "Image-" + n + ".jpg";
    File file = new File(myDir, fname);
    if (file.exists())
        file.delete();
    try
    {
        FileOutputStream out = new FileOutputStream(file);
        finalBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
        out.flush();
        out.close();
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    // Tell the media scanner about the new file so that it is

    MediaScannerConnection.scanFile(getActivity(), new String[]{file.toString()}, null,
            new MediaScannerConnection.OnScanCompletedListener() {
                public void onScanCompleted(String path, Uri uri) {
                    Log.i("ExternalStorage", "Scanned " + path + ":");
                    Log.i("ExternalStorage", "-> uri=" + uri);
                }
            });

}

这是onClick()上的按钮

cvDownload.setOnClickListener(new View.OnClickListener() { 
    @Override public void onClick(View v) { 
        anImage = ((BitmapDrawable) ivPhoto.getDrawable()).getBitmap(); 
        saveImageToExternalStorage(anImage); 
        Toast.makeText(getContext(), "Download successfuly"+anImage.toString(), 
        Toast.LENGTH_SHORT).show(); 
    } 
});
pgx2nnw8

pgx2nnw81#

it is work for me:

    public void onCreate(){

    File path=saveBitmap(bitmap);
    MediaScannerConnection.scanFile(this, new String[]{path.getPath()}, new String[]{"png", "jpg"}, new MediaScannerConnection.OnScanCompletedListener() {
            /*
             *   (non-Javadoc)
             * @see android.media.MediaScannerConnection.OnScanCompletedListener#onScanCompleted(java.lang.String, android.net.Uri)
             */
            public void onScanCompleted(String path, Uri uri) {
                Log.i("ExternalStorage", "Scanned " + path + ":");
                Log.i("ExternalStorage", "-> uri=" + uri);

                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_VIEW);

                intent.setDataAndType(uri, "image/*");
                startActivity(intent);
                // sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(path)));
                startActivity(intent); /** replace with your own uri */
                // finish();
                //  finishAffinity();
            }
        });

    }
    public static File saveBitmap(Bitmap bitmap) {
        String fileName = "";

        FileOutputStream outStream = null;
        File sdCard = Environment.getExternalStorageDirectory();
        File dir = new File(sdCard.getAbsolutePath() + "/MyImages");
        dir.mkdirs();

        fileName = String.format("%d.jpeg", System.currentTimeMillis());

        File outFile = new File(dir, fileName);
        try {
            outStream = new FileOutputStream(outFile);

             bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);

            outStream.flush();

            outStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return outFile;
    }
4c8rllxm

4c8rllxm2#

按照此代码从图像视图下载图像

{ if(上下文兼容.检查自我权限(此,清单.权限.写入_外部_存储)==包管理器.权限_已授予){ try {瓦尔bitmap = imageView.getDrawable().toBitmap()

var outStream: FileOutputStream? = null
                val sdCard: File = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)
                val dir = File(sdCard.getAbsolutePath().toString() + "/sastana")
                dir.mkdirs()
                val fileName = String.format("%d.jpg", System.currentTimeMillis())
                val outFile = File(dir, fileName)
                outStream = FileOutputStream(outFile)
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream)
                outStream.flush()
                outStream.close()
                Toast.makeText(this, "Saved Successfully", Toast.LENGTH_SHORT).show()

            }catch (e : Exception){
                Log.e("qrcode",""+e)
            }

        } else {
            Log.e("qrcode","permission not granted")
        }}

相关问题