Android startActivityForResult尝试从Gallery Error中获取图像

时间:2016-03-22 08:04:41

标签: android

我正在尝试从图库中获取图片,这是我的代码。

 mProfilePic.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 Intent i = new Intent(
                         Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                 startActivityForResult(i, RESULT_LOAD_IMAGE);//Result_Load_Image =1
             }
         });



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

        if ( resultCode == RESULT_OK && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = {MediaStore.Images.Media.DATA};

            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();

            Bitmap bm =BitmapFactory.decodeFile(picturePath);
            Bitmap resized = Bitmap.createScaledBitmap(bm, 100, 100, true);
            mProfilePic.setImageBitmap(ProfilePicture.addPicture(resized));

        }


    }

这是我的ImageConvertor代码

    public class ProfilePicture {
    public static Bitmap getRoundedRectBitmap() {
        Bitmap result = null;
        try {
            result = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(result);

            int color = 0xff424242;
            Paint paint = new Paint();
            Rect rect = new Rect(0, 0, 200, 200);

            paint.setAntiAlias(true);
            canvas.drawARGB(0, 0, 0, 0);
            paint.setColor(color);
            canvas.drawCircle(50, 50, 50, paint);
            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));




        } catch (NullPointerException e) {
        } catch (OutOfMemoryError o) {
        }
        return result;
    }



      public static Bitmap addPicture(Bitmap bitmap){
          Bitmap result = null;
          try {
              result = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888);
              Canvas canvas = new Canvas(result);

              int color = 0xff424242;
              Paint paint = new Paint();
              Rect rect = new Rect(0, 0, 200, 200);

              paint.setAntiAlias(true);
              canvas.drawARGB(0, 0, 0, 0);
              paint.setColor(color);
              canvas.drawCircle(50, 50, 50, paint);
              paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

          } catch (NullPointerException e) {
          } catch (OutOfMemoryError o) {
          }
          return result;
      }

}

这是我的日志

31696-31696/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: moirai.com.moirai, PID: 31696
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.google.android.apps.photos.contentprovider/0/1/mediakey:/AF1QipNJLOnQ-ATM0T7KBDkqc9PUPn2l7zM4L59fsoMv/ORIGINAL/NONE/1079070800 flg=0x1 (has clip) }} to activity {moirai.com.moirai/moirai.com.moirai.RegisterNow}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference
at android.app.ActivityThread.deliverResults(ActivityThread.java:3607)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3650)
at android.app.ActivityThread.access$1400(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1370)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5294)
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:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference
at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:591)
at moirai.com.moirai.RegisterNow.onActivityResult(RegisterNow.java:312)
at android.app.Activity.dispatchActivityResult(Activity.java:6192)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3603)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3650) 
at android.app.ActivityThread.access$1400(ActivityThread.java:154) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1370) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5294) 
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:904) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699) 

问题是,当我启动应用程序并单击ImageView时,它会启动图库,但是当我选择任何图像时,应用程序会立即停止。有人可以告诉我我的代码有什么问题。 THANKYOU。

2 个答案:

答案 0 :(得分:0)

您的问题是当您在代码中的某处获得位图的宽度时。

的NullPointerException:

引起:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'int android.graphics.Bitmap.getWidth()'

答案 1 :(得分:0)

试试这个代码onActivityresult

Bitmap bitmap = null;

    if (requestCode == GALLERY_CODE && responseCode == RESULT_OK && null != intent) {
        Uri uri = intent.getData();
        mImageCaptureUri = intent.getData();
        System.out.println("Gallery Image URI : "+mImageCaptureUri);
        try {
            bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
            mProfilePic.setImageBitmap(bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }


    }