在Android中保存图片到相册

6

我正在使用以下代码保存图片:

URL url = null;
            try {
                url = new URL("image");
            } catch (MalformedURLException e1) {
                e1.printStackTrace();
            }
            Bitmap bmp = null;
            try {
                bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            OutputStream output;
            File filepath = Environment.getExternalStorageDirectory();
            File dir = new File(filepath.getAbsolutePath() + "/folder name/");
            dir.mkdirs();
            File file = new File(dir, image + ".png");
            Toast.makeText(HomeActivity.this, "Image Saved to SD Card", Toast.LENGTH_SHORT).show();
            try {

                output = new FileOutputStream(file);
                bmp.compress(Bitmap.CompressFormat.PNG, 100, output);
                output.flush();
                output.close();

            }

            catch (Exception e) {
                e.printStackTrace();
            }

问题出现在这段代码运行于棒棒糖设备时,图片无法在相册中显示。我必须安装文件管理器来查看这些图片。
使用以下代码:
    MediaStore.Images.Media.insertImage(getContentResolver(), bmp, "image";

照片保存在相机文件夹中。

我想在所有安卓设备中以特定的文件夹名称在相册中展示照片。

请帮忙。


在图库中添加图片后,您需要刷新图库。刷新图库的方法如下: https://dev59.com/YG855IYBdhLWcg3wynmC - Darsh Patel
@VipulPatel:sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()))); file是文件路径吗? - android
你需要参考这个链接来解决你的问题: https://dev59.com/unA75IYBdhLWcg3wbofM - Darsh Patel
5个回答

16
public void saveImageToExternal(String imgName, Bitmap bm) throws IOException {
    //Create Path to save Image
    File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES+appFolder); //Creates app specific folder
    path.mkdirs();
    File imageFile = new File(path, imgName+".png"); // Imagename.png
    FileOutputStream out = new FileOutputStream(imageFile);
    try{
        bm.compress(Bitmap.CompressFormat.PNG, 100, out); // Compress Image
        out.flush();
        out.close();

        // Tell the media scanner about the new file so that it is
        // immediately available to the user.
        MediaScannerConnection.scanFile(context,new String[] { imageFile.getAbsolutePath() }, null,new MediaScannerConnection.OnScanCompletedListener() {
            public void onScanCompleted(String path, Uri uri) {
                 Log.i("ExternalStorage", "Scanned " + path + ":");
                 Log.i("ExternalStorage", "-> uri=" + uri);
            }
        });
    } catch(Exception e) {
        throw new IOException();
    }
}

对我有用。感谢你花时间。


经过将近一周的努力,终于找到了解决方案。谢谢 :) - Naila
此外,在Marshmallow(api 23)中,我们需要授予Manifest.permission.WRITE_EXTERNAL_STORAGE权限。 - Täg

1
这对我有用。
添加MediaScannerConnection来扫描文件。指定文件网址媒体类型
MediaScannerConnection.scanFile(context,new String[] { image.getAbsolutePath() }, 
new String[] {"images/*"}, new MediaScannerConnection.OnScanCompletedListener() {
    public void onScanCompleted(String path, Uri uri) {
        Log.i("ScanCompleted", "Scanned " + path + ":");
    }
});

1

0
如果您想将图像保存在目录中,则此代码对我有效!
saveImage(data);

            private void saveImage(Bitmap data) {
                File createFolder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"test");
                if(!createFolder.exists())
                createFolder.mkdir();
                File saveImage = new File(createFolder,"downloadimage.jpg");
                try {
                    OutputStream outputStream = new FileOutputStream(saveImage);
                    data.compress(Bitmap.CompressFormat.JPEG,100,outputStream);
                    outputStream.flush();
                    outputStream.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

0

只需更改这些行,它就可以工作 -

File filepath = Environment.getExternalStorageDirectory().toString();
        File dir = new File(filepath + "/folder name/");
        dir.mkdirs();
        File file = new File(dir, image + ".png");

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接