简体   繁体   English

如何从意图打开默认文件应用程序中的 DCIM/Camera 文件夹

[英]How to open DCIM/Camera folder in default Files app from intent

I am making a camera app which takes both front camera and back camera images/video and I do not want individual thumbnails on the Camera preview for each file.我正在制作一个相机应用程序,它同时拍摄前置摄像头和后置摄像头图像/视频,我不希望每个文件的相机预览上都有单独的缩略图。

I want to open "/storage/emulated/0/DCIM/Camera" folder using the Files app and further open the photo/video which is not possible with ACTION_GET_CONTENT as it selects the image and exits the Files app as tried here -我想使用“文件”应用程序打开“/storage/emulated/0/DCIM/Camera”文件夹并进一步打开 ACTION_GET_CONTENT 无法使用的照片/视频,因为它选择图像并退出文件应用程序,如此处尝试 -

    val intent = Intent(Intent.ACTION_GET_CONTENT)
    val uri: Uri = Uri.parse(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).path.toString() + "/Camera")
    intent.setDataAndType(uri, "*/*")
    startActivity(Intent.createChooser(intent, "Open folder"))

I tried ACTION_VIEW too, but it is not specific to one folder and opens the gallery showing all media as tried here -我也尝试了 ACTION_VIEW,但它并不特定于一个文件夹,并打开显示所有媒体的画廊,如此处尝试 -

    val intent = Intent()
    intent.action = Intent.ACTION_VIEW
    intent.type = "image/*"
    intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
    startActivity(intent)

"image/*" shows images and videos too in the gallery for me which is good. “image/*” 对我来说也在图库中显示图像和视频,这很好。 When "*/*" is used we can use the Files app too but it opens the downloads folder.当使用“*/*”时,我们也可以使用“文件”应用程序,但它会打开下载文件夹。 One solution I found works only with ES Explorer as tried here -我发现的一种解决方案仅适用于此处尝试的 ES Explorer -

    val uri: Uri = Uri.parse(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).path.toString() + "/Camera")
    val intent = Intent(Intent.ACTION_VIEW)
    intent.setDataAndType(uri, "resource/folder")
    startActivity(intent)

This is due to "resource/folder" not supported leading to a crash.这是由于不支持“资源/文件夹”导致崩溃。 Changing "resource/folder" to "*/*" makes Files app open the downloads folder and Photos app to hang.将“资源/文件夹”更改为“*/*”会使“文件”应用程序打开下载文件夹和“照片”应用程序挂起。

It seems gallery can do it via buckets , but it too is not universal.似乎画廊可以通过buckets做到这一点,但它也不是通用的。

I am not asking for much, just to display my Camera folder from where I can open and view any photo/video.我的要求不高,只是为了显示我的相机文件夹,我可以从中打开和查看任何照片/视频。

It was not possible if the user had not once selected a file using ACTION_GET_CONTENT was the opinion.如果用户没有使用 ACTION_GET_CONTENT 选择一个文件,这是不可能的。

But now... try this code:但现在......试试这个代码:

String scheme = "content://com.android.externalstorage.documents/document/primary%3APictures";
//  String scheme = "content://com.android.externalstorage.documents/document/primary%3ADCIM%2FCamera";

Uri uri = Uri.parse(scheme);

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);

intent.setType("*/*");
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, uri); // Added in API level 26 

startActivityForResult(intent, 12345);

Toast.makeText(context, "Picker opened in:\n\n" + uri.toString(), Toast.LENGTH_LONG).show();

It works here and i'm pretty amazed.它在这里工作,我很惊讶。

To open the Files app starting with a certain folder you can use below code but only for Android 11 devices sadly.要打开从某个文件夹开始的文件应用程序,您可以使用以下代码,但遗憾的是仅适用于 Android 11 设备。

//String scheme = "content://com.android.externalstorage.documents/document/primary%3APictures";
//String scheme = "content://com.android.externalstorage.documents/document/primary%3ADownload";
//String scheme = "content://com.android.externalstorage.documents/document/10F9-2E19%3ADownload";
String scheme = "content://com.android.externalstorage.documents/document/primary%3ADCIM%2FCamera";

Uri uri = Uri.parse(scheme);

Intent intent = new Intent(Intent.ACTION_VIEW);

intent.setDataAndType(uri, "vnd.android.document/root"); 

startActivity(intent );

Toast.makeText(context, "Files app opening in:\n\n" + uri.toString(), Toast.LENGTH_LONG).show();

                

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将默认保存文件夹DCIM \\ Camera更改为任何文件夹名称并使用MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA - How to change default save folder DCIM\Camera to any folder name and used MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA 如何在/ DCIM / Camera上获取图像文件数组? - How to get array of image files on /DCIM/Camera? 如何从app小部件打开相机和图库的Intent选择器 - How to open the Intent chooser for camera and gallery from app widget 如何从Android的SD卡中的DCIM / Camera文件夹中检索最后保存的图像 - How to retrieve the last saved image from DCIM/Camera folder in sd card in Android 如何使用 Android 应用程序中的 Intent 在专业模式下打开默认相机应用程序? - How to open the default camera app in professional mode using an intent in your android app? 从相机捕获图像,它存储在sdcard / dcim文件夹中 - Capturing image from Camera, it stores at sdcard/dcim folder 无法通过默认相机应用程序的意图拍摄照片 - Couldn't take a pic via an intent from default camera app 相机拍摄的两张图像,无法从DCIM / Camera文件夹中删除一张 - Two images captured by camera and not able to delete one from DCIM/Camera folder android - 如何通过采样缩略图从 DCIM/CAMERA 中提取图像 - android - how to pull images from DCIM/CAMERA by sampling thumbnails 意图在方形模式下打开默认摄像头 - Intent to open default camera in square mode
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM