Reputation: 3858
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?
Upvotes: 0
Views: 2950
Reputation: 21097
Uri uri = Uri.parse("string");
in this string you can pass the url or the name or path of the image.
Upvotes: 0
Reputation: 10242
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).
Upvotes: 1