java.lang.RuntimeException : Failure delivering result ResultInfo when capturing image 错误

标签 java android

我正在开发一个允许拍照并将照片保存到 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
    }
}

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

最佳答案

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

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 : Failure delivering result ResultInfo when capturing image 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18440071/

相关文章:

java - 在 java 中使用 Scanner 类以避免看到引号

java - 当我增加 tomcat 上的负载时,log4j2 ParameterFormatter.appendCollection 中出现 ConcurrentModificationException

java - 未找到测试运行程序 JUnit 4 的测试

android - 在没有浏览器的嵌入式设备上使用 google api 访问应用程序中的用户数据

android - Android 的拖放功能

java - 如何将 currentTimeMillis 转换为 Java 中的日期?

java - 从字符串数组中删除逗号

javascript - Highcharts - 数据不会加载到 Android WebView 上的图表中

Android 不尊重颜色!

android - 如何在不从 Azure 下载 Blob 的情况下查看它