如何加载gridview与图片从res drawable文件夹在Android上



我找到了这个关于图像视图使用的教程。

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

我将相册从NAT更改为DCIM/Camera,它工作了。这个程序加载图像从sd卡。我需要的是从res drawable文件夹加载图像。我试图将目录从DCIM/Camera更改为res/drawable/myimage.jpg,当然它不起作用,因为这个更改不是这个项目的有效目录。你能检查一下我上面给出的链接,并给我一些关于如何从drawable加载图像的建议吗?

我的第二个问题是关于从url源加载这些图像。

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

GridViewGridViewImageAdapter中使用ImageView,并在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);

代替使用setImageBitmap,你可以简单地使用setImageDrawable(),完全忽略使用位图。使用activity.getResources().getDrawable(DRAWABLE_ID_HERE)来获得可绘制的。

从url加载图片可以在这里找到

最新更新