当我想要更改imageView时,intent.getData()为null

时间:2014-04-01 12:56:37

标签: java android

当我点击我的imageView时,我想更改我的头像。

代码:

ivAvatar.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            final CharSequence[] items = {"Prendre une photo",
                    "Choisir une image", "Annuler"};

            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                    getActivity());

            // set title
            alertDialogBuilder.setTitle("Avatar :");

            // set dialog message
            alertDialogBuilder.setCancelable(false).setItems(items,
                    new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int item) {

                    if (item == 0) {
                        ContentValues values = new ContentValues();
                        values.put(MediaStore.Images.Media.TITLE,
                                "");
                        mCapturedImageURI = getActivity()
                                .getContentResolver()
                                .insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                                        values);

                        Intent intent = new Intent(
                                MediaStore.ACTION_IMAGE_CAPTURE);

                        intent.putExtra(MediaStore.EXTRA_OUTPUT,
                                mCapturedImageURI);
                        intent.putExtra("crop", "true");
                        intent.putExtra("aspectX", 170);
                        intent.putExtra("aspectY", 170);
                        intent.putExtra("outputX", 5000);
                        intent.putExtra("outputY", 5000);
                        startActivityForResult(Intent
                                .createChooser(intent,
                                        "Appareil photo"),
                                        CAPTURE_PICTURE);
                    }

                    if (item == 1) {
                        Intent intent = new Intent(
                                Intent.ACTION_PICK, 
                                android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);

                        intent.setType("image/*");
                        intent.putExtra("crop", "true");
                        intent.putExtra("aspectX", 170);
                        intent.putExtra("aspectY", 170);
                        intent.putExtra("outputX", 5000);
                        intent.putExtra("outputY", 5000);
                        startActivityForResult(Intent
                                .createChooser(intent,
                                        "Choisir une application"),
                                        SELECT_PICTURE);
                    }

                    if (item == 2) {
                        dialog.cancel();
                    }

                }
            });

            // create alert dialog
            AlertDialog alertDialog = alertDialogBuilder.create();

            // afficher
            alertDialogBuilder.show();

        }
    });

public void onActivityResult(int requestCode, int resultCode, Intent data) {

    Log.i("TAG", data + " : data");
    Log.i("TAG", data.getData() + " : data.getData()");
    Log.i("TAG", getActivity().getApplicationContext() + " : context");

    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == CAPTURE_PICTURE) {

            try {
                ivAvatar.setImageBitmap(decodeUri(getActivity().getApplicationContext(),
                        mCapturedImageURI, 1000));
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

        if (requestCode == SELECT_PICTURE) {

            Uri selectedimg = data.getData();
            try {
                ivAvatar.setImageBitmap(decodeUri(getActivity().getApplicationContext(), selectedimg, 170));
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }
}

public static Bitmap decodeUri(Context c, Uri uri, final int requiredSize)
        throws FileNotFoundException {
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(c.getContentResolver().openInputStream(uri),
            null, o);

    int widthTmp = o.outWidth, heightTmp = o.outHeight;
    int scale = 1;

    while (true) {
        if (widthTmp / 2 < requiredSize || heightTmp / 2 < requiredSize) {
            break;
        }
        widthTmp /= 2;
        heightTmp /= 2;
        scale *= 2;
    }

    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;
    return BitmapFactory.decodeStream(c.getContentResolver()
            .openInputStream(uri), null, o2);
}

我的错误:

04-01 14:46:54.139: E/AndroidRuntime(11422): FATAL EXCEPTION: main
04-01 14:46:54.139: E/AndroidRuntime(11422): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65537, result=-1, data=Intent { (has extras) }} to activity {fr.allbrary/fr.allbrary.activity.ActivityIdentification}: java.lang.NullPointerException
04-01 14:46:54.139: E/AndroidRuntime(11422):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3406)
04-01 14:46:54.139: E/AndroidRuntime(11422):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3449)
04-01 14:46:54.139: E/AndroidRuntime(11422):    at android.app.ActivityThread.access$1200(ActivityThread.java:150)
04-01 14:46:54.139: E/AndroidRuntime(11422):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1328)
04-01 14:46:54.139: E/AndroidRuntime(11422):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-01 14:46:54.139: E/AndroidRuntime(11422):    at android.os.Looper.loop(Looper.java:176)
04-01 14:46:54.139: E/AndroidRuntime(11422):    at android.app.ActivityThread.main(ActivityThread.java:5279)
04-01 14:46:54.139: E/AndroidRuntime(11422):    at java.lang.reflect.Method.invokeNative(Native Method)
04-01 14:46:54.139: E/AndroidRuntime(11422):    at java.lang.reflect.Method.invoke(Method.java:511)
04-01 14:46:54.139: E/AndroidRuntime(11422):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
04-01 14:46:54.139: E/AndroidRuntime(11422):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
04-01 14:46:54.139: E/AndroidRuntime(11422):    at dalvik.system.NativeStart.main(Native Method)
04-01 14:46:54.139: E/AndroidRuntime(11422): Caused by: java.lang.NullPointerException
04-01 14:46:54.139: E/AndroidRuntime(11422):    at android.content.ContentResolver.openInputStream(ContentResolver.java:436)
04-01 14:46:54.139: E/AndroidRuntime(11422):    at fr.allbrary.fragment.FragmentInscription.decodeUri(FragmentInscription.java:556)
04-01 14:46:54.139: E/AndroidRuntime(11422):    at fr.allbrary.fragment.FragmentInscription.onActivityResult(FragmentInscription.java:530)
04-01 14:46:54.139: E/AndroidRuntime(11422):    at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:166)
04-01 14:46:54.139: E/AndroidRuntime(11422):    at android.app.Activity.dispatchActivityResult(Activity.java:5456)
04-01 14:46:54.139: E/AndroidRuntime(11422):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3402)
04-01 14:46:54.139: E/AndroidRuntime(11422):    ... 11 more

为什么data.getData() == null

1 个答案:

答案 0 :(得分:1)

与Nexus 4和Kitkat一起尝试。如果我选择标准图库,data.getData()对我来说也是空的。对于QuickPic或其他图库,getData()返回正确的uri。

如果getData()为null,请尝试:

     Bundle bundle = data.getExtras();
     Bitmap bitmap = (Bitmap) bundle.get("data");

捆绑中还有另一个带有“cropped-rect”键的条目。