0

I have put some images in assets folder and trying to pass image name from db to the function if image is not found in assets folder then my app crashed....how can i get rid of this issue if images are not found then is there any possibility to show any text or any default image in imageview instead of app crashed.... Here is the code:

private Bitmap getBitmapFromAsset(String strName) {
    AssetManager assetManager = getAssets();
    InputStream istr = null;
    try {
        istr = assetManager.open(strName);
    } catch (IOException e) {
        e.printStackTrace();
    }
    Bitmap bitmap = BitmapFactory.decodeStream(istr);
    return bitmap;
}

When i am passing "imageName" to the function from DB if that image is not found its showing me error and my app crashed.Can someone please help me to correct the code so that i can know if image exists or not if image exists then set it in imageview and if image not found then show any dummy image in it.......

Here is the Logcat:

02-15 14:59:43.275: E/AndroidRuntime(467): FATAL EXCEPTION: main
02-15 14:59:43.275: E/AndroidRuntime(467): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jamia.binoria/com.jamia.binoria.GeneralTopicQuestions}: java.lang.NullPointerException
02-15 14:59:43.275: E/AndroidRuntime(467):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1736)
02-15 14:59:43.275: E/AndroidRuntime(467):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1752)
02-15 14:59:43.275: E/AndroidRuntime(467):  at android.app.ActivityThread.access$1500(ActivityThread.java:123)
02-15 14:59:43.275: E/AndroidRuntime(467):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:993)
02-15 14:59:43.275: E/AndroidRuntime(467):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-15 14:59:43.275: E/AndroidRuntime(467):  at android.os.Looper.loop(Looper.java:126)
02-15 14:59:43.275: E/AndroidRuntime(467):  at android.app.ActivityThread.main(ActivityThread.java:3997)
02-15 14:59:43.275: E/AndroidRuntime(467):  at java.lang.reflect.Method.invokeNative(Native Method)
02-15 14:59:43.275: E/AndroidRuntime(467):  at java.lang.reflect.Method.invoke(Method.java:491)
02-15 14:59:43.275: E/AndroidRuntime(467):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
02-15 14:59:43.275: E/AndroidRuntime(467):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
02-15 14:59:43.275: E/AndroidRuntime(467):  at dalvik.system.NativeStart.main(Native Method)
02-15 14:59:43.275: E/AndroidRuntime(467): Caused by: java.lang.NullPointerException
02-15 14:59:43.275: E/AndroidRuntime(467):  at com.jamia.binoria.GeneralTopicQuestions.GetQuestionDataForQuranHadeesBean(GeneralTopicQuestions.java:152)
02-15 14:59:43.275: E/AndroidRuntime(467):  at com.jamia.binoria.GeneralTopicQuestions.onCreate(GeneralTopicQuestions.java:75)
02-15 14:59:43.275: E/AndroidRuntime(467):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
02-15 14:59:43.275: E/AndroidRuntime(467):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1700)
02-15 14:59:43.275: E/AndroidRuntime(467):  ... 11 more
Looking Forward
  • 3,579
  • 8
  • 45
  • 65
user3233280
  • 279
  • 2
  • 7
  • 21

1 Answers1

0

A solution to what you ask:

  1. return null in the catch phrase - this will prevent the crash since the input stream is null.
  2. When ever you call the getBitmapFromAsset(), check the return value. If its null, display the default image.
chipopo
  • 326
  • 2
  • 9
  • my Bitmap function is returning "bitmap" i am checking if its null or not but its not coming inside block and its crashed – user3233280 Feb 16 '14 at 16:56
  • According to the logcat you added, the application crashes with null pointer exception on the GeneralTopicQuestions.java line 152. Unless you give more info or show us the code on that area there isn't much we could do to help. – chipopo Feb 16 '14 at 21:46