Jun 28 | 7:20 PM |
Mark M. | has entered the room |
Jun 28 | 7:25 PM |
Mark M. | turned on guest access |
Jun 28 | 7:35 PM |
Michaelidle | has entered the room |
Mark M. |
sorry!
|
Mark M. |
hello, Michaelidle!
|
Mark M. |
(wish this chat would beep when people enter...)
|
Mark M. |
how can I help you today?
|
Jun 28 | 7:40 PM |
Michaelidle |
Hey Mark. Working for the first time with the "gallery". I'm trying to get the users camera photos and then display them in a list. So far, I can grab the photos and then I have a bunch of URI's. (storage/emulated/0/DCIM/Camera/img1.jpg etc). What's the best way to go from URI to ImageView?
|
Mark M. |
ideally, use an image-loading library, like Picasso
|
Mark M. |
as that will handle the background threading and stuff for you
|
Mark M. |
you just hand the library the Uri and the ImageView, and it does the rest
|
Mark M. |
this is particularly important if you are aiming to show more than one, using an AdapterView (e.g., ListView) or RecyclerView
|
Michaelidle |
I thought Picasso was for network images, but the fact that it just takes a URI also makes sense. Thanks
|
Mark M. |
most of the image-loading libraries also handle HTTP(S) URLs
|
Jun 28 | 7:45 PM |
Michaelidle |
What if I wanted to go from URI to Drawable? Without a library. Would you happen to know off the top of your head about that? I forgot that the library I'm using doesn't use ImageViews directly, and I have to instead hand this API a drawable.
|
Mark M. |
well, you could still use the library
|
Mark M. |
but
|
Mark M. |
beyond that, use ContentResolver and openInputStream() to get an InputStream
|
Mark M. |
then, use BitmapFactory.decodeStream() to get a Bitmap
|
Mark M. |
then wrap that in a BitmapDrawable
|
Mark M. |
and, do all of that on a background thread
|
Jun 28 | 7:50 PM |
Michaelidle |
BitmapDrawable() constructor with a Drawable is deprecated. Should I be using something else?
|
Mark M. |
there is no BitmapDrawable constructor that takes a Drawable
|
Mark M. |
the current constructors take a Resources as the first parameter
|
Mark M. |
it's not entirely clear why, in the case of the BitmapDrawable(Resources, Bitmap) constructor
|
Mark M. |
but, getting a Resources is merely a matter of calling getResources() on your Activity or other Context
|
Mark M. |
so, use that
|
Jun 28 | 7:55 PM |
Michaelidle |
I'm using "public BitmapDrawable (Bitmap bitmap)" and it says its deprecated.
|
Mark M. |
correct
|
Mark M. |
use public BitmapDrawable(Resources res, Bitmap bitmap)
|
Jun 28 | 8:00 PM |
Michaelidle |
Gotcha. Getting a java.io.FileNotFoundException: No content provider: /storage/emulated/0/DCIM/Camera/IMG_20160628_104908.jpg currently. So once I figure that out, I'll switch over.
|
Mark M. |
that's not a Uri
|
Mark M. |
a Uri has a scheme
|
Mark M. |
that is a path
|
Mark M. |
specifically, a filesystem path to a location on external storage
|
Jun 28 | 8:05 PM |
Michaelidle |
Oh hm. So I'm doing something wrong here.
|
Mark M. |
where are you getting that value from?
|
Jun 28 | 8:10 PM |
Michaelidle | |
Mark M. |
the query is fine, but don't use DATA
|
Mark M. |
instead, assemble the appropriate Uri
|
Mark M. |
hold on a moment while I track down a code snippet
|
Mark M. | |
Mark M. |
I knew I answered a question about this today...
|
Mark M. |
in the answer, I point out some code from one of the book's sample apps, where I assemble the Uri to a video that I retrieve from MediaStore
|
Mark M. |
you would do the same thing, except use MediaStore.Images.Media.EXTERNAL_CONTENT_URI instead of MediaStore.Videos.Media.EXTERNAL_CONTENT_URI
|
Mark M. |
the benefit of this is that the Uri will work even for content that you cannot reach, such as images on removable storage
|
Jun 28 | 8:15 PM |
Michaelidle |
So when requesting the cursor, I'm essentially asking for the wrong thing? Path vs URI?
|
Mark M. |
yes
|
Mark M. |
and the path may not be usable (e.g., removable storage)
|
Mark M. |
so, create the appropriate Uri, so you request the image from MediaStore itself
|
Mark M. |
MediaStore has rights to access the image on the filesystem, even if your app does not
|
Michaelidle |
Okay. Let me try to refactor this real quick.
|
Michaelidle |
I can save the content uri as a String correct?
|
Mark M. |
um, well, you will eventually need it back as a Uri
|
Mark M. |
if by "save the content uri", you mean to a database or some persistent data store, you cannot count on being able to use it after your process terminates
|
Michaelidle |
So just using a List<Uri> would be best instead of making a List<String>
|
Mark M. |
yes
|
Michaelidle |
I meant save as in, in memory/variable.
|
Jun 28 | 8:20 PM |
Michaelidle |
Getting compiler errors, but as I work through them. Does this look more in line of what you're talking about? http://pastebin.com/uN5KKNTm
|
Mark M. |
no
|
Jun 28 | 8:25 PM |
Michaelidle |
Is it the projection that has to change?
|
Mark M. |
ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, cursor.getInt(cursor.getColumnIndex(MediaStore.Images.Media._ID)));
|
Michaelidle |
My query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI uses the URI you were speaking of.
|
Michaelidle |
**This uri stuff is hard**
|
Mark M. |
basically, I mean the code that I showed in the Stack Overflow answer that I pointed out (https://stackoverflow.com/a/38075698/115145) with the modification that I pointed out (MediaStore.Images replacing MediaStore.Videos in both locations)
|
Mark M. |
withAppendedId() assembles the Uri from two pieces: the base (which is the Uri you used in your query) and the _ID of the particular entry you want (pulled from the Cursor)
|
Jun 28 | 8:30 PM |
Michaelidle |
Okay. I'll keep at it. Thanks for the help.
|
Mark M. |
OK
|
Mark M. |
you might want to look at the book sample that I listed in that answer, from the RecyclerView chapter
|
Mark M. |
that's a wrap for today's chat
|
Mark M. |
the transcript will go up on https://commonsware.com/office-hours/ shortly
|
Mark M. |
the next chat is tomorrow at 10am US Eastern
|
Mark M. |
have a pleasant day!
|
Michaelidle | has left the room |
Mark M. | turned off guest access |