1

当我拍照时,我收到下面的错误。请注意,该应用程序在 HTC rhyme 和 Galaxy Tab 7.7 上完美运行。如何修复?

    08-06 13:18:29.084: E/AndroidRuntime(27803): FATAL EXCEPTION: main
08-06 13:18:29.084: E/AndroidRuntime(27803): java.lang.RuntimeException: Unable to resume activity {ua.mirkvartir.android.frontend/ua.mirkvartir.android.frontend.AddFillActivityApp}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=12, result=-1, data=null} to activity {ua.mirkvartir.android.frontend/ua.mirkvartir.android.frontend.AddFillActivityApp}: java.lang.NullPointerException
08-06 13:18:29.084: E/AndroidRuntime(27803):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2608)
08-06 13:18:29.084: E/AndroidRuntime(27803):    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2636)
08-06 13:18:29.084: E/AndroidRuntime(27803):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2103)
08-06 13:18:29.084: E/AndroidRuntime(27803):    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3576)
08-06 13:18:29.084: E/AndroidRuntime(27803):    at android.app.ActivityThread.access$700(ActivityThread.java:138)
08-06 13:18:29.084: E/AndroidRuntime(27803):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
08-06 13:18:29.084: E/AndroidRuntime(27803):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-06 13:18:29.084: E/AndroidRuntime(27803):    at android.os.Looper.loop(Looper.java:137)
08-06 13:18:29.084: E/AndroidRuntime(27803):    at android.app.ActivityThread.main(ActivityThread.java:4905)
08-06 13:18:29.084: E/AndroidRuntime(27803):    at java.lang.reflect.Method.invokeNative(Native Method)
08-06 13:18:29.084: E/AndroidRuntime(27803):    at java.lang.reflect.Method.invoke(Method.java:511)
08-06 13:18:29.084: E/AndroidRuntime(27803):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
08-06 13:18:29.084: E/AndroidRuntime(27803):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
08-06 13:18:29.084: E/AndroidRuntime(27803):    at dalvik.system.NativeStart.main(Native Method)
08-06 13:18:29.084: E/AndroidRuntime(27803): Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=12, result=-1, data=null} to activity {ua.mirkvartir.android.frontend/ua.mirkvartir.android.frontend.AddFillActivityApp}: java.lang.NullPointerException
08-06 13:18:29.084: E/AndroidRuntime(27803):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3205)
08-06 13:18:29.084: E/AndroidRuntime(27803):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2595)
08-06 13:18:29.084: E/AndroidRuntime(27803):    ... 13 more
08-06 13:18:29.084: E/AndroidRuntime(27803): Caused by: java.lang.NullPointerException
08-06 13:18:29.084: E/AndroidRuntime(27803):    at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1094)
08-06 13:18:29.084: E/AndroidRuntime(27803):    at android.content.ContentResolver.query(ContentResolver.java:354)
08-06 13:18:29.084: E/AndroidRuntime(27803):    at android.content.ContentResolver.query(ContentResolver.java:313)
08-06 13:18:29.084: E/AndroidRuntime(27803):    at android.support.v4.content.CursorLoader.loadInBackground(CursorLoader.java:49)
08-06 13:18:29.084: E/AndroidRuntime(27803):    at ua.mirkvartir.android.frontend.AddFillActivityApp.getRealPathFromURI(AddFillActivityApp.java:844)
08-06 13:18:29.084: E/AndroidRuntime(27803):    at ua.mirkvartir.android.frontend.AddFillActivityApp.onActivityResult(AddFillActivityApp.java:704)
08-06 13:18:29.084: E/AndroidRuntime(27803):    at android.app.Activity.dispatchActivityResult(Activity.java:5492)
08-06 13:18:29.084: E/AndroidRuntime(27803):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3201)

forResult 调用

Calendar c = Calendar.getInstance();
                                ContentValues values = new ContentValues();
                                values.put(MediaStore.Images.Media.TITLE,
                                        "zdanie " + c.getTime());
                                mCapturedImageURI = getContentResolver()
                                        .insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                                                values);
                                Intent intentPicture = new Intent(
                                        MediaStore.ACTION_IMAGE_CAPTURE);
                                intentPicture.putExtra(MediaStore.EXTRA_OUTPUT,
                                        mCapturedImageURI);
                                startActivityForResult(intentPicture, 12);

onResult

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 12 && resultCode == RESULT_OK) {
            // photo1 = getRealPathFromURI(mCapturedImageURI);
            saveFile(
                    decodeSampledBitmapFromResource(
                            getRealPathFromURI(mCapturedImageURI), 800, 800), 1);
            pho1.setImageBitmap(decodeSampledBitmapFromResource(
                    getRealPathFromURI(mCapturedImageURI), 80, 60));
            miss1 = 1;

        } 
}

获取真实路径

   public String getRealPathFromURI(Uri contentUri) {
        if (contentUri==null){
            Log.e("RealPath", "URI: null");
        }else Log.e("RealPath", "URI: "+contentUri.toString());
        try {
            String[] proj = { MediaStore.Images.Media.DATA };
            CursorLoader loader = new CursorLoader(getApplicationContext(),
                    contentUri, proj, null, null, null);
            Cursor cursor = loader.loadInBackground();
            int column_index = cursor
                    .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        } catch (Exception e) {
            Log.e("RealPath", e.toString());
            e.printStackTrace();
        }
        return "";
    }

来自 realpath 捕获块的 StackTrace

   08-06 15:33:23.520: W/System.err(22083): java.lang.NullPointerException
08-06 15:33:23.536: W/System.err(22083):    at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1097)
08-06 15:33:23.544: W/System.err(22083):    at android.content.ContentResolver.query(ContentResolver.java:357)
08-06 15:33:23.544: W/System.err(22083):    at android.content.ContentResolver.query(ContentResolver.java:316)
08-06 15:33:23.544: W/System.err(22083):    at android.support.v4.content.CursorLoader.loadInBackground(CursorLoader.java:49)
08-06 15:33:23.544: W/System.err(22083):    at ua.mirkvartir.android.frontend.AddFillActivityApp.getRealPathFromURI(AddFillActivityApp.java:842)
08-06 15:33:23.544: W/System.err(22083):    at ua.mirkvartir.android.frontend.AddFillActivityApp.onActivityResult(AddFillActivityApp.java:733)
08-06 15:33:23.544: W/System.err(22083):    at android.app.Activity.dispatchActivityResult(Activity.java:5436)
08-06 15:33:23.544: W/System.err(22083):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3188)
08-06 15:33:23.544: W/System.err(22083):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2592)
08-06 15:33:23.544: W/System.err(22083):    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2633)
08-06 15:33:23.544: W/System.err(22083):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2100)
08-06 15:33:23.544: W/System.err(22083):    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3563)
08-06 15:33:23.544: W/System.err(22083):    at android.app.ActivityThread.access$700(ActivityThread.java:135)
08-06 15:33:23.544: W/System.err(22083):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1207)
08-06 15:33:23.544: W/System.err(22083):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-06 15:33:23.552: W/System.err(22083):    at android.os.Looper.loop(Looper.java:137)
08-06 15:33:23.552: W/System.err(22083):    at android.app.ActivityThread.main(ActivityThread.java:4849)
08-06 15:33:23.552: W/System.err(22083):    at java.lang.reflect.Method.invokeNative(Native Method)
08-06 15:33:23.552: W/System.err(22083):    at java.lang.reflect.Method.invoke(Method.java:511)
08-06 15:33:23.552: W/System.err(22083):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
08-06 15:33:23.559: W/System.err(22083):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
08-06 15:33:23.559: W/System.err(22083):    at dalvik.system.NativeStart.main(Native Method)

编辑我在另外 5 部手机上进行了测试,一切正常。只有 LG 手机会导致问题。 编辑:我尝试使用游标加载器而不是托管查询。同样的问题

4

2 回答 2

3

如果您想知道为什么它在某些设备上没有任何问题,而在某些设备上却没有:

正如您可能看到的 - 相机应用程序不是同一个应用程序。事实上,每个相机应用程序都是完全不同的,即使是随库存一起提供的应用程序也是如此。

这和你的问题有什么关系?MediaStore.ACTION_IMAGE_CAPTURE - 没有人阻止对完全忽略 MediaStore.EXTRA_OUTPUT额外意图 做出反应的应用程序。

您的代码假定用户选择的应用程序(或安装的默认相机应用程序)将确保将捕获的图像存储在指定的路径中。不幸的是,我从经验中知道这并不总是正确的。

悲伤但真实。MediaStore.EXTRA_OUTPUT如果您认为您的用户会使用此类相机应用程序或特定设备(如 LG 系列默认相机应用程序),则无法指望。

在这种情况下,也许你可以通过@Arun C Thomas 建议的方法获取图像,但我很伤心 - 如果相机应用程序没有将数据本身存储在结果中,getData() 不一定会给你带来任何东西。

这就是为什么你必须 varify null,或者做一些“try/catch”来防止你的应用程序在这种情况下崩溃......

于 2013-08-10T07:43:27.850 回答
0

大多数情况下,返回的光标都是null这样,当您尝试获取column_index.

Cursor cursor = managedQuery(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
于 2013-08-02T11:23:47.087 回答