我需要通过按钮单击从库中获取图像并将其显示在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());
}
}
活动结果方法是
@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.它不会加载并显示在图像视图中.
但是宽度和高度较低的图像已成功加载到图像视图中!
有人可以告诉我代码的任何问题以及我做错了什么吗?我只想从图库中导入相机图像并在图像视图中显示它们!
解决方法:
你可以试试这个.
将此代码粘贴到按钮单击事件中.
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, RESULT_LOAD_IMG);
以下代码是您的活动结果
@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();
}
}
它对你有所帮助.