我想在ImageView
中显示图片。
我是这样做的:
File externalDirectory = Environment.getExternalStorageDirectory();
File directory = new File (externalDirectory.getAbsolutePath());
File file = new File(directory, "pic.jpg"); //or any other format supported
FileInputStream streamIn = null;
try {
streamIn = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
bitmap = BitmapFactory.decodeStream(streamIn); //This gets the image
try {
streamIn.close();
} catch (IOException e) {
e.printStackTrace();
}
imageView = (ImageView) findViewById(R.id.image_view);
imageView.setImageBitmap(bitmap);
问题在于,如果我在横向启动应用程序并随后旋转设备,应用程序将崩溃。
Xml代码:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/image_view"/>
</FrameLayout>
下一个问题是图片没有全屏显示 您有什么建议可以解决这些问题吗?
旋转设备时的LOGCAT
01-03 11:23:45.810 25429-25429/com.kamera E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.kamera, PID: 25429
java.lang.OutOfMemoryError: Failed to allocate a 80683020 byte allocation with 16765280 free bytes and 63MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
at android.graphics.BitmapFactory.decodeStreamInternal(BitmapFactory.java:635)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:611)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:649)
at com.kamera.PhotoActivity.loadImageFromExternalStorage(PhotoActivity.java:47)
at com.kamera.PhotoActivity.onCreate(PhotoActivity.java:29)
at android.app.Activity.performCreate(Activity.java:6248)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1125)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2437)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2544)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4398)
at android.app.ActivityThread.access$1000(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1400)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:168)
at android.app.ActivityThread.main(ActivityThread.java:5845)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
答案 0 :(得分:1)
调整位图大小(注意不要丢失太多质量。根据需要更改样本大小的值)
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
Bitmap bitmap = BitmapFactory.decodeStream(stream, null, options);
并在你的清单中加上 android:allowBackup =&#34; true&#34; 。
<application
android:allowBackup="true"
android:hardwareAccelerated="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:supportsRtl="true"
android:theme="@style/AppTheme">
用于全屏图像使用:
机器人:scaleType =&#34; fitXY&#34;
答案 1 :(得分:1)
为避免内存不足问题,请参阅以下链接以缩放位图 https://developer.android.com/training/displaying-bitmaps/load-bitmap.html
要全屏显示图像,请在图片标记内的xml文件中添加“android:scaleType =”fitXY“”。
答案 2 :(得分:0)
每次旋转设备,然后O.S.重新创建活动,并创建/初始化所有对象。假设您启动了应用程序并且您的设备处于纵向模式,您的位图将在内存中加载和处理。旋转设备时,位图将再次从存储中解码为内存。
解决方案: - 1.使位图参考保持全局。 2.在savedInstanceState()或onCreate()中检查是否处理了位图。如果已处理,则无需再次处理。
实施例。
public void onCreate(Bundle savedInstanceState) {
if(null != savedInstanceState && null != bitmap) {
// Bitmap is processed already.
}
else {
// Bitmap is not processed. Need to process it.
}
}
或者在onSaveInstanceState()方法中检查相同内容。