我需要通过点击按钮从图库中获取图像并将其显示在图像视图中。
我是按照以下方式进行的:
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。它不会加载并显示在图像视图中。
但是宽度和高度较低的图像已成功加载到图像视图中!
有人可以告诉我代码的任何问题以及我做错了什么吗?我只是想从图库中导入相机图像并在图像视图中显示它们!
答案 0 :(得分:61)
你可以试试这个。
将此代码粘贴到按钮点击事件中。
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();
}
}
对你有帮助。
答案 1 :(得分:10)
我使用此代码: 此代码用于启动图库活动。
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, GALLERY_REQUEST);
获得结果:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == Activity.RESULT_OK)
switch (requestCode){
case GALLERY_REQUEST:
Uri selectedImage = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), selectedImage);
carImage.setImageBitmap(bitmap);
} catch (IOException e) {
Log.i("TAG", "Some exception " + e);
}
break;
}
}
并且不要原谅AndroidManifest中的许可。
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
答案 2 :(得分:0)
我尝试了上述所有解决方案,但它们对我不起作用。我从 tutorialspoint 网站上看到了一个代码,它对我很有效,这是代码:
final int PICK_IMAGE = 100;
Button button = findViewById(R.id.button33);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent gallery = new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(gallery, PICK_IMAGE);
}
});
@Override
protected void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
Uri imageUri;
ImageView imageView = findViewById(R.id.image_main_galary);
if (resultCode == RESULT_OK && reqCode == 100){
imageUri = data.getData();
imageView.setImageURI(imageUri);
}
}
最重要的问题:不要忘记权限:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
答案 3 :(得分:0)
因为 OnActivityResult 方法已被弃用,现在使用 AndroidX Activity 的正确方法是 Activity Result API,而推荐的方法是 see docs
"registerForActivityResult() 接受一个 ActivityResultContract 和一个 ActivityResultCallback 并返回一个您将使用的 ActivityResultLauncher启动其他活动。”
ActivityResultLauncher<String> mGetContent = registerForActivityResult(new ActivityResultContracts.GetContent(),
new ActivityResultCallback<Uri>() {
@Override
public void onActivityResult(Uri uri) {
previewImage.setImageURI(uri);
}
});
只需调用
mGetContent.launch("image/*");
需要时。