-3

I am a beginner to Android and java programming. I have created a simple image slider app of 13(12 images are of 5 MB size and 1 is of 91 Kb) images with previous and next buttons. And when i try to test it out on device, Huawei Honor 5x, the installation screen appears but then disappears instantly. It does not get installed. The logcat shows an outofmemory error. I dont know how to fix it. This is my first app. I have attached the codes.

JAVA Code:

package com.jibran.ejaz.prayertimes;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ViewFlipper;
import android.content.Intent;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    ViewFlipper viewFlipper;
    Button next;
    Button previous;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewFlipper = (ViewFlipper)findViewById(R.id.viewFlipper);
        next = (Button) findViewById(R.id.next);
        previous = (Button) findViewById(R.id.previous);
        next.setOnClickListener(this);
        previous.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        if (v == next) {
            viewFlipper.showNext();
        }
        else if (v == previous) {
            viewFlipper.showPrevious();
        }
    }
    }

XML Code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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"
    tools:context="com.jibran.ejaz.prayertimes.MainActivity">
    <ViewFlipper
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/viewFlipper">
        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:scaleType="fitXY"
            android:id="@+id/imageView1"
            android:src="@drawable/prayertimes"/>
        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:scaleType="fitXY"
            android:id="@+id/imageView2"
            android:src="@drawable/capture1"/>
        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:scaleType="fitXY"
            android:id="@+id/imageView3"
            android:src="@drawable/capture2"/>
        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:scaleType="fitXY"
            android:id="@+id/imageView4"
            android:src="@drawable/capture3"/>
        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:scaleType="fitXY"
            android:id="@+id/imageView5"
            android:src="@drawable/capture4"/>
        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:scaleType="fitXY"
            android:id="@+id/imageView6"
            android:src="@drawable/capture5"/>
        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:scaleType="fitXY"
            android:id="@+id/imageView7"
            android:src="@drawable/capture6"/>
        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:scaleType="fitXY"
            android:id="@+id/imageView8"
            android:src="@drawable/capture7"/>
        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:scaleType="fitXY"
            android:id="@+id/imageView9"
            android:src="@drawable/capture8"/>
        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:scaleType="fitXY"
            android:id="@+id/imageView10"
            android:src="@drawable/capture9"/>
        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:scaleType="fitXY"
            android:id="@+id/imageView11"
            android:src="@drawable/capture10"/>
        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:scaleType="fitXY"
            android:id="@+id/imageView12"
            android:src="@drawable/capture11"/>
        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:scaleType="fitXY"
            android:id="@+id/imageView13"
            android:src="@drawable/capture12"/>
    </ViewFlipper>
    <Button
        android:id="@+id/next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Next"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"/>
    <Button
        android:id="@+id/previous"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Prev"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentBottom="true"
        android:layout_alignTop="@+id/next"/>
</RelativeLayout>

logcat:

02-17 09:49:52.791 954-954/com.jibran.ejaz.prayertimes V/HwPolicyFactory: : success to get AllImpl object and return....
02-17 09:49:52.801 954-954/com.jibran.ejaz.prayertimes V/HwWidgetFactory: : successes to get AllImpl object and return....
02-17 09:49:52.801 954-954/com.jibran.ejaz.prayertimes V/ActivityThread: ActivityThread,callActivityOnCreate
02-17 09:49:52.881 954-954/com.jibran.ejaz.prayertimes W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
02-17 09:49:52.991 954-954/com.jibran.ejaz.prayertimes D/CubicBezierInterpolator: CubicBezierInterpolator  mControlPoint1x = 0.15, mControlPoint1y = 0.7, mControlPoint2x = 0.2, mControlPoint2y = 0.98
02-17 09:49:52.991 954-954/com.jibran.ejaz.prayertimes D/CubicBezierInterpolator: CubicBezierInterpolator  mControlPoint1x = 0.6, mControlPoint1y = 0.9, mControlPoint2x = 0.8, mControlPoint2y = 1.0
02-17 09:49:53.001 954-954/com.jibran.ejaz.prayertimes V/BoostFramework: mAcquireFunc method = public int com.qualcomm.qti.Performance.perfLockAcquire(int,int[])
02-17 09:49:53.001 954-954/com.jibran.ejaz.prayertimes V/BoostFramework: mReleaseFunc method = public int com.qualcomm.qti.Performance.perfLockRelease()
02-17 09:49:53.001 954-954/com.jibran.ejaz.prayertimes V/BoostFramework: mAcquireTouchFunc method = public int com.qualcomm.qti.Performance.perfLockAcquireTouch(android.view.MotionEvent,android.util.DisplayMetrics,int,int[])
02-17 09:49:53.001 954-954/com.jibran.ejaz.prayertimes V/BoostFramework: mIOPStart method = public int com.qualcomm.qti.Performance.perfIOPrefetchStart(int,java.lang.String)
02-17 09:49:53.001 954-954/com.jibran.ejaz.prayertimes V/BoostFramework: mIOPStop method = public int com.qualcomm.qti.Performance.perfIOPrefetchStop()
02-17 09:49:53.001 954-954/com.jibran.ejaz.prayertimes V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@47c49a4
02-17 09:49:53.001 954-954/com.jibran.ejaz.prayertimes D/CubicBezierInterpolator: CubicBezierInterpolator  mControlPoint1x = 0.15, mControlPoint1y = 0.7, mControlPoint2x = 0.2, mControlPoint2y = 0.98
02-17 09:49:53.001 954-954/com.jibran.ejaz.prayertimes D/CubicBezierInterpolator: CubicBezierInterpolator  mControlPoint1x = 0.6, mControlPoint1y = 0.9, mControlPoint2x = 0.8, mControlPoint2y = 1.0
02-17 09:49:53.001 954-954/com.jibran.ejaz.prayertimes V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@33830d
02-17 09:49:53.071 954-954/com.jibran.ejaz.prayertimes I/HwCust: Constructor found for class android.widget.HwCustTextViewImpl
02-17 09:49:53.071 954-954/com.jibran.ejaz.prayertimes D/HwCust: Create obj success use class android.widget.HwCustTextViewImpl
02-17 09:49:53.571 954-954/com.jibran.ejaz.prayertimes I/art: Starting a blocking GC Alloc
02-17 09:49:53.571 954-954/com.jibran.ejaz.prayertimes I/art: Starting a blocking GC Alloc
02-17 09:49:53.581 954-954/com.jibran.ejaz.prayertimes I/art: Alloc partial concurrent mark sweep GC freed 337(27KB) AllocSpace objects, 0(0B) LOS objects, 40% free, 18MB/31MB, paused 253us total 10.199ms
02-17 09:49:53.581 954-954/com.jibran.ejaz.prayertimes I/art: Starting a blocking GC Alloc
02-17 09:49:53.591 954-954/com.jibran.ejaz.prayertimes I/art: Starting a blocking GC Alloc
02-17 09:49:53.601 954-954/com.jibran.ejaz.prayertimes I/art: Alloc concurrent mark sweep GC freed 20(12KB) AllocSpace objects, 0(0B) LOS objects, 40% free, 18MB/31MB, paused 312us total 15.129ms
02-17 09:49:53.601 954-954/com.jibran.ejaz.prayertimes I/art: Forcing collection of SoftReferences for 445MB allocation
02-17 09:49:53.601 954-954/com.jibran.ejaz.prayertimes I/art: Starting a blocking GC Alloc
02-17 09:49:53.621 954-954/com.jibran.ejaz.prayertimes I/art: Alloc concurrent mark sweep GC freed 11(352B) AllocSpace objects, 0(0B) LOS objects, 40% free, 18MB/31MB, paused 286us total 14.715ms
02-17 09:49:53.621 954-954/com.jibran.ejaz.prayertimes W/art: Throwing OutOfMemoryError "Failed to allocate a 467251212 byte allocation with 13040548 free bytes and 173MB until OOM"
02-17 09:49:53.621 954-954/com.jibran.ejaz.prayertimes I/art: Starting a blocking GC Alloc
02-17 09:49:53.621 954-954/com.jibran.ejaz.prayertimes I/art: Starting a blocking GC Alloc
02-17 09:49:53.631 954-954/com.jibran.ejaz.prayertimes I/art: Starting a blocking GC Alloc
02-17 09:49:53.631 954-954/com.jibran.ejaz.prayertimes I/art: Alloc partial concurrent mark sweep GC freed 6(192B) AllocSpace objects, 0(0B) LOS objects, 40% free, 18MB/31MB, paused 249us total 7.162ms
02-17 09:49:53.631 954-954/com.jibran.ejaz.prayertimes I/art: Starting a blocking GC Alloc
02-17 09:49:53.651 954-954/com.jibran.ejaz.prayertimes I/art: Alloc concurrent mark sweep GC freed 3(96B) AllocSpace objects, 0(0B) LOS objects, 40% free, 18MB/31MB, paused 278us total 14.701ms
02-17 09:49:53.651 954-954/com.jibran.ejaz.prayertimes I/art: Forcing collection of SoftReferences for 445MB allocation
02-17 09:49:53.651 954-954/com.jibran.ejaz.prayertimes I/art: Starting a blocking GC Alloc
02-17 09:49:53.661 954-954/com.jibran.ejaz.prayertimes I/art: Alloc concurrent mark sweep GC freed 3(96B) AllocSpace objects, 0(0B) LOS objects, 40% free, 18MB/31MB, paused 266us total 14.733ms
02-17 09:49:53.661 954-954/com.jibran.ejaz.prayertimes W/art: Throwing OutOfMemoryError "Failed to allocate a 467251212 byte allocation with 13040548 free bytes and 173MB until OOM"
02-17 09:49:53.661 954-954/com.jibran.ejaz.prayertimes D/skia: --- allocation failed for scaled bitmap
02-17 09:49:53.681 954-954/com.jibran.ejaz.prayertimes D/AndroidRuntime: Shutting down VM
02-17 09:49:53.731 954-954/com.jibran.ejaz.prayertimes E/AndroidRuntime: FATAL EXCEPTION: main
                                                                         Process: com.jibran.ejaz.prayertimes, PID: 954
                                                                         java.lang.OutOfMemoryError: Failed to allocate a 467251212 byte allocation with 13040548 free bytes and 173MB until OOM
                                                                             at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
                                                                             at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
                                                                             at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:667)
                                                                             at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:488)
                                                                             at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1082)
                                                                             at android.content.res.Resources.loadDrawableForCookie(Resources.java:2702)
                                                                             at android.content.res.Resources.loadDrawable(Resources.java:2603)
                                                                             at android.content.res.HwResources.loadDrawable(HwResources.java:665)
                                                                             at android.content.res.TypedArray.getDrawable(TypedArray.java:912)
                                                                             at android.widget.ImageView.<init>(ImageView.java:152)
                                                                             at android.widget.ImageView.<init>(ImageView.java:140)
                                                                             at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:57)
                                                                             at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:53)
                                                                             at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)
                                                                             at android.support.v7.app.AppCompatDelegateImplV7.createView(AppCompatDelegateImplV7.java:1008)
                                                                             at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:1067)
                                                                             at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
                                                                             at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:765)
                                                                             at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:723)
                                                                             at android.view.LayoutInflater.rInflate(LayoutInflater.java:854)
                                                                             at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:817)
                                                                             at android.view.LayoutInflater.rInflate(LayoutInflater.java:857)
                                                                             at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:817)
                                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:534)
                                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:434)
                                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
                                                                             at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
                                                                             at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
                                                                             at com.jibran.ejaz.prayertimes.MainActivity.onCreate(MainActivity.java:17)
                                                                             at android.app.Activity.performCreate(Activity.java:6367)
                                                                             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2397)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2504)
                                                                             at android.app.ActivityThread.access$900(ActivityThread.java:165)
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1368)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                             at android.os.Looper.loop(Looper.java:150)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5546)
                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684)
02-17 09:49:54.041 954-954/com.jibran.ejaz.prayertimes I/Process: Sending signal. PID: 954 SIG: 9

3 Answers3

0

swich to picasso liabrary its easy to implement

compile "com.squareup.picasso:picasso:2.4.0" use this in gradle files .

 Picasso.with(this)
.load("YOUR IMAGE URL HERE")
.placeholder(Your Drawable Resource) //this is optional the image to display       while the url image is downloading
.error(Your Drawable Resource)         //this is also optional if some error has occurred in downloading the image this image would be displayed
.into(imageView);
android_jain
  • 788
  • 8
  • 19
0

Because of too large image source and a lot of ImageView on View. so you have to choose decreasing image size and weight or use third-party library.

So I recommend you use third-party library to manage its size for you like Glide:

https://github.com/bumptech/glide

Paste in build.gradle app-level

repositories {
  mavenCentral() // jcenter() works as well because it pulls from Maven Central
}

dependencies {
  compile 'com.github.bumptech.glide:glide:3.7.0'
  compile 'com.android.support:support-v4:19.1.0'
}

And load image in runtime:

Glide.with(this).load(R.drawable.prayertimes).into(imageView1);
...

EDIT

Devoters should tell your reasons.

Harry T.
  • 3,478
  • 2
  • 21
  • 41
-1

Scale the images down using BitmapFactory.Options http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html

OutOfMemoryError is the most common problem occur in android while especially dealing with bitmaps. This error is thrown by the Java Virtual Machine (JVM) when an object cannot be allocated due to lack of memory space and also, the garbage collector cannot free some space.

int imageWidth, imageHeight;

Bitmap result = Bitmap.createScaledBitmap(bitmapPicture,
                        imageWidth, imageHeight, false);
Akshay
  • 1,161
  • 1
  • 12
  • 33
  • Try something like this: – Akshay Feb 17 '17 at 05:06
  • I couldn't understand that how to use the above mentioned function. So i tried compressing the images using an online compressor (57 MB to 1.99MB) but still it doesn't solve my problem. Can you please edit my code using the solution you have mentioned? – Jibran Ejaz Feb 17 '17 at 06:46