在相册中创建类似于WhatsApp图片、WhatsApp视频的文件夹。

5

我的要求是在“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");

但是没有运气,朋友们请帮帮我,提前感谢。


这个链接可能会帮助您像WhatsApp一样在相册中保存图像。 - Ali Momeni
通过使用上面的链接,我得到了相同的结果,它没有显示在“画廊”下的目录,它只是创建了一个可以通过文件管理器找到的目录,请分享其他方法,我会尝试。谢谢。 - Mobiram
5个回答

0

试试这个。

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

它正在创建DCIM目录,但在相册中不可见。我可以在文件管理器中通过搜索目录名称看到此目录,但我想像Skype、WhatsApp图像一样在相册中显示我的目录。 - Mobiram
我认为你需要刷新数据,才能使其显示出来。 - Eyosiyas

0

您可以使用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显示为相册。 已在安卓10及以上的三星S10上进行了检查。


0
这是使用java.nio库创建目录的方法。
Java代码
File targetFile = new File(Environment.getExternalStorageDirectory(), "subDir");
Path sourceFile =  Paths.get(targetFile.getAbsolutePath());
Files.createDirectory(sourceFile);

0
一些安卓手机在创建应用程序文件夹时会创建.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()
            }

        }

0

也可以尝试这个解决方案:

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()"创建了目录,但在图库/相册中并未显示出来。 - Mobiram

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