Android:图库选择器和相机意图在onActivityresult

时间:2016-05-31 08:37:53

标签: java android android-intent camera android-gallery

所以我在我的应用程序中有这个页面,让用户可以选择用相机拍照或从图库中选择图像。

选择带有所选图像集的警告对话框时,将显示确认图标。

现在我的问题出现了,每当我尝试使用图库选择器或相机意图来获取图像时,我的应用程序会立即崩溃并出现错误:

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1888, result=-1, data=null} to activity {my.com.thelorry.ken.thelorrypartner/com.example.Activity}: 

如果我正在使用相机意图,或者如果我使用了图库选择器,则显示:

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1888, result=-1, data=null} to activity {com.example/com.example.Activity}: 

以下是我用来启动相机意图的代码:

public void startCamera(){
    ContentValues values;
    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    values = new ContentValues();
    values.put(MediaStore.Images.Media.TITLE, "New Picture");
    values.put(MediaStore.Images.Media.DESCRIPTION, "From phone Camera");
    imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
    startActivityForResult(cameraIntent, CAMERA_REQUEST);
}

我的代码开始画廊选择器:

public void pickGallery(){
    Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
    photoPickerIntent.setType("image/*");
    startActivityForResult(photoPickerIntent, GALLERY_REQUEST);
}

这是我处理结果的代码:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    photo = null;
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK ) {
        try {
            photo = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri);
            photo = getResizedBitmap(photo, 600, 450);
            File file = new File(imageurl);
            file.delete();
        } catch (Exception e) {
            e.printStackTrace();
        }
        createDialog(photo);
    }if (requestCode == GALLERY_REQUEST && resultCode == RESULT_OK && data != null) {
        Uri pickedImage = data.getData();
        String[] filePath = {MediaStore.Images.Media.DATA};
        Cursor cursor = getContentResolver().query(pickedImage, filePath, null, null, null);
        cursor.moveToFirst();
        String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        Bitmap bitmap = BitmapFactory.decodeFile(imagePath, options);
        photo = getResizedBitmap(bitmap, 600, 450);
        cursor.close();
        createDialog(photo);
    }

}

另外,奇怪的是,根据我的崩溃报告,这些代码似乎只在三星手机上崩溃,而且仅在某些型号上崩溃,例如Galaxy J7,Note5,Note4(到目前为止,应用程序还没有巨大的用户基础)。

编辑:在实际获得可以重现崩溃的手机后,我意识到上面的代码可能不是问题,这就是我在logcats中所拥有的:

Process: com.example, PID: 6220
                                                                               java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1888, result=-1, data=null} to activity {com.example/com.example.Activity}: android.content.res.Resources$NotFoundException: Resource ID #0xffffffff
                                                                                   at android.app.ActivityThread.deliverResults(ActivityThread.java:4019)
                                                                                   at android.app.ActivityThread.handleSendResult(ActivityThread.java:4062)
                                                                                   at android.app.ActivityThread.access$1400(ActivityThread.java:177)
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1483)
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                   at android.os.Looper.loop(Looper.java:135)
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:5930)
                                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                                   at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
                                                                                Caused by: android.content.res.Resources$NotFoundException: Resource ID #0xffffffff
                                                                                   at android.content.res.Resources.getValue(Resources.java:1542)
                                                                                   at android.support.v7.widget.ResourcesWrapper.getValue(ResourcesWrapper.java:204)
                                                                                   at android.support.v7.widget.AppCompatDrawableManager.loadDrawableFromDelegates(AppCompatDrawableManager.java:321)
                                                                                   at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:197)
                                                                                   at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:192)
                                                                                   at android.support.v7.widget.AppCompatImageHelper.setImageResource(AppCompatImageHelper.java:66)
                                                                                   at android.support.v7.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:71)
                                                                                   at com.android.internal.app.AlertController.setupTitle(AlertController.java:565)
                                                                                   at com.android.internal.app.AlertController.setupView(AlertController.java:474)
                                                                                   at com.android.internal.app.AlertController.installContent(AlertController.java:240)
                                                                                   at android.app.AlertDialog.onCreate(AlertDialog.java:356)
                                                                                   at android.app.Dialog.dispatchOnCreate(Dialog.java:373)
                                                                                   at android.app.Dialog.show(Dialog.java:274)
                                                                                   at com.example.Activity.createDialog(Activity.java:697)
                                                                                   at com.example.Activity.onActivityResult(Activity.java:618)
                                                                                   at android.app.Activity.dispatchActivityResult(Activity.java:6441)
                                                                                   at android.app.ActivityThread.deliverResults(ActivityThread.java:4015)
                                                                                   at android.app.ActivityThread.handleSendResult(ActivityThread.java:4062) 
                                                                                   at android.app.ActivityThread.access$1400(ActivityThread.java:177) 
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1483) 
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                   at android.os.Looper.loop(Looper.java:135) 
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:5930) 
                                                                                   at java.lang.reflect.Method.invoke(Native Method) 
                                                                                   at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405) 
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200) 

此外:

FATAL EXCEPTION: main
                                                                               Process: com.example, PID: 9794
                                                                               java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1889, result=-1, data=Intent { dat=content://media/external/images/media/3196 flg=0x1 (has extras) }} to activity {com.example/com.example.Activity}: android.content.res.Resources$NotFoundException: Resource ID #0xffffffff
                                                                                   at android.app.ActivityThread.deliverResults(ActivityThread.java:4019)
                                                                                   at android.app.ActivityThread.handleSendResult(ActivityThread.java:4062)
                                                                                   at android.app.ActivityThread.access$1400(ActivityThread.java:177)
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1483)
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                   at android.os.Looper.loop(Looper.java:135)
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:5930)
                                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                                   at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
                                                                                Caused by: android.content.res.Resources$NotFoundException: Resource ID #0xffffffff
                                                                                   at android.content.res.Resources.getValue(Resources.java:1542)
                                                                                   at android.support.v7.widget.ResourcesWrapper.getValue(ResourcesWrapper.java:204)
                                                                                   at android.support.v7.widget.AppCompatDrawableManager.loadDrawableFromDelegates(AppCompatDrawableManager.java:321)
                                                                                   at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:197)
                                                                                   at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:192)
                                                                                   at android.support.v7.widget.AppCompatImageHelper.setImageResource(AppCompatImageHelper.java:66)
                                                                                   at android.support.v7.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:71)
                                                                                   at com.android.internal.app.AlertController.setupTitle(AlertController.java:565)
                                                                                   at com.android.internal.app.AlertController.setupView(AlertController.java:474)
                                                                                   at com.android.internal.app.AlertController.installContent(AlertController.java:240)
                                                                                   at android.app.AlertDialog.onCreate(AlertDialog.java:356)
                                                                                   at android.app.Dialog.dispatchOnCreate(Dialog.java:373)
                                                                                   at android.app.Dialog.show(Dialog.java:274)
                                                                                   at com.example.Activity.createDialog(Activity.java:697)
                                                                                   at com.example.Activity.onActivityResult(Activity.java:639)
                                                                                   at android.app.Activity.dispatchActivityResult(Activity.java:6441)
                                                                                   at android.app.ActivityThread.deliverResults(ActivityThread.java:4015)
                                                                                   at android.app.ActivityThread.handleSendResult(ActivityThread.java:4062) 
                                                                                   at android.app.ActivityThread.access$1400(ActivityThread.java:177) 
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1483) 
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                   at android.os.Looper.loop(Looper.java:135) 
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:5930) 
                                                                                   at java.lang.reflect.Method.invoke(Native Method) 
                                                                                   at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405) 
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200) 

更多信息,这是我的createDialog函数,实际上会导致问题。

Drawable icon = new BitmapDrawable(this.getResources(),photo);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(getString(R.string.business_post_camera_dialog_title))
                .setMessage(getString(R.string.business_post_camera_dialog_message))
                .setCancelable(false)
                .setPositiveButton(R.string.sign_confirm_btn, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        doSomething();
                    }
                })
                .setNegativeButton(R.string.sign_cancel_btn, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        // do nothing
                    }
                })
                .setIcon(-1).setIcon(icon); //setIcon(-1) as a workaround for bug in Android Lollipop 5.0
        AlertDialog alert = builder.create();
        alert.show();

显然.setIcon(-1)会导致三星手机崩溃,但如果没有它,我就无法在Lollipop 5.0上的某些手机上显示我的图像,任何解决方案?

2 个答案:

答案 0 :(得分:1)

问题在于:

...
Caused by: android.content.res.Resources$NotFoundException: Resource ID #0xffffffff
...

您正尝试将setImageResource(int id)与不存在的图像一起使用。您可以像这样使用它:

imageView.setImageResource(R.drawable.image_name)

TextView也可以使用同样的东西。

textView.setText("" + intValue);

答案 1 :(得分:0)

尝试使用以下方法进行对话,

    int currentapiVersion = android.os.Build.VERSION.SDK_INT;
    Drawable icon = new BitmapDrawable(this.getResources(),photo);
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(getString(R.string.business_post_camera_dialog_title))
            .setMessage(getString(R.string.business_post_camera_dialog_message))
            .setCancelable(false)
            .setPositiveButton(R.string.sign_confirm_btn, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    doSomething();
                }
            })
            .setNegativeButton(R.string.sign_cancel_btn, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // do nothing
                }
            });


         if (currentapiVersion >= android.os.Build.VERSION_CODES.LOLLIPOP){

          builder.setIcon(-1).setIcon(icon);

         } else{
                 builder.setIcon(icon);
        }
    AlertDialog alert = builder.create();
    alert.show();