1

I'm creating an App for displaying images from a previously specified source. I will be downloading these images if a version file has changed. If there is no internet connection available, it will display the local images I have previously downloaded.

For this, I am using an ImageView and Drawable objects like this:

String imageName = Startseite.FILENAME_PRE + "1" + Startseite.FILENAME_POST;
Drawable d = Drawable.createFromPath(imageName);
mImageView.setImageDrawable(d);

I triple checked via DDMS that the file specified (in this case "page1.jpeg") does indeed exist in the directory "data/data/out.path/files/image1.jpeg". However, in the above code example the second command returns a null object. The file is readable and writable by normal means (streams), but it seems the ImageView doesn't like the image.

I'm not sure how to proceed from here. It could either be that I am missing a crucial security element that allows for the display of images. Or, maybe the image cannot be displayed. Is there a tool to check for displayable images?

Thank you for your time.

Edit 1: I have changed the code to this:

String imageName = Startseite.FILENAME_PRE + "1" + Startseite.FILENAME_POST;
String imageNameWithPath = getApplicationContext().getFilesDir().getPath() + '/' + imageName;
Drawable d = Drawable.createFromPath(imageNameWithPath);

This creates the Drawable object successfully. Sadly, when assigning the object via SetImageDrawable(), my debugger goes nuts and reads 4 exceptions in classes I have never heard of (most probably some Android interna). I have checked for a double slash (because I copy one to the end), but the path looks clean.

Edit 2: Sadly, the same happens when I create the drawable from a stream and not download the image beforehand.

Edit 3: Here is the LogCat snippet of when the application fails.

06-28 08:14:32.323: WARN/dalvikvm(21462): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
06-28 08:14:32.323: ERROR/AndroidRuntime(21462): Uncaught handler: thread main exiting due to uncaught exception
06-28 08:14:32.343: ERROR/AndroidRuntime(21462): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.ourcompany.apps/de.ourcompany.apps.Startseite}: java.lang.NullPointerException
06-28 08:14:32.343: ERROR/AndroidRuntime(21462):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
06-28 08:14:32.343: ERROR/AndroidRuntime(21462):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
06-28 08:14:32.343: ERROR/AndroidRuntime(21462):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
06-28 08:14:32.343: ERROR/AndroidRuntime(21462):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
06-28 08:14:32.343: ERROR/AndroidRuntime(21462):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-28 08:14:32.343: ERROR/AndroidRuntime(21462):     at android.os.Looper.loop(Looper.java:123)
06-28 08:14:32.343: ERROR/AndroidRuntime(21462):     at android.app.ActivityThread.main(ActivityThread.java:4363)
06-28 08:14:32.343: ERROR/AndroidRuntime(21462):     at java.lang.reflect.Method.invokeNative(Native Method)
06-28 08:14:32.343: ERROR/AndroidRuntime(21462):     at java.lang.reflect.Method.invoke(Method.java:521)
06-28 08:14:32.343: ERROR/AndroidRuntime(21462):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
06-28 08:14:32.343: ERROR/AndroidRuntime(21462):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
06-28 08:14:32.343: ERROR/AndroidRuntime(21462):     at dalvik.system.NativeStart.main(Native Method)
06-28 08:14:32.343: ERROR/AndroidRuntime(21462): Caused by: java.lang.NullPointerException
06-28 08:14:32.343: ERROR/AndroidRuntime(21462):     at de.ourcompany.apps.Startseite.displayImages(Startseite.java:61)
06-28 08:14:32.343: ERROR/AndroidRuntime(21462):     at de.ourcompany.apps.Startseite.onCreate(Startseite.java:42)
06-28 08:14:32.343: ERROR/AndroidRuntime(21462):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-28 08:14:32.343: ERROR/AndroidRuntime(21462):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
06-28 08:14:32.343: ERROR/AndroidRuntime(21462):     ... 11 more
06-28 08:14:32.373: INFO/Process(64): Sending signal. PID: 21462 SIG: 3
06-28 08:14:32.373: INFO/dalvikvm(21462): threadid=7: reacting to signal 3
06-28 08:14:32.373: ERROR/dalvikvm(21462): Unable to open stack trace file '/data/anr/traces.txt': Permission denied

I tried the version using the BitmapFactory.decode() method, sadly it results in an exception as well.

What I can add: The following code stems from a video tutorial:

    String mUrlString = "http://www.android.com/images/gingerdroid.png";
    try {
        mImageView.setImageDrawable(
            Drawable.createFromStream(
                    (InputStream) new URL(mUrlString).getContent(), 
                    "qrcode")
        );
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

This code works in the tutorial, yet it exits with the exact same image the same way that my tries do so far.

Hope this helps.

Edit 4: The problem has kinda resolved itself. I created the project anew, copypastad the old source, created the VM anew, suddenly it works. If it was the old project or the VM, I cannot say for sure.

0xCAFEBABE
  • 5,576
  • 5
  • 34
  • 59

3 Answers3

1

I think you must use the full path.

A safe way of getting the path is by using the following:

 Context context = getApplicationContext(); // or other way of getting current context

 String dir = context.getFilesDir().getPath();

Good luck!

[EDIT]: In response to your edit. Can you post a logcat output?

[EDIT 2]: See below for code on how I would go about this.

Nabeel
  • 78
  • 1
  • 1
  • 5
1

Here is what I think you should do:

String imageName = Startseite.FILENAME_PRE + "1" + Startseite.FILENAME_POST;
String imageNameWithPath = getApplicationContext().getFilesDir().getPath() + '/' + imageName;
File img = new File(imageNameWithPath);
mImageView.setImageBitmap(BitmapFactory.decodeFile(img));

Let me know if it works.

Reference: Show Image View from file path?

Community
  • 1
  • 1
Nabeel
  • 78
  • 1
  • 1
  • 5
0

I am not sure if it is the case (we cannot see values from your code), but are you sure you are using full path? In other words is FILENAME_PRE="image" or "/data/data/..../image" ? It should be the latter. (mind the "/" at the beginning which you were missing in your comment).

Jarek Potiuk
  • 19,317
  • 2
  • 60
  • 61
  • I am not using the full path. I do not, because I did not use the full path when saving the file. Using a full path should be unsafe, for the directory structure could change, couldn't it? – 0xCAFEBABE Jun 28 '11 at 07:26
  • Nope. You should know the full path anyway and once you know it, it won't change. Rather than that - current working directory is pretty volatile and I would not relay on this (this is what relative path is doing - adding name to the current working directory). I recommend to always use full path (you can get the right paths where to store your files following the android storage guide http://developer.android.com/guide/topics/data/data-storage.html) depending whether your files should be stored on SD carrd, internally, in a cache or permanently you need to choose the right storage). – Jarek Potiuk Jun 28 '11 at 09:16