java.lang.RuntimeException的:捕捉图像时未能提供结果ResultInfo [英] java.lang.RuntimeException: Failure delivering result ResultInfo when capturing image

查看:126
本文介绍了java.lang.RuntimeException的:捕捉图像时未能提供结果ResultInfo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作,允许拍照,并将其保存到SD卡上的目录中的应用程序。它工作正常,在大多数的设备,但是我得到这个错误在极少数的设备:

I'm working on an application that allows taking photos and saving them into a directory on the SD card. It works fine on most of the devices, but I'm getting this error on very few devices:

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

下面是我如何创建我的文件夹,保存图像和启动相机

Here's how I create my folder, save images and launch camera

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

下面是发生错误的部分:

Here's the part where the error occurs:

@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
    }
}

谁能帮我看看有什么不对吗?为什么它在某些设备上,但不能在其他人的工作?谢谢你。

Can anyone help me find what's wrong please? And why does it work on some devices but not on others? Thanks.

推荐答案

试试这个,这就像魅力对我来说工作:

Try this, it's working like charm for me:

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

这篇关于java.lang.RuntimeException的:捕捉图像时未能提供结果ResultInfo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆