简体   繁体   中英

App Crashed if trying to get Bitmap Image from Assets Folder in Android ?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM