无法使用Intent.ACTION_PICK从库中获取位图

时间:2015-05-16 09:54:23

标签: android bitmap onactivityresult

我有一个看似简单的简单问题,但是,当我从图库中选择一个图像并尝试将其设置在onActivityResult中的imageview中时,错误会显示出来。

 java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://media/external/images/media/35 }} to activity {com.mypackagename/com.mypackagename.SQLiteActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

此错误必须归因于null对象。这意味着没有数据被重新检索?我觉得我所做的设置没有问题,但是,我可能错过了一些东西。我在下面设置了示例代码。

这是调用图库的代码

public void settingImage(View v){
    Intent i = new Intent(
            Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

    startActivityForResult(i, RESULT_LOAD_IMAGE);
}

此代码用于获取结果

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

    if (data != null) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };
        Cursor cursor = getApplicationContext().getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);

        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        // Log.d("Path", picturePath);
        //sampleimage.setImageBitmap(BitmapFactory.decodeFile(picturePath));
        // imgPath = picturePath;
        icontext.setText(picturePath);
        cursor.close();
    } else {

    }
}

这里有一些设置

private static int RESULT_LOAD_IMAGE = 1;

基本的sdk设置

minSdkVersion 14
targetSdkVersion 22

1 个答案:

答案 0 :(得分:4)

我试过这个来获取Bitmap,

调用图库选取器的代码

  Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
  photoPickerIntent.setType("image/*");
  startActivityForResult(photoPickerIntent, SELECT_PHOTO);

获取位图的结果,

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

    switch (requestCode) {
        case SELECT_PHOTO:
            if (resultCode == RESULT_OK) {
                Uri selectedImage = imageReturnedIntent.getData();                

                //yourSelectedImage = BitmapFactory.decodeStream(imageStream);
                try {
                    yourSelectedImage = decodeUri(selectedImage);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            //    imgViewProfilePic.setImageBitmap(yourSelectedImage);                  
            }
    }
}

解码uri,

private Bitmap decodeUri(Uri selectedImage) throws FileNotFoundException {

    // Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o);

    // The new size we want to scale to
    final int REQUIRED_SIZE = 140;

    // Find the correct scale value. It should be the power of 2.
    int width_tmp = o.outWidth, height_tmp = o.outHeight;
    int scale = 1;
    while (true) {
        if (width_tmp / 2 < REQUIRED_SIZE
                || height_tmp / 2 < REQUIRED_SIZE) {
            break;
        }
        width_tmp /= 2;
        height_tmp /= 2;
        scale *= 2;
    }

    // Decode with inSampleSize
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;
    return BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o2);

}