【问题标题】:How to create an app image folder to show in Android gallery如何创建应用图像文件夹以在 Android 库中显示
【发布时间】:2023-03-04 20:20:02
【问题描述】:

我有一个应用程序,用户在其中创建图像,然后我想保存它,以便它在默认图库应用程序中可见。

现在我不希望将照片保存在与从相机拍摄的照片相同的文件夹中,我希望将它们保存在专用于应用程序的文件夹中,就像来自 whatsapp 或 facebook 等应用程序的图像一样。

我尝试将它们保存在这两个位置:

File imagePath = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)+ File.separator + appDirectoryName + File.separator);

这里

File imagePath = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+ File.separator + appDirectoryName + File.separator);

如果我通过手机浏览,我会看到我成功保存了图像,但它们没有显示在图库应用中。很明显,我错过了一些东西,但我不知道它是什么。也许在文件或文件夹中添加某种元数据以便画廊识别它们?

【问题讨论】:

    标签: android image gallery


    【解决方案1】:

    嗯,我终于找到了答案。

    事实证明,这正是我所怀疑的。保存的图像需要添加一些元数据才能在图库中看到(至少在我的设备中)。

    这就是我所做的:

    OutputStream fOut = null;
        File file = new File(imagePath,"GE_"+ System.currentTimeMillis() +".jpg");
    
        try {
            fOut = new FileOutputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
        try {
            fOut.flush();
            fOut.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    
        ContentValues values = new ContentValues();
        values.put(Images.Media.TITLE, this.getString(R.string.picture_title));
        values.put(Images.Media.DESCRIPTION, this.getString(R.string.picture_description));
        values.put(Images.Media.DATE_TAKEN, System.currentTimeMillis ());
        values.put(Images.ImageColumns.BUCKET_ID, file.toString().toLowerCase(Locale.US).hashCode());
        values.put(Images.ImageColumns.BUCKET_DISPLAY_NAME, file.getName().toLowerCase(Locale.US));
        values.put("_data", file.getAbsolutePath());
    
        ContentResolver cr = getContentResolver();
        cr.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
    

    【讨论】:

    • 很好的解决方案。谢谢
    • @Nemesis 嘿伙计,我也在我的应用程序中创建类似的流程,除此之外,我需要在图像查看器中打开图像。我通过传递包含点击图像的 uri 的意图来做到这一点。但是它只显示一个图像,有什么方法可以从单击的图像开始浏览指定文件夹中的所有图像。谢谢
    • 它有效,但不幸的是它减小了要保存的位图的大小,但分辨率相同。我认为bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut); 正在压缩图像。
    • 即使在 fOut = new FileOutputStream(file); 之后,outputStream 仍为空。
    • @suv 这不适用于视频吗?您需要将文件格式更改为视频格式,但我认为大多数元数据会非常相似
    【解决方案2】:

    我也遇到了同样的问题。 第二种方式是正确的方式,但是您看不到图库中的图像,因为图库需要刷新。 因此,您可以等待一段时间,直到它自行刷新,或者您可以使用 MediaScanner - 看here

    希望这有帮助!

    【讨论】:

    • 您的链接无效,请更新链接或在此处发布示例。
    【解决方案3】:

    我做了以下工作来让它工作:

    public void galleryAddPic() {
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_MOUNTED);
        String mCurrentPhotoPath = "file:" + image.getAbsolutePath(); // image is the created file image
        File file = new File(mCurrentPhotoPath);
        Uri contentUri = Uri.fromFile(file);
        mediaScanIntent.setData(contentUri);
        sendBroadcast(mediaScanIntent);
    }
    

    【讨论】:

      【解决方案4】:

      试试这个

      private void createDirectoryAndSaveFile(Bitmap imageToSave, String fileName) {
      
          File direct = new File(Environment.getExternalStorageDirectory() + "/DirName");
      
          if (!direct.exists()) {
              File wallpaperDirectory = new File("/sdcard/DirName/");
              wallpaperDirectory.mkdirs();
          }
      
              File file = new File(new File("/sdcard/DirName/"), fileName);
              if (file.exists())
                  file.delete();
              try {
                  FileOutputStream out = new FileOutputStream(file);
                  imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, out);
                  out.flush();
                  out.close();
      
              } catch (Exception e) {
                  e.printStackTrace();
              }
       }
      

      【讨论】:

      • 它可以工作,但不幸的是它减小了要保存的位图的大小,但分辨率相同
      【解决方案5】:

      我试过了,效果很好。

      private Uri imageUri;
      String getimgname, getimgpath;
      intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
      File photo = new File(Environment.getExternalStorageDirectory(),  "IMG"+System.currentTimeMillis()+".jpg");
      intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(photo));
      imageUri = Uri.fromFile(photo);
      getimgname = photo.getName();
      getimgpath = photo.getAbsolutePath();
      

      【讨论】:

        【解决方案6】:

        试试这个

        MediaScannerConnection.scanFile(context,
                        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);
                            }
                        });
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-07-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-11-18
          相关资源
          最近更新 更多