要从应用程序库中选择图像,有时它会返回位图 - 在Android中为null?

时间:2012-04-16 04:46:28

标签: android bitmap

在应用程序中我打开图库并选择图像我必须在对话框中显示该图像.. 有时它会returen位图值= null ..并且有时它的位图值没有显示对话框..它通过例外。

 java.lang.NullPointerException
04-16 10:11:52.310: WARN/System.err(1395):     at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:432)
04-16 10:11:52.310: WARN/System.err(1395):     at myclassname.onActivityResult

对于iopen gallery我使用下面的代码

 Intent intent = new Intent();
         intent.setType("image/*");
         intent.setAction(Intent.ACTION_GET_CONTENT);
         ((Activity) context).startActivityForResult(Intent.createChooser(intent,"Select Picture"), PICK_FROM_FILE);

敌人从图库中获取数据我使用此代码

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

                  if(resultCode == Activity.RESULT_OK)
                  { 
                      Uri selectedImage = data.getData();
                       try {

 Bitmap bitmap =   BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage));

                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                  }
                 }

Plz帮助..

1 个答案:

答案 0 :(得分:0)

在你onActivityResult中更改代码以将图像接收到此代码,

    if (resultCode == RESULT_OK && requestCode == PICK_FROM_FILE) {

        Uri contentUri = data.getData();
        String[] proj = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(contentUri, proj, null, null, null);
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        String tmppath = cursor.getString(column_index); // get path of image
        Bitmap mBitmap = BitmapFactory.decodeFile(tmppath); // decode path into bitmap
        iv.setImageBitmap(mBitmap); // set image to imageview
    }