gpt4 book ai didi

java.lang.OutOfMemory 错误 - SD 卡图像导入 ImageView

转载 作者:行者123 更新时间:2023-12-01 11:15:54 25 4
gpt4 key购买 nike

我试图将 SD 卡图像显示到 ImageView 中并收到 OutOfMemory 错误。

请告诉我如何将 SDCard 的图像显示到 ImageView 中(无论它们有多大。)

我知道我们可以使用这些属性来控制它,但我正在寻找最好的解决方案:

    android:hardwareAccelerated="false"
android:largeHeap="true"

Logcat:

08-05 11:27:34.530: E/AndroidRuntime(8158): FATAL EXCEPTION: main
08-05 11:27:34.530: E/AndroidRuntime(8158): java.lang.OutOfMemoryError
08-05 11:27:34.530: E/AndroidRuntime(8158): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
08-05 11:27:34.530: E/AndroidRuntime(8158): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:650)
08-05 11:27:34.530: E/AndroidRuntime(8158): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:389)
08-05 11:27:34.530: E/AndroidRuntime(8158): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:449)
08-05 11:27:34.530: E/AndroidRuntime(8158): at com.physic.UploadActivity.previewMedia(UploadActivity.java:171)
08-05 11:27:34.530: E/AndroidRuntime(8158): at com.physic.UploadActivity.onCreate(UploadActivity.java:109)
08-05 11:27:34.530: E/AndroidRuntime(8158): at android.app.Activity.performCreate(Activity.java:5191)
08-05 11:27:34.530: E/AndroidRuntime(8158): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
08-05 11:27:34.530: E/AndroidRuntime(8158): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
08-05 11:27:34.530: E/AndroidRuntime(8158): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
08-05 11:27:34.530: E/AndroidRuntime(8158): at android.app.ActivityThread.access$700(ActivityThread.java:140)
08-05 11:27:34.530: E/AndroidRuntime(8158): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
08-05 11:27:34.530: E/AndroidRuntime(8158): at android.os.Handler.dispatchMessage(Handler.java:99)
08-05 11:27:34.530: E/AndroidRuntime(8158): at android.os.Looper.loop(Looper.java:137)
08-05 11:27:34.530: E/AndroidRuntime(8158): at android.app.ActivityThread.main(ActivityThread.java:4921)
08-05 11:27:34.530: E/AndroidRuntime(8158): at java.lang.reflect.Method.invokeNative(Native Method)
08-05 11:27:34.530: E/AndroidRuntime(8158): at java.lang.reflect.Method.invoke(Method.java:511)
08-05 11:27:34.530: E/AndroidRuntime(8158): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1036)
08-05 11:27:34.530: E/AndroidRuntime(8158): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:803)
08-05 11:27:34.530: E/AndroidRuntime(8158): at dalvik.system.NativeStart.main(Native Method)

代码:

/**
* Displaying captured image/video on the screen
* */
private void previewMedia(boolean isImage) {
// Checking whether captured media is image or video
if (isImage) {
imgPreview.setVisibility(View.VISIBLE);

final Bitmap bitmap = BitmapFactory.decodeFile(filePath);

ExifInterface exif = null;
try {
exif = new ExifInterface(filePath);

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED);

Bitmap bmRotated = rotateBitmap(bitmap, orientation);
imgPreview.setImageBitmap(bmRotated);

} else {
imgPreview.setVisibility(View.GONE);
}

}

使用 picasso 库:

        Picasso.with(UploadActivity.this)
.load(new File(filePath)).rotate(90)
.fit().centerInside()
.error(R.drawable.ic_launcher)
.placeholder(R.drawable.ic_launcher)
.into(imgPreview);

甚至尝试过这个:

        .transform(transformation)

日志:

Log.d("file:", filePath);
D/file:(6201): /storage/sdcard0/Pictures/Testing/LICPolicy20150805120029.jpg

尝试使用 Picasso lib 但未显示图像预览..

最佳答案

当您的图像太大并且您的设备无法渲染它时,会发生OutOfMemoryException,因此您可以根据需要缩小图像,下面是缩小尺寸的代码

 public static Bitmap compressed_Bitmap(Bitmap bitmap) {
final int maxSize = 700;
int outWidth;
int outHeight;
int inWidth = bitmap.getWidth();
int inHeight = bitmap.getHeight();
if (inWidth > inHeight) {
outWidth = maxSize;
outHeight = (inHeight * maxSize) / inWidth;
} else {
outHeight = maxSize;
outWidth = (inWidth * maxSize) / inHeight;
}

Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitmap, outWidth, outHeight, false);
return resizedBitmap;
}

关于java.lang.OutOfMemory 错误 - SD 卡图像导入 ImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31824672/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com