首页 > 解决方案 > 从图库获取图像但大于 3 mb 的图像未在 imageview 中加载

问题描述

小图像已成功加载,但大图像(例如大于 3mb)未加载到图像视图中。我也在从相机捕获图像,它工作正常,但从图库中它没有加载到图像视图中并且它仍然是空的。我不知道如何解决这个问题

 public void gallery() {
    Intent i = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(i, PHOTO_FROM_MEMORY_REQUESTED);
}
 private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    }
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if( requestCode== REQUEST_IMAGE_CAPTURE) {
        try {
            if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
                Bundle extras = data.getExtras();
                Bitmap imageBitmap = (Bitmap) extras.get("data");
                iv.setImageBitmap(imageBitmap);
                image = encode(imageBitmap);//this line is added to encode
            }
        }
        catch (Exception e){ Toast.makeText(DoReport.this, e.toString(), Toast.LENGTH_LONG).show();}
    }
    else if (requestCode == PHOTO_FROM_MEMORY_REQUESTED && resultCode == RESULT_OK) {
        updateSelectedPicture(data.getData());
    }

}
 private void updateSelectedPicture(Uri uri){
    try{
        imageUri = uri;
        InputStream imageStream = getContentResolver().openInputStream(imageUri);
        selectedImage = BitmapFactory.decodeStream(imageStream);
        iv.setImageDrawable(new BitmapDrawable(selectedImage));
        image=encode(selectedImage);

    }catch(FileNotFoundException ex){

        Log.e("File not found", "Cannot find background file under received URI");
    }
}
public static String encode(Bitmap image)
{
    Bitmap immagex=image;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    immagex.compress(Bitmap.CompressFormat.JPEG, 20, baos);
    byte[] b = baos.toByteArray();
    String imageEncoded = Base64.encodeToString(b,Base64.DEFAULT);

    Log.e("LOOK", imageEncoded);
    return imageEncoded;
}

//这里是xml代码

 <ImageView
    android:layout_marginLeft="5dp"

    android:layout_width="350dp"
    android:layout_height="197dp"
    android:maxWidth="100dp"
    android:maxHeight="100dp"
    android:layout_below="@+id/toolbar"
    android:id="@+id/ImgView"
    android:layout_centerHorizontal="true"/>

标签: androidxampp

解决方案


试试这个<application android:largeHeap="true"</application>,我也建议用毕加索来处理图像。


推荐阅读