我认为这很容易,但不幸的是,事实并非如此。
我有什么:
我的外部存储设备上有一个名为“myFolder”的文件夹(不是SD卡,因为它是Nexus 4,但这应该不是问题)。该文件夹包含一些*.csv
个文件。
我想要的是什么:
我想编写一个执行以下操作的方法:显示各种应用程序(文件浏览器),我可以从中选择一个(见图片)。单击它后,所选的文件浏览器应该启动并显示“myFolder”的内容。不多也不少。
我的问题:
我该怎么做?我想我接下来的代码非常接近,但无论我做什么 - 而且我确定必须有一些我还没有做到的事情 - 它总是只打开来自外部存储器的主文件夹。
public void openFolder()
{
File file = new File(Environment.getExternalStorageDirectory(),
"myFolder");
Log.d("path", file.toString());
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setDataAndType(Uri.fromFile(file), "*/*");
startActivity(intent);
}
答案 0 :(得分:56)
这应该有效:
Uri selectedUri = Uri.parse(Environment.getExternalStorageDirectory() + "/myFolder/");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(selectedUri, "resource/folder");
if (intent.resolveActivityInfo(getPackageManager(), 0) != null)
{
startActivity(intent);
}
else
{
// if you reach this place, it means there is no any file
// explorer app installed on your device
}
请确保您的设备上安装了任何文件资源管理器应用程序。
编辑:从评论中添加了shantanu的推荐。
<强>库强>: 如果当前的解决方案对您没有帮助,您还可以查看这些文件/目录选择器库https://android-arsenal.com/tag/35。
答案 1 :(得分:36)
我终于开始工作了。这样,选择器(Google Drive,Dropbox,Root Explorer和Solid Explorer)只显示几个应用程序。它与两个浏览器一起正常工作,但不适用于Google Drive和Dropbox(我猜因为它们无法访问外部存储)。另一种MIME类型如"*/*"
也是可能的。
public void openFolder(){
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()
+ File.separator + "myFolder" + File.separator);
intent.setDataAndType(uri, "text/csv");
startActivity(Intent.createChooser(intent, "Open folder"));
}
答案 2 :(得分:3)
Intent chooser = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getDownloadCacheDirectory().getPath().toString());
chooser.addCategory(Intent.CATEGORY_OPENABLE);
chooser.setDataAndType(uri, "*/*");
// startActivity(chooser);
try {
startActivityForResult(chooser, SELECT_FILE);
}
catch (android.content.ActivityNotFoundException ex)
{
Toast.makeText(this, "Please install a File Manager.",
Toast.LENGTH_SHORT).show();
}
在上面的代码中,如果setDataAndType为“* / *”,则打开内置文件浏览器以选择任何文件,如果我设置了“text / plain”,则打开Dropbox。我安装了Dropbox,Google云端硬盘。如果我卸载Dropbox,只有“* / *”可以打开文件浏览器。这是Android 4.4.2。我可以通过getContentResolver()。openInputStream(data.getData())从Dropbox和Google Drive下载内容。
答案 3 :(得分:2)
线程很旧,但是我在应用程序中需要这种功能,因此我找到了一种方法,因此我决定将其发布,如果它可以帮助我的情况的任何人。
由于我们的设备仅由Samsung Galaxy Tab 2组成,因此我只需要找到文件浏览器的程序包名称,并提供正确的路径,然后我就可以在需要的地方成功打开文件浏览器了。我希望可以使用Intent.CATEGORY_APP_FILES
,但仅在API 29中可用。
Intent intent = context.getPackageManager().getLaunchIntentForPackage("com.sec.android.app.myfiles");
Uri uri = Uri.parse(rootPath);
if (intent != null) {
intent.setData(uri);
startActivity(intent);
}
正如我所说,这对我来说很容易,因为我们的客户使用相同的设备,但这可能会帮助其他人找到适合自己情况的解决方法。
答案 4 :(得分:1)
此代码适用于OI文件管理器:
File root = new File(Environment.getExternalStorageDirectory().getPath()
+ "/myFolder/");
Uri uri = Uri.fromFile(root);
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setData(uri);
startActivityForResult(intent, 1);
您可以在此处获取OI文件管理器:http://www.openintents.org/en/filemanager
答案 5 :(得分:1)
答案 6 :(得分:1)
这是我的答案
private fun openFolder() {
val location = "/storage/emulated/0/Download/";
val intent = Intent(Intent.ACTION_VIEW)
val myDir: Uri = FileProvider.getUriForFile(context, context.applicationContext.packageName + ".provider", File(location))
intent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
intent.setDataAndType(myDir, DocumentsContract.Document.MIME_TYPE_DIR)
else intent.setDataAndType(myDir, "*/*")
if (intent.resolveActivityInfo(context.packageManager, 0) != null)
{
context.startActivity(intent)
}
else
{
// if you reach this place, it means there is no any file
// explorer app installed on your device
CustomToast.toastIt(context,context.getString(R.string.there_is_no_file_explorer_app_present_text))
}
}
这就是为什么我使用FileProvider-android.os.FileUriExposedException: file:///storage/emulated/0/test.txt exposed beyond app through Intent.getData()
我在此设备上进行了测试
设备:三星SM-G950F(dreamltexx),操作系统API级别:28
答案 7 :(得分:0)
今天,您应该使用其内容:从Storage Access Framework获得的URI表示一个文件夹,并且打开它应该像这样简单:
Intent i = new Intent(Intent.ACTION_VIEW, uri);
startActivity(i);
A,“文件”应用当前包含一个错误,导致您在使用外部存储提供程序尝试此操作时将其崩溃。但是,可以以这种方式显示来自第三方提供商的文件夹。
答案 8 :(得分:-1)
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("text/csv");
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityForResult(Intent.createChooser(intent, "Select a File to Upload"), 0);
} catch (android.content.ActivityNotFoundException ex) {
ex.printStackTrace();
}
然后你只需要添加回复
public void onActivityResult(int requestCode, int resultCode, Intent data){
switch (requestCode) {
case 0: {
//what you want to do
//file = new File(uri.getPath());
}
}
}
答案 9 :(得分:-2)
你似乎很亲密。
我会尝试像这样设置URI:
String folderPath = Environment.getExternalStorageDirectory()+"/pathTo/folder";
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
Uri myUri = Uri.parse(folderPath);
intent.setDataAndType(myUri , "file/*");
startActivity(intent);
但它与你所尝试的并没有太大的不同。 告诉我们它是否有变化?
还要确保目标文件夹存在,并查看生成的 Uri 对象,然后再将其发送到意图,这可能不是我们预期的
答案 10 :(得分:-2)
File temp = File.createTempFile("preview", ".png" );
String fullfileName= temp.getAbsolutePath();
final String fileName = Uri.parse(fullfileName)
.getLastPathSegment();
final String filePath = fullfileName.
substring(0,fullfileName.lastIndexOf(File.separator));
Log.d("filePath", "filePath: " + filePath);
fullfileName :
/mnt/sdcard/Download_Manager_Farsi/preview.png
filePath :
的/ mnt / SD卡/ Download_Manager_Farsi