我想以编程方式打开文件夹。我使用下面的代码,但没有运气
Intent intent = new Intent(Intent.ActionView);
var uri=Uri.Parse("/storage/emulated/0/myFolder");
intent.SetDataAndType(uri, "*/*");
intent.SetFlags(ActivityFlags.NewTask);
// activity.StartActivity(Intent.CreateChooser(intent, "choose folder"));
activity.StartActivity(intent);
它显示了各种应用程序,例如电话,消息传递,ES File Explorer,联系人等。我想打开默认的文件浏览器以打开该文件夹。
答案 0 :(得分:0)
@Amit,您好,如果要打开默认文件浏览器,可以在本机Android中执行此操作:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");//set type , here is setted to every type.
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent,1);
其他类型:
intent.setType("image/*"); //set type be image
intent.setType("audio/*"); //set type be audio
intent.setType("video/*"); //set type be video (mp4 3gp be suported in android)
intent.setType("video/*;image/*");//set type be all video and image
当返回应用程序时,可以在此处处理文件:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
//no choose no continue to here
Uri uri = data.getData();
//get file's url , so last can get file
String[] proj = {MediaStore.Images.Media.DATA};
Cursor actualimagecursor = managedQuery(uri, proj, null, null, null);
int actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
actualimagecursor.moveToFirst();
String img_path = actualimagecursor.getString(actual_image_column_index);
File file = new File(img_path);
Toast.makeText(MainActivity.this, file.toString(), Toast.LENGTH_SHORT).show();
}
}
也可以通过这种方式打开指定的文件,需要使用方法setDataAndType
,如下所示:
intent.setDataAndType(Uri.fromFile(dir.getParentFile()), "file/*.txt");
dir
是File
文件夹,其中的文件如下:
String compName = AppString.getCompanyName();
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + compName + "/OSC_DATA/";
File dir = new File(path);
if (!dir.exists()) {
dir.mkdirs();
}
如果只想使用其他文件浏览器应用程序扫描文件,也可以使用intent打开其他应用程序。但是,您现在应该使用它的程序包名称:
public void doStartApplicationWithPackageName(String packagename) {
PackageInfo packageinfo = null;
try {
packageinfo = mContext.getPackageManager().getPackageInfo(packagename, 0);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
if (packageinfo == null) {
return;
}
Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
resolveIntent.setPackage(packageinfo.packageName);
List<ResolveInfo> resolveinfoList = mContext.getPackageManager()
.queryIntentActivities(resolveIntent, 0);
ResolveInfo resolveinfo = resolveinfoList.iterator().next();
if (resolveinfo != null) {
String packageName = resolveinfo.activityInfo.packageName;
String className = resolveinfo.activityInfo.name;
// LAUNCHER Intent
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
ComponentName cn = new ComponentName(packageName, className);
intent.setComponent(cn);
mContext.startActivity(intent);
}
}
这样使用:
doStartApplicationWithPackageName("com.android.documentsui")
此软件包名称在不同的android系统中可能有所不同。使用这种方法时,请确保当前系统中的软件包名称正确。