gpt4 book ai didi

android - 从图库中获取图像并在 ImageView 中显示

转载 作者:IT老高 更新时间:2023-10-28 22:19:20 25 4
gpt4 key购买 nike

我需要通过单击按钮从图库中获取图像并将其显示到 ImageView 中。

我的做法如下:

    btn_image_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

getImageFromAlbum();
}
});

方法定义如下:

   private void getImageFromAlbum(){
try{
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}catch(Exception exp){
Log.i("Error",exp.toString());
}
}

Activity 结果方法是

  @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
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();

try {
bmp = getBitmapFromUri(selectedImage);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

image_view.setImageBitmap(bmp);

//to know about the selected image width and height
Toast.makeText(MainActivity.this, image_view.getDrawable().getIntrinsicWidth()+" & "+image_view.getDrawable().getIntrinsicHeight(), Toast.LENGTH_SHORT).show();
}

}

问题

我面临的问题是当图像分辨率很高时,假设图像大小为 5mp 到 13mp。它不会加载并显示在 ImageView 中。

但宽度和高度较低的图像已成功加载到 ImageView 中!

谁能告诉我代码的任何问题以及我做错了什么?我只想从图库中导入相机图像并在 ImageView 中显示它们!

最佳答案

你可以试试这个。

将此代码粘贴到您的按钮单击事件中。

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

下面的代码是你的 Activity 结果

@Override
protected void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);


if (resultCode == RESULT_OK) {
try {
final Uri imageUri = data.getData();
final InputStream imageStream = getContentResolver().openInputStream(imageUri);
final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
image_view.setImageBitmap(selectedImage);
} catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(PostImage.this, "Something went wrong", Toast.LENGTH_LONG).show();
}

}else {
Toast.makeText(PostImage.this, "You haven't picked Image",Toast.LENGTH_LONG).show();
}
}

希望对你有帮助。

关于android - 从图库中获取图像并在 ImageView 中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38352148/

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