1

在应用程序中,我打开图库并选择图像我必须在对话框中显示该图像..但有时它会返回位图值 =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 画廊,我使用了下面的代码

 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();
                    }

                  }
                 }

请帮助..

4

1 回答 1

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
    }
于 2012-04-16T05:17:33.153 回答