【问题标题】:Create a folder like WhatsApp Images, WhatsApp Videos in Albums or Gallery在相册或图库中创建一个文件夹,如 WhatsApp 图像、WhatsApp 视频
【发布时间】:2021-01-26 19:25:30
【问题描述】:

我的要求是在 Gallery/ Albums 下显示目录, 以以下方式创建目录并不能完全满足我的要求...

 File rootPath = new File(Environment.getExternalStorageDirectory(), "directoryName"); 
 if(!rootPath.exists()) {
                rootPath.mkdirs();
            }

            final File localFile = new File(rootPath,fileName);

通过使用此代码,我可以通过使用带有路径的“文件管理器”来查看文件夹... “deviceStorage/directoryName”,但该文件夹在图库或相册下不可见

为了创建目录,我也尝试了以下方法...

 1)File directory = new File(this.getFilesDir()+File.separator+"directoryName");

 2)File directory = new File (Environment.getExternalFilesDir(null) + "/directoryName/");

3)File directory = new File(Environment.
  getExternalStoragePublicDirectory(   
 (Environment.DIRECTORY_PICTURES).toString() + "/directoryName");

但没有运气,请帮助我的朋友 提前致谢。

【问题讨论】:

  • 这个link 可能会帮助您在whatsapp 等图库中保存图片
  • 通过使用上面的链接也得到了相同的结果,它没有显示Gallery下的目录,它只是创建了一个可以使用文件管理器找到的目录,任何其他方式请分享我会试试。谢谢

标签: java android image directory gallery


【解决方案1】:

也检查这个解决方案:

String path = Environment.getExternalStorageDirectory().toString();
File dir = new File(path, "/appname/media/app images/");
if (!dir.isDirectory()) {
        dir.mkdirs();
}

File file = new File(dir, filename + ".jpg");
String imagePath =  file.getAbsolutePath();
    //scan the image so show up in album
MediaScannerConnection.scanFile(this,
        new String[] { imagePath }, null,
        new MediaScannerConnection.OnScanCompletedListener() {
        public void onScanCompleted(String path, Uri uri) {
        if(Config.LOG_DEBUG_ENABLED) {
            Log.d(Config.LOGTAG, "scanned : " + path);
        }
        }
});

【讨论】:

  • 是的,兄弟我也检查了这个,但没有运气。 “Environment.getExternalStorageDirectory()”创建目录,但它没有显示在 Gallery/相册下
【解决方案2】:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

java代码

File folder = new File(Environment.getExternalStorageDirectory() + "gallery/albums");
boolean success = true;
if (!folder.exists()) {
    success = folder.mkdirs();
}
if (success) {
    // Do something on success
} else {
    // Do something else on failure 
}

【讨论】:

  • 您的代码创建文件夹,但不在画廊/相册下。
【解决方案3】:

试试这个。

File root = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + File.separator + "albums");
boolean rootCreated = false;
if (!root.exists())
    rootCreated = root.mkdir();

【讨论】:

  • 它正在 DCIM 中创建目录,但在图库中不可见。在按目录名称搜索后,我可以在文件管理器中看到这个目录,但我想在画廊中显示我的目录,如 skype、whatsAppImages
  • 我认为您必须刷新数据才能显示出来。
【解决方案4】:

您可以使用MediaStore API 来保存您的媒体文件:

val values = ContentValues().apply {
    put(MediaStore.Images.Media.DISPLAY_NAME, "filename")
    put(MediaStore.Images.Media.RELATIVE_PATH, "${Environment.DIRECTORY_DCIM}/$directoryName")
    put(MediaStore.Images.Media.MIME_TYPE, mimeType)
}
val collection = MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)
val item = context.contentResolver.insert(collection, values) ?: throw IOException("Insert failed")
resolver.openFileDescriptor(item, "w", null)?.use {
   writeImage(it.fileDescriptor)
}

这会将媒体文件保存到媒体收藏中,并提供给图库。图库应用程序通常将您的directoryName 显示为相册。 已在搭载 Android 10 的三星 S10 及更高版本上进行检查。

【讨论】:

    【解决方案5】:

    这里是 java.nio 库创建目录的方法

    Java 代码

    File targetFile = new File(Environment.getExternalStorageDirectory(), "subDir");
    Path sourceFile =  Paths.get(targetFile.getAbsolutePath());
    Files.createDirectory(sourceFile);
    

    【讨论】:

      【解决方案6】:

      一些android手机在创建的应用文件夹中创建.nomedia文件,以防止媒体在图库应用中显示,因此您可以检查此隐藏文件是否存在,如果存在则将其删除。设置文件夹和文件可读。您可能等待一段时间,系统才会在您的图库应用中反映您创建的文件

              val parentFile = File(Environment.getExternalStorageDirectory(), "MyAppFolder")
              if(!parentFile.exists())
                   parentFile.mkdir()
              parentFile.setReadable(true)
              // .nomedia file prevents media from being shown in gallery app
              var nomedia = File(parentFile, ".nomedia")
              if(nomedia.exists()){
                  nomedia.delete()
              }
              val file = File(parentFile , "myvideo.mp4")
              file.setReadable(true)
              input.use { input ->
                  var output: FileOutputStream? = null
                  try {
                      output = FileOutputStream(file)
                      val buffer = ByteArray(4 * 1024)
                      var read: Int = input.read(buffer)
                      while (read != -1) {
                          output.write(buffer)
                          read = input.read(buffer)
                      }
                      output.flush()
                      // file written to memory
      
                  } catch (ex: Exception) {
                      ex.printStackTrace()
                  } finally {
                      output?.close()
                  }
      
              }
      

      【讨论】:

        猜你喜欢
        • 2018-05-08
        • 2016-07-17
        • 2014-06-29
        • 2014-04-07
        • 1970-01-01
        • 2016-09-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多