Android 如何使用文件选择器从外部存储中选择文件
How to pick file from external storage using file picker in Android
我在 Android 中使用文件选择器从外部存储中选择图像文件时遇到问题。这个问题是这个问题的结果 - No such file or diectory error in image file upload using Retrofit in Android。我的问题是在 activity 结果上从外部存储打开和读取文件。我想将结果 URI 转换为文件。
我在 activity 结果
上从下载文件夹中读取了一个 pdf 文件
Uri bookUri = data.getData();
if(bookUri!=null)
{
String filePath = bookUri.toString();//bookUri.toString()
String mime = app.getMimeType(filePath);
if(mime!=null && !mime.isEmpty() && (mime.toLowerCase()=="application/pdf" || mime.toLowerCase()=="application/txt" || mime.toLowerCase()=="application/text"))
{
bookFile = new File(bookUri.getPath());
ivBookFile.setImageResource(R.drawable.book_selected);
}
else{
Toast.makeText(getBaseContext(),"Unable to process file you have chosen.",Toast.LENGTH_SHORT).show();
}
}
如你所见,我使用了 new File(bookUri.getPath());转换成文件。上面的代码运行良好。这是工作。现在的问题是我试图在 activity 结果上打开 DCIM/Camera 文件夹中的图像文件。
这是我使用的代码
Uri selectedImageUri = data.getData();
if(selectedImageUri!=null)
{
try{
bmpCoverImage = MediaStore.Images.Media.getBitmap(getContentResolver(), selectedImageUri);
imageFile = new File(selectedImageUri.getPath());
if(bmpCoverImage!=null)
{
ivCoverImage.setImageBitmap(bmpCoverImage);
}
}
catch (IOException e)
{
Toast.makeText(getBaseContext(),"An error occurred with the file selected",Toast.LENGTH_SHORT).show();
}
}
如你所见,我使用了 new File(selectedImageUri.getPath());就像我在阅读 pdf 文件时所做的那样。这次代码不起作用。当我像上一个问题一样对文件进行操作时,它给我错误。
我也是这样用的
imageFile = new File(Environment.getExternalStorageDirectory(),selectedImageUri.getPath());
我遇到了同样的错误。如何从外部存储正确打开图像文件?如何将所选文件 URI 从外部存储转换为文件?
I am having a problem with selecting image file from external storage using file picker in Android
如果您指的是您在 this question 中使用的代码,那么您不是 "using file picker"。您正在使用 ACTION_GET_CONTENT
,它从来不是 "file picker",也永远不会是 "file picker"。
I want to convert result URI into File.
通常,这是没有必要的。但是,如果那是你想要做的:
使用ContentResolver
和openInputStream()
在Uri
[=57=表示的内容上得到一个InputStream
]
在您想要的文件上创建一个 FileOutputStream
使用Java I/O将字节从InputStream
复制到FileOutputStream
The above code works well. It is working.
它适用于您测试的少数设备,适用于用户选择处理 ACTION_GET_CONTENT
请求的特定活动。它不适用于大多数 Android 设备,并且在大多数情况下都不起作用。只有当 Uri
具有 file
方案时,该代码才有效。大多数时候,它不会。相反,它将有一个 content
方案,表示由 ContentProvider
.
提供的内容
Please how can I open the image file correctly from external storage?
如果您希望继续使用ACTION_GET_CONTENT
,请理解这与外部存储具体无关。您没有在外部存储或其他地方获取文件。你得到了 Uri
。这类似于 URL,例如此网页的 URL。正如 URL 不一定指向硬盘上的文件一样,Uri
也不一定指向文件系统上的文件。使用 ContentResolver
和 DocumentFile
来处理 Uri
及其标识的内容。
如果您希望始终 获取外部存储设备上的文件(而不是其他地方),请使用an actual file picker library.
我在 Android 中使用文件选择器从外部存储中选择图像文件时遇到问题。这个问题是这个问题的结果 - No such file or diectory error in image file upload using Retrofit in Android。我的问题是在 activity 结果上从外部存储打开和读取文件。我想将结果 URI 转换为文件。
我在 activity 结果
上从下载文件夹中读取了一个 pdf 文件Uri bookUri = data.getData();
if(bookUri!=null)
{
String filePath = bookUri.toString();//bookUri.toString()
String mime = app.getMimeType(filePath);
if(mime!=null && !mime.isEmpty() && (mime.toLowerCase()=="application/pdf" || mime.toLowerCase()=="application/txt" || mime.toLowerCase()=="application/text"))
{
bookFile = new File(bookUri.getPath());
ivBookFile.setImageResource(R.drawable.book_selected);
}
else{
Toast.makeText(getBaseContext(),"Unable to process file you have chosen.",Toast.LENGTH_SHORT).show();
}
}
如你所见,我使用了 new File(bookUri.getPath());转换成文件。上面的代码运行良好。这是工作。现在的问题是我试图在 activity 结果上打开 DCIM/Camera 文件夹中的图像文件。
这是我使用的代码
Uri selectedImageUri = data.getData();
if(selectedImageUri!=null)
{
try{
bmpCoverImage = MediaStore.Images.Media.getBitmap(getContentResolver(), selectedImageUri);
imageFile = new File(selectedImageUri.getPath());
if(bmpCoverImage!=null)
{
ivCoverImage.setImageBitmap(bmpCoverImage);
}
}
catch (IOException e)
{
Toast.makeText(getBaseContext(),"An error occurred with the file selected",Toast.LENGTH_SHORT).show();
}
}
如你所见,我使用了 new File(selectedImageUri.getPath());就像我在阅读 pdf 文件时所做的那样。这次代码不起作用。当我像上一个问题一样对文件进行操作时,它给我错误。
我也是这样用的
imageFile = new File(Environment.getExternalStorageDirectory(),selectedImageUri.getPath());
我遇到了同样的错误。如何从外部存储正确打开图像文件?如何将所选文件 URI 从外部存储转换为文件?
I am having a problem with selecting image file from external storage using file picker in Android
如果您指的是您在 this question 中使用的代码,那么您不是 "using file picker"。您正在使用 ACTION_GET_CONTENT
,它从来不是 "file picker",也永远不会是 "file picker"。
I want to convert result URI into File.
通常,这是没有必要的。但是,如果那是你想要做的:
使用
[=57=表示的内容上得到一个ContentResolver
和openInputStream()
在Uri
InputStream
]在您想要的文件上创建一个
FileOutputStream
使用Java I/O将字节从
InputStream
复制到FileOutputStream
The above code works well. It is working.
它适用于您测试的少数设备,适用于用户选择处理 ACTION_GET_CONTENT
请求的特定活动。它不适用于大多数 Android 设备,并且在大多数情况下都不起作用。只有当 Uri
具有 file
方案时,该代码才有效。大多数时候,它不会。相反,它将有一个 content
方案,表示由 ContentProvider
.
Please how can I open the image file correctly from external storage?
如果您希望继续使用ACTION_GET_CONTENT
,请理解这与外部存储具体无关。您没有在外部存储或其他地方获取文件。你得到了 Uri
。这类似于 URL,例如此网页的 URL。正如 URL 不一定指向硬盘上的文件一样,Uri
也不一定指向文件系统上的文件。使用 ContentResolver
和 DocumentFile
来处理 Uri
及其标识的内容。
如果您希望始终 获取外部存储设备上的文件(而不是其他地方),请使用an actual file picker library.