android – 在文件管理器中打开特定文件夹以供查看
作者:互联网
我为用户下载了一个文件,现在我想将用户导航到下载文件的特定文件夹.我搜索了很多,这是我能想到的最好的
public static void openASpecificFolderInFileManager(Context context, String path) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.setDataAndType(Uri.parse(path), "resource/folder");
if (i.resolveActivityInfo(context.getPackageManager(), 0) != null) {
context.startActivity(Intent.createChooser(i, "Open with"));
} else {
Toast.makeText(context, "File Manager not found ", Toast.LENGTH_SHORT).show();
}
}
这适用于特定的文件浏览器(如ES filemanager),但不适用于默认的android文件管理器.
注意:我不想选择文件.我只想打开特定文件夹,以便用户可以查看文件
有什么方法可以使用默认文件浏览器实现它吗?
解决方法:
测试此项以打开包含文本文件的文件夹,例如:
public class fillEditText extends Activity {
final int REQUEST_CODE=1;//your code for onActivityResult
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getAbsolutePath()
+ "/YourValidFolder/");
intent.setDataAndType(uri, "text/plain");
if (intent.resolveActivity(getPackageManager()) != null)
startActivityForResult(Intent.createChooser(intent, getString("Select your file")), REQUEST_CODE);
}
...}
选择文件后的结果:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Uri selectedfile_uri = data.getData();
try {
String text = readTextFromUri(selectedfile_uri);
if (!text.isEmpty()) {
editText.setText(text);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private String readTextFromUri(Uri uri) throws IOException {
InputStream inputStream = getContentResolver().openInputStream(uri);
BufferedReader reader = new BufferedReader(new InputStreamReader(
inputStream));
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
}
inputStream.close();
return stringBuilder.toString();
}
标签:android,file-manager 来源: https://codeday.me/bug/20190710/1426169.html