简体   繁体   中英

How to load gridview with images from res drawable folder on Android?

I found this tutorial about image view usage .

http://www.androidhive.info/2013/09/android-fullscreen-image-slider-with-swipe-and-pinch-zoom-gestures/

I changed the photoalbum from NAT to DCIM / Camera and it worked. This program loads the images from sd card. What I need is to load images from res drawable folder. I tried to change the directory from DCIM / Camera to res/drawable/myimage.jpg and of course it didn't work since this change is not a valid directory for this project. Can you please check the link I gave above and give me some advice on how to load images from drawable ?

And my 2nd question will be about loading these images from a url source.

Logcat Results ;

07-10 22:26:32.060: I/Process(25981): Sending signal. PID: 25981 SIG: 9
07-10 22:26:48.379: D/AndroidRuntime(26069): Shutting down VM
07-10 22:26:48.379: W/dalvikvm(26069): threadid=1: thread exiting with uncaught exception (group=0x41fecd40)
07-10 22:26:48.387: E/AndroidRuntime(26069): FATAL EXCEPTION: main
07-10 22:26:48.387: E/AndroidRuntime(26069): Process: info.androidhive.imageslider, PID: 26069
07-10 22:26:48.387: E/AndroidRuntime(26069): java.lang.RuntimeException: Unable to start activity ComponentInfo{info.androidhive.imageslider/info.androidhive.imageslider.GridViewActivity}: java.lang.NullPointerException
07-10 22:26:48.387: E/AndroidRuntime(26069):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
07-10 22:26:48.387: E/AndroidRuntime(26069):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
07-10 22:26:48.387: E/AndroidRuntime(26069):    at android.app.ActivityThread.access$800(ActivityThread.java:139)
07-10 22:26:48.387: E/AndroidRuntime(26069):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
07-10 22:26:48.387: E/AndroidRuntime(26069):    at android.os.Handler.dispatchMessage(Handler.java:102)
07-10 22:26:48.387: E/AndroidRuntime(26069):    at android.os.Looper.loop(Looper.java:136)
07-10 22:26:48.387: E/AndroidRuntime(26069):    at android.app.ActivityThread.main(ActivityThread.java:5102)
07-10 22:26:48.387: E/AndroidRuntime(26069):    at java.lang.reflect.Method.invokeNative(Native Method)
07-10 22:26:48.387: E/AndroidRuntime(26069):    at java.lang.reflect.Method.invoke(Method.java:515)
07-10 22:26:48.387: E/AndroidRuntime(26069):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
07-10 22:26:48.387: E/AndroidRuntime(26069):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
07-10 22:26:48.387: E/AndroidRuntime(26069):    at dalvik.system.NativeStart.main(Native Method)
07-10 22:26:48.387: E/AndroidRuntime(26069): Caused by: java.lang.NullPointerException
07-10 22:26:48.387: E/AndroidRuntime(26069):    at info.androidhive.imageslider.helper.Utils.getFilePaths(Utils.java:39)
07-10 22:26:48.387: E/AndroidRuntime(26069):    at info.androidhive.imageslider.GridViewActivity.onCreate(GridViewActivity.java:36)
07-10 22:26:48.387: E/AndroidRuntime(26069):    at android.app.Activity.performCreate(Activity.java:5248)
07-10 22:26:48.387: E/AndroidRuntime(26069):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
07-10 22:26:48.387: E/AndroidRuntime(26069):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2173)
07-10 22:26:48.387: E/AndroidRuntime(26069):    ... 11 more
07-10 22:26:49.809: I/jdwp(26069): Ignoring second debugger -- accepting and dropping
07-10 22:26:50.072: I/jdwp(26069): Ignoring second debugger -- accepting and dropping
07-10 22:26:50.641: I/jdwp(26069): Ignoring second debugger -- accepting and dropping
07-10 22:26:50.944: I/jdwp(26069): Ignoring second debugger -- accepting and dropping

The GridView uses an ImageView in GridViewImageAdapter and loads the image reading from the sdcard with the following code in getView(...)

    Bitmap image = decodeFile(_filePaths.get(position), imageWidth,
            imageWidth);

    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    imageView.setLayoutParams(new GridView.LayoutParams(imageWidth,
            imageWidth));
    imageView.setImageBitmap(image);

Instead of using setImageBitmap you could simply use setImageDrawable() and ignore using bitmaps entirely. To get the drawable use activity.getResources().getDrawable(DRAWABLE_ID_HERE) .

Loading images from a url can be found here .

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