从文件存储中选择文件后如何从文件选择器中获取文件路径
how to get the file path from the file chooser after selecting the file from the file storage
我正在尝试创建一个带有按钮的个人聊天应用程序。单击该按钮时,框架变得可见,单击选择器上的按钮是这样创建的。单击图像按钮时,它会在选择我已将其保存在图像文件 Uri 中的文件后创建选择器 activity。
但是当我尝试使用 data.getdata().getpath()
方法获取文件路径时,它给出 doument/236
作为输出,但没有给出文件的实际路径。当我尝试使用 fileutlis 获取路径时,它显示 "can't resolve fileutils"。请帮助我,以便我可以获得我的文件的路径。
imagesend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
checker = "image";
Intent imageIntent = new Intent();
imageIntent.setAction(Intent.ACTION_GET_CONTENT);
imageIntent.setType("image/*");
startActivityForResult(Intent.createChooser(imageIntent,"Select Image"),438);
}
});
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 438 && resultCode == RESULT_OK && data!=null && data.getData()!=null){
loadingBar.setTitle("Sending Message");
loadingBar.setMessage("Please wait...");
loadingBar.setCanceledOnTouchOutside(false);
loadingBar.show();
imagefile = data.getData();
String filepath = data.getData().getPath();
if(checker.equals("pdf")){
pdfFilemessage();
}else if(checker.equals("image")){
//imagefilemessage();
Toast.makeText(personalChat.this,filepath,Toast.LENGTH_SHORT).show();
}
}
}
你可以试试这个:
Uri fileUri = intent.getData();
现在您可以使用下面的要点文件从 URI 生成文件路径
这一行 returns 文件的 Uri。
imagefile = data.getData();
你要做的是,
public String getRealPathFromURI(Uri contentUri) {
String[] proj = {
MediaStore.Audio.Media.DATA
};
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
将此添加到您的代码中。
String filePath = this.getRealPathFromURI(imagefile);
参考这个:
使用 Android 10,默认情况下您将无法通过路径直接访问文件,因为 Google 迫使人们使用存储访问框架和/或 MediaStore 来访问文件。
对于 Android11,他们的计划是使其成为访问文件的唯一方式。
详情见https://developer.android.com/training/data-storage/files/external-scoped。
您将需要使用 ContentResolver#openFileDescriptor(Uri, String)
和 ParcelFileDescriptor#getFileDescriptor()
获得可用的东西
例如
ParcelFileDescriptor pfd =
this.getContentResolver().
openFileDescriptor(Uri, "r");
FileInputStream fileInputStream =
new FileInputStream(
pfd.getFileDescriptor());
如果您想获取有关该文件的其他信息,您可以使用 DISPLAY_NAME 或 MIME_TYPE 文件类型的 URI 上的 contentResolver 查询来实现。
例如
// useful name to display to user
Cursor cursor = this.getContentResolver()
.query(Uri, new String[] { MediaStore.Files.FileColumns.DISPLAY_NAME},
null, null, null);
cursor.moveToFirst();
filename = cursor.getString(0);
cursor.close();
我正在尝试创建一个带有按钮的个人聊天应用程序。单击该按钮时,框架变得可见,单击选择器上的按钮是这样创建的。单击图像按钮时,它会在选择我已将其保存在图像文件 Uri 中的文件后创建选择器 activity。
但是当我尝试使用 data.getdata().getpath()
方法获取文件路径时,它给出 doument/236
作为输出,但没有给出文件的实际路径。当我尝试使用 fileutlis 获取路径时,它显示 "can't resolve fileutils"。请帮助我,以便我可以获得我的文件的路径。
imagesend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
checker = "image";
Intent imageIntent = new Intent();
imageIntent.setAction(Intent.ACTION_GET_CONTENT);
imageIntent.setType("image/*");
startActivityForResult(Intent.createChooser(imageIntent,"Select Image"),438);
}
});
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 438 && resultCode == RESULT_OK && data!=null && data.getData()!=null){
loadingBar.setTitle("Sending Message");
loadingBar.setMessage("Please wait...");
loadingBar.setCanceledOnTouchOutside(false);
loadingBar.show();
imagefile = data.getData();
String filepath = data.getData().getPath();
if(checker.equals("pdf")){
pdfFilemessage();
}else if(checker.equals("image")){
//imagefilemessage();
Toast.makeText(personalChat.this,filepath,Toast.LENGTH_SHORT).show();
}
}
}
你可以试试这个:
Uri fileUri = intent.getData();
现在您可以使用下面的要点文件从 URI 生成文件路径
这一行 returns 文件的 Uri。
imagefile = data.getData();
你要做的是,
public String getRealPathFromURI(Uri contentUri) {
String[] proj = {
MediaStore.Audio.Media.DATA
};
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
将此添加到您的代码中。
String filePath = this.getRealPathFromURI(imagefile);
参考这个:
使用 Android 10,默认情况下您将无法通过路径直接访问文件,因为 Google 迫使人们使用存储访问框架和/或 MediaStore 来访问文件。
对于 Android11,他们的计划是使其成为访问文件的唯一方式。
详情见https://developer.android.com/training/data-storage/files/external-scoped。
您将需要使用 ContentResolver#openFileDescriptor(Uri, String)
和 ParcelFileDescriptor#getFileDescriptor()
获得可用的东西
例如
ParcelFileDescriptor pfd =
this.getContentResolver().
openFileDescriptor(Uri, "r");
FileInputStream fileInputStream =
new FileInputStream(
pfd.getFileDescriptor());
如果您想获取有关该文件的其他信息,您可以使用 DISPLAY_NAME 或 MIME_TYPE 文件类型的 URI 上的 contentResolver 查询来实现。
例如
// useful name to display to user
Cursor cursor = this.getContentResolver()
.query(Uri, new String[] { MediaStore.Files.FileColumns.DISPLAY_NAME},
null, null, null);
cursor.moveToFirst();
filename = cursor.getString(0);
cursor.close();