0

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
user3633876
  • 117
  • 3
  • 9

1 Answers1

0

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.

Community
  • 1
  • 1
z153
  • 156
  • 5
  • I didn't understand the last part. What is the point of getResources().getDrawable(DRAWABLE_ID_HERE). I modified with my image's id instead of DRAWABLE ID HERE. but I don't know where to use this code. In addition there is no getResources() method , I get error about definin a method called getResources() – user3633876 Jul 10 '14 at 17:21
  • Note that the `GridViewImageAdapter` has an activity field, so it's actually `activity.getResources().getDrawable(DRAWABLE_ID_HERE)` (sorry, my mistake). DRAWABLE_ID_HERE will probably look something like `R.drawable.myImage` if the image you want to load is `res/drawable/myImage.jpg`. You could also use `setImageResource()` directly instead of getting the Drawable object. It's just more flexible if you also want to use web images since you can just have a generic array of Drawables, though it doesn't really make much of a difference in the end. – z153 Jul 10 '14 at 17:32
  • so you are saying that all I need to change is ; imageView.setImageDrawable(activity.getResources().getDrawable(myimage)) is it true ? – user3633876 Jul 10 '14 at 18:42
  • sorry man but I still don't know what to do , can you be more clear please ? – user3633876 Jul 10 '14 at 18:49
  • Sorry if I wasn't clear. You need either `imageView.setImageDrawable(activity.getResources().getDrawable(myimageResource))` or `imageView.setImageResource(myimageResource)`. Does it not work for you? You could have just tried it out. – z153 Jul 10 '14 at 19:05
  • I tried but activity gives error and it says create class activity. – user3633876 Jul 10 '14 at 19:17
  • I have written this --> imageView.setImageResource(R.drawable.myimage); it did not give any errors but when i run it crashed. It said unfortunately stopped. Now I am checking the log cat – user3633876 Jul 10 '14 at 19:19
  • I modified the question and added the logcat results in it. – user3633876 Jul 10 '14 at 19:28
  • Your error is pointed out directly in the logcat .... you're getting in error in `Utils.getFilePaths(Utils.java:39)`. It's probably not related to the `GridViewImageAdapter` code unless it's involved with `Utils` with whatever modifications you might have made to it. – z153 Jul 10 '14 at 19:34
  • All the project is exactly the same as in the link I gave. I only modified the part you said. I have done nothing in Utils – user3633876 Jul 10 '14 at 19:38
  • Do you have `` in your `AndroidManifest.xml`? You need it to read from the sdcard. It doesn't seem to be included by default in the code sample provided. How did the code work before? Did it actually run or did it just compile? – z153 Jul 10 '14 at 19:48
  • interesting. Let me explain what happened. At first the project worked and it was displaying my camera photos. All the images were in DCIM / Camera. Now I changed this part as you suggested imageView.setImageResource(R.drawable.myimage). And then added the external storage permission. Now it works but it shows myimage(R.drawable.myimage) in the grids as a background image and then when i click on a grid then it displays a photo in my camera. I mean it loads the images in camera folder. But i want to display images in drawable folder not in sd card camera. Did you understand my point ? – user3633876 Jul 10 '14 at 20:05
  • You should modify FullScreenViewAdapter like you did GridViewImageAdapter. – z153 Jul 10 '14 at 20:20
  • Actually in this modification I add only one image in drawable but how can I call all of images in drawable folder and set to the list ? – user3633876 Jul 10 '14 at 20:34
  • You can find this on Google (I'm on mobile now). Why would you want all the images in the drawable folder anyway? – z153 Jul 10 '14 at 20:38
  • A good point. Unfortunately this is not up to me , I am assigned to do it. – user3633876 Jul 10 '14 at 20:42