Reputation: 1632
I want to show an Image in an ImageView
.
I do it this way:
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);
The problem is that if I start the application in landscape and rotate the device afterwards, the application crashes.
Xml code:
<?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>
The next problem is that the picture isn't shown in fullscreen.
Do you have any suggestions to solve these issues?
LOGCAT WHEN ROTATING THE DEVICE
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)
Upvotes: 0
Views: 508
Reputation: 2819
Resize bitmap(Take care you don't lose too much quality. Change the value of sample size according to need)
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
Bitmap bitmap = BitmapFactory.decodeStream(stream, null, options);
and put android:allowBackup="true" in your manifest.
<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">
for full screen image use:
android:scaleType="fitXY"
Upvotes: 1
Reputation: 532
To avoid out of memory issue, Refer the below link for scaling the bitmap https://developer.android.com/training/displaying-bitmaps/load-bitmap.html.
To display the image in full screen add " android:scaleType="fitXY" " in your xml file inside image tag.
Upvotes: 1
Reputation: 461
Every time you rotate the device then O.S. recreates the activity and all it's objects are created/ initialized. Suppose you started the application and your device is in portrait mode, Your bitmap is loaded and processed at memory. When you rotate the device, the bitmap is again decoded to memory from your storage.
Solution :- 1. Keep the bitmap reference global. 2. At savedInstanceState() or onCreate() check for bitmap is processed or not. If processed then need not to process it again.
Example.
public void onCreate(Bundle savedInstanceState) {
if(null != savedInstanceState && null != bitmap) {
// Bitmap is processed already.
}
else {
// Bitmap is not processed. Need to process it.
}
}
Or Check the same at onSaveInstanceState() method.
Upvotes: 0