use*_*690 8 android android-intent android-afilechooser
我使用此代码使用Intent选择任何类型的文件并在我的应用程序中获取它的路径
//this when button click
public void onBrowse(View view) {
Intent chooseFile;
Intent intent;
chooseFile = new Intent(Intent.ACTION_GET_CONTENT);
chooseFile.addCategory(Intent.CATEGORY_OPENABLE);
chooseFile.setType("file/*");
intent = Intent.createChooser(chooseFile, "Choose a file");
startActivityForResult(intent, ACTIVITY_CHOOSE_FILE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK) return;
String path = "";
if(requestCode == ACTIVITY_CHOOSE_FILE)
{
Uri uri = data.getData();
String FilePath = getRealPathFromURI(uri); // should the path be here in this string
System.out.print("Path = " + FilePath);
}
}
public String getRealPathFromURI(Uri contentUri) {
String [] proj = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query( contentUri, proj, null, null,null);
if (cursor == null) return null;
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
Run Code Online (Sandbox Code Playgroud)
文件浏览器打开时的问题我无法选择一个文件似乎它没有启用当我按下一个文件什么都没发生所以这个代码有什么问题
我从我的Android手机
图像
上传截图
提前谢谢
文件的类型是.txt
然后text/plain用作MIME类型.正如Ian Lake在评论中指出的那样,file/*它不是有效的MIME类型.
除此之外,删除getRealPathFromURI()(这将无效).除了Uri自身之外,没有任何道路.您可以Uri通过拨打openInputStream()a 来阅读由此标识的内容ContentResolver,并且您可以ContentResolver通过拨打getContentResolver()您的电话来获取Activity.
小智 8
换行:
chooseFile.setType("file/*");
Run Code Online (Sandbox Code Playgroud)
至
chooseFile.setType( "*/ *");
Run Code Online (Sandbox Code Playgroud)
这将帮助您选择任何类型的文件.
希望它有效.