1

I try get real file path from URI after taking photo from camera. I do this:

private String getRealPathFromURI(Uri contentURI) {
    Cursor cursor = getContentResolver().query(contentURI, null, null, null, null); 
    cursor.moveToFirst(); 
    int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); 
    return cursor.getString(idx); 
}

and this:

 public String getRealPathFromURI(Uri contentUri) {
     String[] proj = { MediaColumns.DATA };
     Cursor cursor = managedQuery(contentUri, proj, null, null, null);
     int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
     cursor.moveToFirst();
     return cursor.getString(column_index);
     }

and:

private String getRealPathFromURI(Uri selectedVideoUri) {
    String filePath;
    final String[] filePathColumn = new String[] { MediaColumns.DATA };

    Cursor cursor = getContentResolver().query(selectedVideoUri,
            filePathColumn, null, null, null);
    cursor.moveToFirst();

    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
    filePath = cursor.getString(columnIndex);
    cursor.close();
    return filePath;
}

on most phones, everything is working correctly. But in some phones app crashes with error:

08-03 14:55:13.092: E/AndroidRuntime(1676):     at com.android.internal.app.AlertController$ButtonHandler.handleMessage(SourceFile:190)
08-03 14:55:13.092: E/AndroidRuntime(1676):     at android.os.Handler.dispatchMessage(SourceFile:130)
08-03 14:55:13.092: E/AndroidRuntime(1676):     at android.os.Looper.loop(SourceFile:351)
08-03 14:55:13.092: E/AndroidRuntime(1676):     at android.app.ActivityThread.main(SourceFile:4070)
08-03 14:55:13.092: E/AndroidRuntime(1676):     at java.lang.reflect.Method.invokeNative(Native Method)
08-03 14:55:13.092: E/AndroidRuntime(1676):     at java.lang.reflect.Method.invoke(Method.java:538)
08-03 14:55:13.092: E/AndroidRuntime(1676):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(SourceFile:901)
08-03 14:55:13.092: E/AndroidRuntime(1676):     at com.android.internal.os.ZygoteInit.main(SourceFile:659)
08-03 14:55:13.092: E/AndroidRuntime(1676):     at dalvik.system.NativeStart.main(Native Method)
08-03 14:55:19.823: I/System.out(1728): --MyLocationListener is created!
08-03 14:55:19.838: D/AndroidRuntime(1728): Shutting down VM
08-03 14:55:19.838: W/dalvikvm(1728): threadid=1: thread exiting with uncaught exception (group=0x4001c648)
08-03 14:55:19.842: E/AndroidRuntime(1728): FATAL EXCEPTION: main
08-03 14:55:19.842: E/AndroidRuntime(1728): java.lang.RuntimeException: Unable to create service com.webparadox.tonquer.MyLocationListener: java.lang.NullPointerException
08-03 14:55:19.842: E/AndroidRuntime(1728):     at android.app.ActivityThread.handleCreateService(SourceFile:2221)
08-03 14:55:19.842: E/AndroidRuntime(1728):     at android.app.ActivityThread.access$2500(SourceFile:160)

Help Me to fix this bug, please

4

2 に答える 2

0

以下のコードが役立つことを願っています

   String[] projection = { MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA,
    };
    Cursor mImageCursor =   getContentResolver().query(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null,
            null, null);

    if (mImageCursor != null) 
    {
        if (mImageCursor.getCount() == 0) 
        {
            setContentView(R.layout.no_image_found);
                        } 
        else
        {

            while (mImageCursor.moveToNext()){
                Images im = new Images();
                int column_index = mImageCursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                im.setFilePath(mImageCursor.getString(column_index));
                image_column_index = mImageCursor
                        .getColumnIndexOrThrow(MediaStore.Images.Media._ID);
                int id = mImageCursor.getInt(image_column_index);
                im.setUri(Uri.withAppendedPath(
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI, ""
                                + id));
                arrayOfImages.add(im);
            }
        }
    }
于 2013-03-04T10:08:56.157 に答える