【问题标题】:How can I open images that save in internal storage (cache folder in Android/data/%package%/cache) in android gallery如何在 android 库中打开保存在内部存储中的图像(Android/data/%package%/cache 中的缓存文件夹)
【发布时间】:2015-11-06 03:55:26
【问题描述】:

图像下载后,我将其保存在内部存储中

Android/data/%packagename%/cache 目录。下面是代码。

private class SaveImageToStorage extends AsyncTask<Void,Void,Void>
{
    String picName;
    Bitmap bitmap;
    public SaveImageToStorage(String name, Bitmap bitmap)
    {
        this.picName = name;
        this.bitmap = bitmap;
    }

    @Override
    protected Void doInBackground(Void... params) {
        if(bitmap != null && !picName.isEmpty()) {
            File file = new File(feedImagesCacheDir.getPath(), extractImageNameFromUrl(picName));
            try {
                FileOutputStream fos = new FileOutputStream(file);
                bitmap.compress(Bitmap.CompressFormat.WEBP, 80, fos);
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}

在这一切之后,我只想在 android 库中打开图像。

我尝试下面的代码,但它不起作用。它显示 Toast“找不到图像”。

所以我尝试删除“file://”,然后它打开画廊,除了损坏的图像图标之外什么都没有。

private void openImageInGallery(){
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.parse("file://"+getFeedImagesCacheDir().getPath() + "/" + imgName+".webp"), "image/*");
    startActivity(intent);
}

我想也许我把它保存在缓存文件夹中,所以它变成了私有的,只能访问我的应用程序。

我该如何解决这个问题?

谢谢。对不起,我的英语不好。

【问题讨论】:

    标签: android caching android-image android-gallery android-file


    【解决方案1】:

    缓存图像不能直接查看,因为它像加密格式一样保存,所以首先你下载该图像并保存到 SD 卡中。使用下面的代码。

    String root = Environment.getExternalStorageDirectory().toString();
    String wallpaperFilePath=Android/data/%packagename%/cache/imagename;
    File cacheDir = GlobalClass.instance().getCacheFolder(this);
    File cacheFile = new File(cacheDir, wallpaperFilePath);
    InputStream fileInputStream = new FileInputStream(cacheFile);
    BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
    bitmapOptions.inSampleSize = scale;
    bitmapOptions.inJustDecodeBounds = false;
    Bitmap wallpaperBitmap = BitmapFactory.decodeStream(fileInputStream, null, bitmapOptions);
    
    
    File myDir = new File(root + "/saved_images");    
    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);
           wallpaperBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
           out.flush();
           out.close();
    
    } catch (Exception e) {
           e.printStackTrace();
    }
    

    并将其添加到清单中

     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    

    通过使用此行,您可以在图库视图中查看已保存的图像。

    sendBroadcast(new Intent(
    Intent.ACTION_MEDIA_MOUNTED,
            Uri.parse("file://" + Environment.getExternalStorageDirectory())));
    

    【讨论】:

      【解决方案2】:

      您可以将图像下载到 DCIM(您在 sdCard 中的图库文件夹)。 然后使用下面打开图库:

      public static final int RESULT_GALLERY = 0;
      Intent galleryIntent = new Intent(
                          Intent.ACTION_PICK,
                      android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
      startActivityForResult(galleryIntent , RESULT_GALLERY );
      

      如果您想获取结果 URI 或做其他任何事情,请使用:

      @Override
      public void onActivityResult(int requestCode, int resultCode, Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
      
          switch (requestCode) {
          case QuestionEntryView.RESULT_GALLERY :
              if (null != data) {
                  imageUri = data.getData();
                  //Do whatever that you desire here. or leave this blank
      
              }
              break;
          default:
              break;
          }
      }
      

      希望对您有所帮助!

      【讨论】:

        猜你喜欢
        • 2018-10-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-21
        • 2021-08-12
        相关资源
        最近更新 更多