android - 从图库获取图像但大于 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"/>
解决方案
试试这个<application android:largeHeap="true"</application>
,我也建议用毕加索来处理图像。
推荐阅读
- javascript - 查找js中所有重复记录的索引
- ios - 分隔符在具有自动行高的 UITableView 中生成约束问题
- python - 关于在 keras 中使用 CudnnLSTM 层时出现的错误消息的问题
- arrays - 在数组中搜索用户定义的值
- amazon-web-services - 担任 Localstack 的 IAM 角色
- c# - 如何在c#中方便地表示c数据?
- django - Django 酥脆的表单选项卡无法更改 css
- google-sheets - 用arrayformula和isblank对两列进行减法
- asp.net-core - 指定 Swagger 记录的 API
- c# - PlayerPrefs 在统一编辑器上工作,但不在构建中