[英]Can't set image from gallery or camera in dialogFragment android
[英]Why can't I get an image from the camera album in Android gallery?
我可以从图库中获取图像,但不能从相册/胶卷中获取图像。 但是我知道我从相机胶卷中获得了图像的URI,但它仍然不会显示在我的imageview中。
打开画廊:
if(resultCode == RESULT_OK){
imgUri = data.getData();
if(requestCode == PICK_IMAGE){
Intent sendpic = new Intent(getApplicationContext(), GalleryChoice.class);
sendpic.putExtra("imagePath", imgUri.toString());
Log.d("SHOW UP", imgUri.toString());
startActivity(sendpic);
从上一个活动中获取URI以显示在未显示图像的ImageView中。
if(getIntent().hasExtra("imagePath")){
ur = getIntent().getStringExtra("imagePath");
Uri newUri = Uri.parse(ur);
try {
bitmapy = MediaStore.Images.Media.getBitmap(this.getContentResolver(), newUri);
imgView1.setImageBitmap(bitmapy);
}
catch (IOException e){
}
}
通常,当Image
大小为2048*2048
时,会发生这种情况。 通常,相机胶卷会捕获尺寸较大的图像,如果图像的长度和宽度大于2048
,则需要裁切。
ImageView < 2048*2048
上设置的有效尺寸 下面仅是裁剪图像的示例(如果尺寸过大且不考虑图像比例)
public class InputImageCompressor extends Activity{
public static void compressInputImage(Uri inputImageData, Context context, ImageView newIV)
{
Bitmap bitmap;
//Uri inputImageData = data.getData();
try
{
Bitmap bitmapInputImage = MediaStore.Images.Media.getBitmap(context.getContentResolver(), inputImageData);
if (bitmapInputImage.getWidth() > 2048 && bitmapInputImage.getHeight() > 2048)
{
bitmap = Bitmap.createScaledBitmap(bitmapInputImage, 1024, 1280, true);
newIV.setImageBitmap(bitmap);
}
else if (bitmapInputImage.getWidth() > 2048 && bitmapInputImage.getHeight() < 2048)
{
bitmap = Bitmap.createScaledBitmap(bitmapInputImage, 1920, 1200, true);
newIV.setImageBitmap(bitmap);
}
else if (bitmapInputImage.getWidth() < 2048 && bitmapInputImage.getHeight() > 2048)
{
bitmap = Bitmap.createScaledBitmap(bitmapInputImage, 1024, 1280, true);
newIV.setImageBitmap(bitmap);
}
else if (bitmapInputImage.getWidth() < 2048 && bitmapInputImage.getHeight() < 2048)
{
bitmap = Bitmap.createScaledBitmap(bitmapInputImage, bitmapInputImage.getWidth(), bitmapInputImage.getHeight(), true);
newIV.setImageBitmap(bitmap);
}
} catch (Exception e)
{
Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show();
}
}
}
在您的代码中使用它:
if(getIntent().hasExtra("imagePath")){
ur = getIntent().getStringExtra("imagePath");
Uri newUri = Uri.parse(ur);
try {
InputImageCompressor.compressInputImage(newUri, your_class_name.this, imgView1);
}
catch (IOException e){
}
}
为什么图像尺寸限制为2048 * 2048 阅读更多
检查Uri
是否有效。 如果是这样,请使用下面的方法获取它的真实地址。
public String getRealPathFromURI(Uri uri){
String filePath = "";
String[] filePahColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(uri, filePahColumn, null, null, null);
if (cursor != null) {
if(cursor.moveToFirst()){
int columnIndex = cursor.getColumnIndex(filePahColumn[0]);
filePath = cursor.getString(columnIndex);
}
cursor.close();
}
return filePath;
}
使用此文件filePath
设置图像
yourImageView.setImageBitmap(BitmapFactory.decodeFile(filePath));
您可以使用它来缩小图像。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.