【问题标题】:java.lang.RuntimeException: Failure delivering result ResultInfo when capturing imagejava.lang.RuntimeException:捕获图像时传递结果ResultInfo失败
【发布时间】:2013-08-26 08:45:52
【问题描述】:

我正在开发一个允许拍照并将照片保存到 SD 卡上的目录中的应用程序。它在大多数设备上都能正常工作,但在极少数设备上出现此错误:

08-26 15:29:11.712 11925 11925 E AndroidRuntime: FATAL EXCEPTION: main
08-26 15:29:11.712 11925 11925 E AndroidRuntime: java.lang.RuntimeException: Failure    delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { act=inline-data dat=file:///storage/emulated/0/.MyImgs/IMG_20132926032905.jpg typ=image/jpeg (has extras) }} to activity {com.example.myApp/com.example.myApp.Com}: java.lang.NullPointerException
08-26 15:29:11.712 11925 11925 E AndroidRuntime:    at android.app.ActivityThread.deliverResults(ActivityThread.java:3403)
08-26 15:29:11.712 11925 11925 E AndroidRuntime:    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3446)
08-26 15:29:11.712 11925 11925 E AndroidRuntime:    at android.app.ActivityThread.access$1100(ActivityThread.java:150)
08-26 15:29:11.712 11925 11925 E AndroidRuntime:    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324)
08-26 15:29:11.712 11925 11925 E AndroidRuntime:    at android.os.Handler.dispatchMessage(Handler.java:99)
08-26 15:29:11.712 11925 11925 E AndroidRuntime:    at android.os.Looper.loop(Looper.java:213)
08-26 15:29:11.712 11925 11925 E AndroidRuntime:    at android.app.ActivityThread.main(ActivityThread.java:5153)
08-26 15:29:11.712 11925 11925 E AndroidRuntime:    at java.lang.reflect.Method.invokeNative(Native Method)
08-26 15:29:11.712 11925 11925 E AndroidRuntime:    at java.lang.reflect.Method.invoke(Method.java:511)
08-26 15:29:11.712 11925 11925 E AndroidRuntime:    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-26 15:29:11.712 11925 11925 E AndroidRuntime:    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-26 15:29:11.712 11925 11925 E AndroidRuntime:    at dalvik.system.NativeStart.main(Native Method)
08-26 15:29:11.712 11925 11925 E AndroidRuntime: Caused by: java.lang.NullPointerException
08-26 15:29:11.712 11925 11925 E AndroidRuntime:    at android.os.Parcel.readException(Parcel.java:1445)
08-26 15:29:11.712 11925 11925 E AndroidRuntime:    at android.os.Parcel.readException(Parcel.java:1389)
08-26 15:29:11.712 11925 11925 E AndroidRuntime:    at android.content.IContentService$Stub$Proxy.notifyChange(IContentService.java:472)
08-26 15:29:11.712 11925 11925 E AndroidRuntime:    at android.content.ContentResolver.notifyChange(ContentResolver.java:1297)
08-26 15:29:11.712 11925 11925 E AndroidRuntime:    at android.content.ContentResolver.notifyChange(ContentResolver.java:1286)
08-26 15:29:11.712 11925 11925 E AndroidRuntime:    at android.content.ContentResolver.notifyChange(ContentResolver.java:1266)
-->08-26 15:29:11.712 11925 11925 E AndroidRuntime:     at com.example.myApp.Com.onActivityResult(Com.java:265)<--
08-26 15:29:11.712 11925 11925 E AndroidRuntime:    at android.app.Activity.dispatchActivityResult(Activity.java:5293)
08-26 15:29:11.712 11925 11925 E AndroidRuntime:    at android.app.ActivityThread.deliverResults(ActivityThread.java:3399)
08-26 15:29:11.712 11925 11925 E AndroidRuntime:    ... 11 more

以下是我创建文件夹、保存图像和启动相机的方法

String root = Environment.getExternalStorageDirectory()+"/.MyImgs/";
File newDirectory = new File(root);
//create directory if it doesn't exist
if(!newDirectory.exists())
    newDirectory.mkdirs();

//create and name the image
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
String date = dateFormat.format(new Date());
File f = new File(newDirectory,"IMG_"+date+".jpg");

//save image directory
mUri = Uri.fromFile(f);
// Result of activity: TAKE_PICTURE
startActivityForResult(i, TAKE_PICTURE); 

这是发生错误的部分:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == Activity.RESULT_OK) {
        getContentResolver().notifyChange(mUri, null);//line 265
    }
}

谁能帮我找出问题所在?为什么它在某些设备上有效,而在其他设备上无效?谢谢。

【问题讨论】:

    标签: java android


    【解决方案1】:

    试试这个,它对我来说很有魅力:

    private String selectedImagePath = "";
    final private int PICK_IMAGE = 1;
    final private int CAPTURE_IMAGE = 2;
    
    public Uri setImageUri() {
        // Store image in dcim
        File file = new File(Environment.getExternalStorageDirectory() + "/DCIM/", "image" + new Date().getTime() + ".png");
        Uri imgUri = Uri.fromFile(file);
        this.imgPath = file.getAbsolutePath();
        return imgUri;
    }
    
    
    public String getImagePath() {
        return imgPath;
    }
    
    btnGallery.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent, ""), PICK_IMAGE);
        }
    });
    
    btnCapture.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, setImageUri());
            startActivityForResult(intent, CAPTURE_IMAGE);
        }
    });
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != Activity.RESULT_CANCELED) {
            if (requestCode == PICK_IMAGE) {
                selectedImagePath = getAbsolutePath(data.getData());
                imgUser.setImageBitmap(decodeFile(selectedImagePath));
            } else if (requestCode == CAPTURE_IMAGE) {
                selectedImagePath = getImagePath();
                imgUser.setImageBitmap(decodeFile(selectedImagePath));
            } else {
                super.onActivityResult(requestCode, resultCode, data);
            }
        }
    }
    
    
    public Bitmap decodeFile(String path) {
        try {
            // Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(path, o);
            // The new size we want to scale to
            final int REQUIRED_SIZE = 70;
            // Find the correct scale value. It should be the power of 2.
            int scale = 1;
            while (o.outWidth / scale / 2 >= REQUIRED_SIZE && o.outHeight / scale / 2 >= REQUIRED_SIZE)
                scale *= 2;
            // Decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            return BitmapFactory.decodeFile(path, o2);
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return null;
    }
    
    public String getAbsolutePath(Uri uri) {
        String[] projection = { MediaColumns.DATA };
        @SuppressWarnings("deprecation")
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        if (cursor != null) {
            int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        } else
            return null;
    }
    

    【讨论】:

    • 还是有上面的问题可以通过这个链接找到我的pastebin.com/pH8FTxwX
    • 在 onActivityResult 函数中检查 if (data != null) 还是比较安全的,它保证了android不同版本的奇怪东西..
    猜你喜欢
    • 2013-10-16
    • 2017-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-13
    • 1970-01-01
    • 2021-06-18
    • 2012-04-07
    相关资源
    最近更新 更多