如何从Android上的res drawable文件夹中加载gridview图像?

时间:2014-07-10 16:31:13

标签: android image grid touch drawable

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

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

我将photoalbum从NAT更改为DCIM / Camera,它确实有效。该程序从SD卡加载图像。我需要的是从res drawable文件夹加载图像。我试图将目录从DCIM / Camera更改为res / drawable / myimage.jpg,当然它不起作用,因为此更改不是此项目的有效目录。您能否查看我上面给出的链接,并就如何从drawable加载图片给我一些建议?

我的第二个问题是关于从网址源加载这些图片。

Logcat结果;

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

1 个答案:

答案 0 :(得分:0)

GridView使用ImageView中的GridViewImageAdapter并使用getView(...)

中的以下代码加载来自SD卡的图像读取
    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)

从网址加载图片可以找到here