Android - 如何将图像路径转换为内容uri

时间:2013-07-31 02:04:04

标签: android path uri

嘿我想将图片路径翻译成内容uri,我翻译后收到错误, 我把这个方法称为:

uri = getUriFromPath(picturePath);  
tv2.setText(uri.toString());

什么是真的我叫这样的方法

查看我的代码,谢谢


这是我的转换路径到uri的代码

private Uri getUriFromPath(String filePath) {
long photoId;
Uri photoUri = MediaStore.Images.Media.getContentUri("external");

String[] projection = {MediaStore.Images.ImageColumns._ID};
// TODO This will break if we have no matching item in the MediaStore.
Cursor cursor = getContentResolver().query(photoUri, projection, MediaStore.Images.ImageColumns.DATA + " LIKE ?", new String[] { filePath }, null);
cursor.moveToFirst();

int columnIndex = cursor.getColumnIndex(projection[0]);
photoId = cursor.getLong(columnIndex);

cursor.close();
return Uri.parse(photoUri.toString() + "/" + photoId);
}

我收到错误

07-31 08:46:12.239: E/AndroidRuntime(29041): FATAL EXCEPTION: main
07-31 08:46:12.239: E/AndroidRuntime(29041): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dpapayas.mixmatch1/com.dpapayas.mixmatch1.editPhoto}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
07-31 08:46:12.239: E/AndroidRuntime(29041):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2071)
07-31 08:46:12.239: E/AndroidRuntime(29041):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2096)
07-31 08:46:12.239: E/AndroidRuntime(29041):    at android.app.ActivityThread.access$600(ActivityThread.java:138)
07-31 08:46:12.239: E/AndroidRuntime(29041):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1207)
07-31 08:46:12.239: E/AndroidRuntime(29041):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-31 08:46:12.239: E/AndroidRuntime(29041):    at android.os.Looper.loop(Looper.java:213)
07-31 08:46:12.239: E/AndroidRuntime(29041):    at android.app.ActivityThread.main(ActivityThread.java:4787)
07-31 08:46:12.239: E/AndroidRuntime(29041):    at java.lang.reflect.Method.invokeNative(Native Method)
07-31 08:46:12.239: E/AndroidRuntime(29041):    at java.lang.reflect.Method.invoke(Method.java:511)
07-31 08:46:12.239: E/AndroidRuntime(29041):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
07-31 08:46:12.239: E/AndroidRuntime(29041):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
07-31 08:46:12.239: E/AndroidRuntime(29041):    at dalvik.system.NativeStart.main(Native Method)
07-31 08:46:12.239: E/AndroidRuntime(29041): Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
07-31 08:46:12.239: E/AndroidRuntime(29041):    at android.database.AbstractCursor.checkPosition(AbstractCursor.java:418)
07-31 08:46:12.239: E/AndroidRuntime(29041):    at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
07-31 08:46:12.239: E/AndroidRuntime(29041):    at android.database.AbstractWindowedCursor.getLong(AbstractWindowedCursor.java:74)
07-31 08:46:12.239: E/AndroidRuntime(29041):    at android.database.CursorWrapper.getLong(CursorWrapper.java:106)
07-31 08:46:12.239: E/AndroidRuntime(29041):    at com.dpapayas.mixmatch1.editPhoto.getUriFromPath(editPhoto.java:75)
07-31 08:46:12.239: E/AndroidRuntime(29041):    at com.dpapayas.mixmatch1.editPhoto.onCreate(editPhoto.java:44)
07-31 08:46:12.239: E/AndroidRuntime(29041):    at android.app.Activity.performCreate(Activity.java:5008)
07-31 08:46:12.239: E/AndroidRuntime(29041):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
07-31 08:46:12.239: E/AndroidRuntime(29041):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2035)
07-31 08:46:12.239: E/AndroidRuntime(29041):    ... 11 more

2 个答案:

答案 0 :(得分:4)

这就是我做到的。

public static Uri getContentUri(Context context, File imageFile) {
    String filePath = imageFile.getAbsolutePath();
    Cursor cursor = context.getContentResolver().query(
            MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
            new String[] { MediaStore.Audio.Media._ID },
            MediaStore.Audio.Media.DATA + "=? ",
            new String[] { filePath }, null);

    if (cursor != null && cursor.moveToFirst()) {
        int id = cursor.getInt(cursor
                .getColumnIndex(MediaStore.MediaColumns._ID));
        Uri baseUri = Uri.parse("content://media/external/audio/media");
        return Uri.withAppendedPath(baseUri, "" + id);

    }
}

这里我的参数是File,并从中查找路径。你可以自己传递路径。

答案 1 :(得分:0)

使用此

     Uri myUri = Uri.parse("http://www.google.com");