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

标签 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

此错误肯定是由于空对象造成的。这是否意味着没有检索到任何数据?我认为我所做的设置没有问题,但是,我可能遗漏了一些东西。我在下面设置了示例代码。

这是调用图库的代码

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

最佳答案

我尝试过这个来获取位图,

调用图库选择器的代码,

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

}

关于android - 无法使用 Intent.ACTION_PICK 从库中获取位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30274199/

相关文章:

android - 在 Intent 中覆盖 OnActivityResult 不起作用 - kotlin

Android:确定应用程序在哪里耗尽电池电量?

javascript - 创建 Android NativeScript 插件

java - 如何多次使用同一个 fragment ?

android - 如何在 Activity 类之外实现 onActivityResult

Android Fragment 生命周期问题(onActivityResult 上的 NullPointerException)

android - ListView 中的 ViewPager 和 OnItemClickListener

java - 创建纹理图集错误(opengl ES 2.0,android)

Android 上传前调整图像大小(重新采样/缩小采样)

c# - 使用字节数组检查图像大小