2

Im new to android development and trying to get metadata of image using ExifInterface. I stored the image under drawable and trying to get the metadata but getting null values for all fields(date, imagelength, imagewidth). I tried to access image path as this :

          String path = "drawable://" + R.drawable.testimage;

and provided this path to ExifInterface.

          ExifInterface exif = new ExifInterface(path);

I dont know if storing image under drawable is correct or not because when I run the app in emulator I get something like this :

          E/JHEAD﹕ can't open 'drawable://2130837561'

So if this is wrong then please tell me where should I store the image and how to provide image path to ExifInterface. Thank you in advance.

  • I don't get the point, during the process of creating your apk file, images inside the drawable folder are optimized (by compressing them). Also all Exif data will be removed, to save space. So what's the point of reading the Exif data if it's not there? – Rolf ツ May 17 '15 at 06:54

1 Answers1

1

To get a drawable, you can you this snippet:

  Drawable drawable = getResources().getDrawable(android.R.drawable.your_drawable);

I'm not sure if your way is correct, as I've never seen it like that. Do you really need the path to your image to use it on that ExifInterface class?

Ok, I did some digging and found this question, which led me to this one. As it seems, you can not get an absolute path from a resource inside your apk. A good solution would be for you to save it as a file on the external memory, and then you can get the path you want.

First of all, add this to your AndroidManifest.xml, so your app can write to the cellphone memory:

<uses-permission
 android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Ok, to save it you can try this, first create a bitmap from your drawable resource:

Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.your_drawable);

After that get the path you want to save your images, and put it on a String. More info on that here.

The Android docs have a good example on how to get the path. You can see it here.

To keep it simple, I'll copy and paste the snippet from the docs.

    void createExternalStoragePrivateFile() {
    // Create a path where we will place our private file on external
    // storage.
    File file = new File(getExternalFilesDir(null), "DemoFile.jpg");

    try {
        // Very simple code to copy a picture from the application's
        // resource into the external file.  Note that this code does
        // no error checking, and assumes the picture is small (does not
        // try to copy it in chunks).  Note that if external storage is
        // not currently mounted this will silently fail.
        InputStream is = getResources().openRawResource(R.drawable.balloons);
        OutputStream os = new FileOutputStream(file);
        byte[] data = new byte[is.available()];
        is.read(data);
        os.write(data);
        is.close();
        os.close();
    } catch (IOException e) {
        // Unable to create file, likely because external storage is
        // not currently mounted.
        Log.w("ExternalStorage", "Error writing " + file, e);
    }
}

void deleteExternalStoragePrivateFile() {
    // Get path for the file on external storage.  If external
    // storage is not currently mounted this will fail.
    File file = new File(getExternalFilesDir(null), "DemoFile.jpg");
    if (file != null) {
        file.delete();
    }
}

boolean hasExternalStoragePrivateFile() {
    // Get path for the file on external storage.  If external
    // storage is not currently mounted this will fail.
    File file = new File(getExternalFilesDir(null), "DemoFile.jpg");
    if (file != null) {
        return file.exists();
    }
    return false;
}

After that, get the path of the file you saved on the external memory, and do as you wish.

I'll keep the old example as well. You can use the method getExternalStorageDirectory() to get the path, or getExternalCacheDir(). After that, you can use File method called getAbsolutePath() to get your String.

String path = (...) // (you can choose where to save here.)
File file = new File(path, "your_drawable.png");
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); // You can change the quality from 0 to 100 here, and the format of the file. It can be PNG, JPEG or WEBP. 
out.flush();
out.close();

For more info on the Bitmap class, check the docs.

If you need more info, let me know and I'll try to show more samples.

EDIT: I saw your link, and there was this snippet there:

    //change with the filename & location of your photo file
        String filename = "/sdcard/DSC_3509.JPG";
        try {
   ExifInterface exif = new ExifInterface(filename);
   ShowExif(exif);
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
   Toast.makeText(this, "Error!", 
     Toast.LENGTH_LONG).show();
  }

As you can see, if you really want to see the exif data of a internal image resource, you'll have to save it somewhere else, and then you can try to get the absolute path for that File, then, call the method to show the exif.

Community
  • 1
  • 1
Mauker
  • 11,237
  • 7
  • 58
  • 76
  • thank you @Mauker for the reply. You said good solution would be to save it on external memory. Could you please explain it more? I am testing my app on computer(emulator) only. Then how to store image on external memory and get its path? –  May 17 '15 at 03:46
  • 1
    Sorry @Mauker I didnt get it. Please tell me in some simpler way. 1) I'm storing image under drawable. 2) How to get file(image) name as I have to provide file name to ExifInterface. Please here is a example I am referring to [link](http://android-er.blogspot.com/2009/12/read-exif-information-in-jpeg-file.html). As in this example how to provide filename to ExifInterface. –  May 17 '15 at 05:05
  • There's a problem with your link, it says the page doesn't exists... But ok, I'll try to explain in a simpler way. – Mauker May 17 '15 at 05:06
  • Well, I'm a little sleepy right now and I could improve the answer after a good night of sleep. So... You can try with the examples and links I've provided to: 1- save your resource as an image on the external memory. 2- get the path to that file. 3- with the path, extract the exif data. – Mauker May 17 '15 at 05:29
  • thanks. I appreciate your help. Glad to tell you im able to solve the issue finally. Thank you once again. –  May 17 '15 at 06:12
  • You're welcome! And if my answer helped, mark it as correct so it can help some other people in the future. Glad to help! – Mauker May 17 '15 at 06:14
  • Oh. And if it was not my answer that solved your issue, please create your own answer. But if it was mine, please click it as correct. Thanks! – Mauker May 18 '15 at 02:26