Java 如何在android中将`content://media/external/images/media/Y`转换为`file:///storage/sdcard0/Pictures/X.jpg`?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20028319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to convert `content://media/external/images/media/Y` to `file:///storage/sdcard0/Pictures/X.jpg` in android?
提问by Elad Benda
I'm trying to upload image to Google Drive from my android app,
我正在尝试从我的 Android 应用程序将图像上传到 Google Drive,
based on this tutorial.
基于本教程。
When I debug their sample project I see a typical fileUri =
当我调试他们的示例项目时,我看到一个典型的 fileUri =
file:///storage/sdcard0/Pictures/IMG_20131117_090231.jpg
file:///storage/sdcard0/Pictures/IMG_20131117_090231.jpg
In my app I want to upload an existing photo.
在我的应用程序中,我想上传一张现有照片。
I retrieve its path like that
我像那样检索它的路径
private void GetAnyImage()
{
File dir = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/Pictures/Screenshots");
// --> /storage/sdcard0/Pictures/Screenshots
Log.d("File path ", dir.getPath());
String dirPath=dir.getAbsolutePath();
if(dir.exists() && dir.isDirectory()) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
intent.setType("image/*");
startActivityForResult(intent,REQUEST_ID);
}
}
and eventually get this typical fileUri =content://media/external/images/media/74275
并最终得到这个典型的 fileUri =content://media/external/images/media/74275
however when running this line of code
但是当运行这行代码时
private void saveFileToDrive() {
// progressDialog = ProgressDialog.show(this, "", "Loading...");
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
// File's binary content
java.io.File fileContent = new java.io.File(fileUri.getPath());
FileContent mediaContent = new FileContent("image/jpeg", fileContent);
// File's metadata.
File body = new File();
body.setTitle(fileContent.getName());
body.setMimeType("image/jpeg");
File file = service.files().insert(body, mediaContent).execute();
I get an error:
我收到一个错误:
java.io.FileNotFoundException: /external/images/media/74275: open failed: ENOENT (No such file or directory)
java.io.FileNotFoundException: /external/images/media/74275: open failed: ENOENT (No such file or directory)
how can I solve this?
我该如何解决这个问题?
how to convert content://media/external/images/media/Y
to file:///storage/sdcard0/Pictures/X.jpg
?
如何转换content://media/external/images/media/Y
为file:///storage/sdcard0/Pictures/X.jpg
?
采纳答案by Selecsosi
Will something like this work for you? What this does is query the content resolver to find the file path data that is stored for that content entry
这样的事情对你有用吗?这样做是查询内容解析器以查找为该内容条目存储的文件路径数据
public static String getRealPathFromUri(Context context, Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = { MediaStore.Images.Media.DATA };
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} finally {
if (cursor != null) {
cursor.close();
}
}
}
This will end up giving you an absolute file path that you can construct a file uri from
这最终会为您提供一个绝对文件路径,您可以从中构建文件 uri
回答by Maurice
If you just want the bitmap, This too works
如果你只想要位图,这也有效
InputStream inputStream = mContext.getContentResolver().openInputStream(uri);
Bitmap bmp = BitmapFactory.decodeStream(inputStream);
if( inputStream != null ) inputStream.close();
sample uri : content://media/external/images/media/12345
示例 uri:content://media/external/images/media/12345