gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 07:54:20 26 4
gpt4 key购买 nike

我遇到了一个看起来很简单的简单问题,但是,当我从图库中挑选一张图片并尝试在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com