【问题标题】:save image in gallery android将图像保存在画廊 android
【发布时间】:2015-07-04 05:04:51
【问题描述】:

我正在使用此代码保存图像:

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";

图像保存在相机文件夹中。

我想在所有 android 设备中显示具有特定文件夹名称的图库中的图像。

请帮忙。

【问题讨论】:

标签: android gallery


【解决方案1】:
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();
    }
}

为我工作。 感谢您的宝贵时间

【讨论】:

  • 经过将近一周的时间,终于得到了解决方案。谢谢:)
  • 另外,对于 Marshmallow (api 23),我们需要获得Manifest.permission.WRITE_EXTERNAL_STORAGE 权限。
【解决方案2】:

只是您的代码缺少 MediaScannerConnection 类。此类扫描从您的应用程序创建的库中的新文件和目录。请参阅演示这一点的完整演示示例。 http://whats-online.info/science-and-tutorials/135/how-to-save-an-image-to-gallery-in-android-programmatically/

【讨论】:

    【解决方案3】:

    这对我有用。

    添加 MediaScannerConnection 以扫描文件。指定 文件 urlmimeType

    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 + ":");
        }
    });
    

    【讨论】:

      【解决方案4】:

      只需更改这些行,它就会起作用 -

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

      【讨论】:

        【解决方案5】:

        如果您想将图像保存在目录中,那么此代码对我有用!

        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();
                        }
                    }
        

        【讨论】:

          猜你喜欢
          • 2012-01-23
          • 1970-01-01
          • 2014-01-18
          • 1970-01-01
          • 1970-01-01
          • 2015-08-03
          • 1970-01-01
          • 1970-01-01
          • 2016-08-06
          相关资源
          最近更新 更多