2

嘿,我想将图像路径翻译成内容 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
4

2 回答 2

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);

    }
}

这里我的参数是文件,并从中找到路径。您可以通过路径本身。

于 2013-07-31T04:28:43.247 回答
0

用这个

     Uri myUri = Uri.parse("http://www.google.com");
于 2013-09-02T08:57:04.820 回答