gpt4 book ai didi

java - SetImageBitMap 在 api 23 android java 中不起作用

转载 作者:行者123 更新时间:2023-12-01 09:57:01 25 4
gpt4 key购买 nike

我正在尝试使用位图将照片从图库上传到我的应用程序,在我的模拟器(api 22)中它可以工作,但由于某种原因,在我的手机(api 23)中它不起作用,图片不会仅显示空白页面。如果重要的话,这是我的代码:

public class Photoactivity extends Activity {

private static final int SELECTED_PICTURE=1;
ImageView iv;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_photoactivity);

iv=(ImageView)findViewById(R.id.ImgView);
}
public void btnClick(View v){
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, SELECTED_PICTURE);

}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);

switch (requestCode) {
case SELECTED_PICTURE:
if(resultCode==RESULT_OK){
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
ImageView imageView = (ImageView) findViewById(R.id.ImgView);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

}
break;

default:
break;
}

}

}

最佳答案

替换这个:

    case SELECTED_PICTURE:
if(resultCode==RESULT_OK){
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
ImageView imageView = (ImageView) findViewById(R.id.ImgView);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

}
break;

这样:

    case SELECTED_PICTURE:
if(resultCode==RESULT_OK){
InputStream is=getContentResolver().openInputStream(data.getData());
imageView.setImageBitmap(BitmapFactory.decodeStream(is));
}
break;

更好的是,切换到使用 an image-loading library ,如Picasso ,因此此 I/O 可以在后台线程上完成,因此您的 UI 在加载图像时不会卡住。

关于java - SetImageBitMap 在 api 23 android java 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37124856/

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