sarath
sarath

Reputation: 45

Android Image View?

i am trying to run an activity when the user clicks a button and the activity shows images depending on the ringer volume .when i execute it for some reason it dose not work.any help is appreciated .

private void show() {
    AudioManager am=(AudioManager)this.getSystemService(AUDIO_SERVICE);
    mode=am.getRingerMode();
    switch(mode){
    case AudioManager.RINGER_MODE_NORMAL:
    try{

        tv.setBackground(this.getResources().getDrawable(R.drawable.a1));
        Thread.sleep(1000);

        tv.setBackground(this.getResources().getDrawable(R.drawable.a2));
        Thread.sleep(1000);

        tv.setBackground(this.getResources().getDrawable(R.drawable.a3));
        Thread.sleep(1000);

        tv.setBackground(this.getResources().getDrawable(R.drawable.a31));
        Thread.sleep(1000);

        tv.setBackground(this.getResources().getDrawable(R.drawable.a4));
        Thread.sleep(1000);

        tv.setBackground(this.getResources().getDrawable(R.drawable.a5));
        Thread.sleep(1000);

        tv.setBackground(this.getResources().getDrawable(R.drawable.a6));
        Thread.sleep(1000);

        tv.setBackground(this.getResources().getDrawable(R.drawable.a7));
        Thread.sleep(1000);

    } catch (InterruptedException e) {
            e.printStackTrace();
        }

    break;
    case AudioManager.RINGER_MODE_SILENT:
    case AudioManager.RINGER_MODE_VIBRATE:

        try{

            tv.setBackground(this.getResources().getDrawable(R.drawable.a7));
            Thread.sleep(1000);

            tv.setBackground(this.getResources().getDrawable(R.drawable.a6));
            Thread.sleep(1000);

            tv.setBackground(this.getResources().getDrawable(R.drawable.a5));
            Thread.sleep(1000);

            tv.setBackground(this.getResources().getDrawable(R.drawable.a4));
            Thread.sleep(1000);

            tv.setBackground(this.getResources().getDrawable(R.drawable.a31));
            Thread.sleep(1000);

            tv.setBackground(this.getResources().getDrawable(R.drawable.a3));
            Thread.sleep(1000);

            tv.setBackground(this.getResources().getDrawable(R.drawable.a2));
            Thread.sleep(1000);

            tv.setBackground(this.getResources().getDrawable(R.drawable.a1));
            Thread.sleep(1000);

        } catch (InterruptedException e) {
                e.printStackTrace();
            }


    }

}

the log says something like this -

09-19 09:12:54.656: W/dalvikvm(555): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
09-19 09:12:54.696: E/AndroidRuntime(555): FATAL EXCEPTION: main
09-19 09:12:54.696: E/AndroidRuntime(555): java.lang.OutOfMemoryError
09-19 09:12:54.696: E/AndroidRuntime(555):  at android.graphics.Bitmap.nativeCreate(Native Method)
09-19 09:12:54.696: E/AndroidRuntime(555):  at android.graphics.Bitmap.createBitmap(Bitmap.java:605)
09-19 09:12:54.696: E/AndroidRuntime(555):  at android.graphics.Bitmap.createBitmap(Bitmap.java:551)
09-19 09:12:54.696: E/AndroidRuntime(555):  at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:437)
09-19 09:12:54.696: E/AndroidRuntime(555):  at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:524)
09-19 09:12:54.696: E/AndroidRuntime(555):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:499)
09-19 09:12:54.696: E/AndroidRuntime(555):  at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:351)
09-19 09:12:54.696: E/AndroidRuntime(555):  at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:773)
09-19 09:12:54.696: E/AndroidRuntime(555):  at android.content.res.Resources.loadDrawable(Resources.java:1935)
09-19 09:12:54.696: E/AndroidRuntime(555):  at android.content.res.Resources.getDrawable(Resources.java:664)
09-19 09:12:54.696: E/AndroidRuntime(555):  at Sarath.app.SilencePlzz.Androidzip.show(Androidzip.java:75)
09-19 09:12:54.696: E/AndroidRuntime(555):  at Sarath.app.SilencePlzz.Androidzip.onResume(Androidzip.java:28)
09-19 09:12:54.696: E/AndroidRuntime(555):  at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1154)
09-19 09:12:54.696: E/AndroidRuntime(555):  at android.app.Activity.performResume(Activity.java:4539)
09-19 09:12:54.696: E/AndroidRuntime(555):  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2434)
09-19 09:12:54.696: E/AndroidRuntime(555):  at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2472)
09-19 09:12:54.696: E/AndroidRuntime(555):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1986)
09-19 09:12:54.696: E/AndroidRuntime(555):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
09-19 09:12:54.696: E/AndroidRuntime(555):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
09-19 09:12:54.696: E/AndroidRuntime(555):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-19 09:12:54.696: E/AndroidRuntime(555):  at android.os.Looper.loop(Looper.java:137)
09-19 09:12:54.696: E/AndroidRuntime(555):  at android.app.ActivityThread.main(ActivityThread.java:4424)
09-19 09:12:54.696: E/AndroidRuntime(555):  at java.lang.reflect.Method.invokeNative(Native Method)
09-19 09:12:54.696: E/AndroidRuntime(555):  at java.lang.reflect.Method.invoke(Method.java:511)
09-19 09:12:54.696: E/AndroidRuntime(555):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-19 09:12:54.696: E/AndroidRuntime(555):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-19 09:12:54.696: E/AndroidRuntime(555):  at dalvik.system.NativeStart.main(Native Method)

Upvotes: 1

Views: 327

Answers (5)

Mani
Mani

Reputation: 1186

Actually you are using most of the time images. In andorid images are handled by Bitmap, so these bitmaps will consume more "Heap Memory", because of creating more bitmap objects every time while changing the images. So we have to use "Object Pooling method" to avoid java.lang.OutOfMemoryError exception.

Here is the link, how to create an Object pooling.

Link:http://www.devahead.com/blog/2011/12/recycling-objects-in-android-with-an-object-pool-to-avoid-garbage-collection/

Upvotes: 0

Jitesh Dalsaniya
Jitesh Dalsaniya

Reputation: 1917

Use below code to set your image in image view.

BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.a1,options);
// Then set this bitmap to your imageview
tv.setImageBitmap(bitmap);

an image with resolution 2048x1536 that is decoded with an inSampleSize of 4 produces a bitmap of approximately 512x384. Loading this into memory uses 0.75MB rather than 12MB for the full image (assuming a bitmap configuration of ARGB_8888).

Upvotes: 0

Raghunandan
Raghunandan

Reputation: 133560

You need to load images efficiently. You can check a sample

Out of Memory error with Bitmap

http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

Use Appropriate decode methods to scale down the image

http://developer.android.com/reference/android/graphics/BitmapFactory.html

On Android 2.3.3 (API level 10) and lower, using recycle() is recommended. If you're displaying large amounts of bitmap data in your app, you're likely to run into OutOfMemoryError errors. The recycle() method allows an app to reclaim memory as soon as possible.

Android 3.0 (API Level 11) introduces the BitmapFactory.Options.inBitmap field. If this option is set, decode methods that take the Options object will attempt to reuse an existing bitmap when loading content. This means that the bitmap's memory is reused, resulting in improved performance, and removing both memory allocation and de-allocation.

For more info do check the below link

http://developer.android.com/training/displaying-bitmaps/manage-memory.html

Also you are calling sleep on the ui thread which blocks the ui thread. You should not call sleep on the ui thread. You might get ANR.

http://developer.android.com/training/articles/perf-anr.html

Upvotes: 2

Bracadabra
Bracadabra

Reputation: 3659

You are using too large bitmaps which don't fit in memory, try to scale it at first. In addition avoid using Thread.sleep() in UI thread, it's bad UX.

Upvotes: 0

prijupaul
prijupaul

Reputation: 2086

Are you doing this in the main thread? If so, please avoid as it would lead to Application not responding dialog. There is beautiful document on how to load image effectively. Please read this link

Upvotes: 0

Related Questions