简体   繁体   English

如果尝试从Android的Assets文件夹中获取位图图像,应用程序崩溃了吗?

[英]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: 我在资产文件夹中放置了一些图像,如果在资产文件夹中找不到图像,则尝试将图像名称从db传递给函数,然后我的应用崩溃了。...如果找不到图像,我如何摆脱这个问题呢?有可能在imageview中显示任何文本或任何默认图像,而不是应用程序崩溃了。...这是代码:

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....... 当我将“ imageName”从数据库传递给函数时,如果找不到该图像,则显示我错误并且我的应用程序崩溃了。有人可以帮助我更正代码,以便我可以知道图像是否存在,如果图像存在然后在imageview中设置它,如果找不到图像,则在其中显示任何虚拟图像.......

Here is the Logcat: 这是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. 在catch短语中返回null-由于输入流为null,因此可以防止崩溃。
  2. When ever you call the getBitmapFromAsset(), check the return value. 每当调用getBitmapFromAsset()时,请检查返回值。 If its null, display the default image. 如果为空,则显示默认图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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