如何以编程方式在android中打开文件夹?

时间:2014-06-30 19:06:32

标签: java android

我正在开设一个打开文件夹的Android应用程序,

我的问题是如何在Android中以编程方式打开文件夹?

我尝试了堆栈溢出中可用的所有解决方案并在Google中搜索但我找不到解决方案。有人可以给我一个答案吗?

提前致谢。

2 个答案:

答案 0 :(得分:1)

public void AccessingFiles(View view) {
   Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
   intent.setType("*/*");
   intent.addCategory(Intent.CATEGORY_OPENABLE);

   // special intent for Samsung file manager
   Intent sIntent = new Intent("com.sec.android.app.myfiles.PICK_DATA");
   // if you want any file type, you can skip next line 
   sIntent.putExtra("CONTENT_TYPE", "*/*"); 
   sIntent.addCategory(Intent.CATEGORY_DEFAULT);

   Intent chooserIntent;
   if (getPackageManager().resolveActivity(sIntent, 0) != null){
      // it is device with samsung file manager
      chooserIntent = Intent.createChooser(sIntent, "Open file");
      chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { intent});
   }
   else {
      chooserIntent = Intent.createChooser(intent, "Open file");
   }

   try {
      startActivityForResult(chooserIntent, CHOOSE_FILE_REQUESTCODE);
   } 
   catch (android.content.ActivityNotFoundException ex) {
      Toast.makeText(getApplicationContext(), "No suitable File Manager was found.", Toast.LENGTH_SHORT).show();
   }
}

答案 1 :(得分:0)

您可以使用以下代码打开文件夹

File file = new File(path);
        Uri uri_path = Uri.fromFile(file);
        String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension
                (MimeTypeMap.getFileExtensionFromUrl(path));


        Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
        intent.setType(mimeType);
        intent.setDataAndType(uri_path, mimeType);
        startActivity(intent);