我onActivityResult从mediastore图像选择返回,我可以使用以下内容获取图像的URI:
Uri selectedImage = data.getData();
Run Code Online (Sandbox Code Playgroud)
将其转换为字符串可以得到:
content://media/external/images/media/47
Run Code Online (Sandbox Code Playgroud)
或者路径给出:
/external/images/media/47
Run Code Online (Sandbox Code Playgroud)
但是我似乎无法找到将其转换为绝对路径的方法,因为我想将图像加载到位图而不必将其复制到某处.我知道这可以使用URI和内容解析器完成,但这似乎在重新启动手机时中断,我想MediaStore在重新启动之间不会保持其编号相同.
我正在使用以下代码启动选择documnets的意图.
private void showFileChooser() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityForResult(
Intent.createChooser(intent, "Select a File to Upload"), 1);
} catch (android.content.ActivityNotFoundException ex) {
// Potentially direct the user to the Market with a Dialog
Toast.makeText(this, "Please install a File Manager.",
Toast.LENGTH_SHORT).show();
}
}
Run Code Online (Sandbox Code Playgroud)
在onActivity结果当我试图获取文件路径时,它在文件名的位置给出了一些其他数字.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 1:
if (resultCode == RESULT_OK) {
// Get the Uri of the selected file
Uri uri = data.getData(); …Run Code Online (Sandbox Code Playgroud)