0

I have a class that have a reference to an image. This image can be on a server or among the drawable resources of the app. I need to store the location of either the url or the drawable location of the image file. In order to handle this, I'm using an Uri as attribute of the class.

First of all, is this a right solution? Then, how can I check if the Uri has a reference to the drawable or a reference to an URL? And how can I store the location of the drawable resource in the Uri?

Daniele Vitali
  • 3,848
  • 8
  • 48
  • 71

2 Answers2

1

What you propose is one solution. It's hard to say if this is the right solution without seeing some code.

I haven't tried this myself but apparently you're able to use an Uri to a drawable like this:

Uri uri = Uri.parse("android.resource://your.package.here/drawable/image_name");
InputStream stream = getContentResolver().openInputStream(uri);

This means that you should be able to store the URI as a String in SharedPreferences. The next time you need to load the image you retrieve the URI string from SharedPreferences and if if the string starts with "android.resource://" you load the image using the content resolver and BitmapFactory.loadStream(). If the string doesn't start with "android.resource://" you get an input stream to the image on the web using whatever Http framework you decide to us (OkHttp, UrlConnection, Android Async Http etc).

britzl
  • 10,132
  • 7
  • 41
  • 38
  • Ok thank you! I'm gonna try it. One more question please... what is the difference between URI and Uri? I've read the documentation, but I didn't get it... – Daniele Vitali May 22 '13 at 08:11
  • Have a look at http://stackoverflow.com/a/1984225/1266551 In my example the Uri identifies a specific resource, but it says nothing of it's location. It is up to the content resolver to actually find the location of the content in the Uri and provide an input stream to it. – britzl May 22 '13 at 09:59
0
Uri uri = Uri.parse("string");

in this string you can pass the url or the name or path of the image.

Muhammad Aamir Ali
  • 20,419
  • 10
  • 66
  • 57