Office Hours — Today, June 28

Saturday, June 25

Jun 28
7:20 PM
Mark M.
has entered the room
7:25 PM
Mark M.
turned on guest access
7:35 PM
Michaelidle
has entered the room
Mark M.
sorry!
hello, Michaelidle!
(wish this chat would beep when people enter...)
how can I help you today?
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
as that will handle the background threading and stuff for you
you just hand the library the Uri and the ImageView, and it does the rest
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
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
but
beyond that, use ContentResolver and openInputStream() to get an InputStream
then, use BitmapFactory.decodeStream() to get a Bitmap
then wrap that in a BitmapDrawable
and, do all of that on a background thread
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
the current constructors take a Resources as the first parameter
it's not entirely clear why, in the case of the BitmapDrawable(Resources, Bitmap) constructor
but, getting a Resources is merely a matter of calling getResources() on your Activity or other Context
so, use that
7:55 PM
Michaelidle
I'm using "public BitmapDrawable (Bitmap bitmap)" and it says its deprecated.
Mark M.
correct
use public BitmapDrawable(Resources res, Bitmap bitmap)
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
a Uri has a scheme
that is a path
specifically, a filesystem path to a location on external storage
8:05 PM
Michaelidle
Oh hm. So I'm doing something wrong here.
Mark M.
where are you getting that value from?
8:10 PM
Michaelidle
Mark M.
the query is fine, but don't use DATA
instead, assemble the appropriate Uri
hold on a moment while I track down a code snippet
I knew I answered a question about this today...
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
you would do the same thing, except use MediaStore.Images.Media.EXTERNAL_CONTENT_URI instead of MediaStore.Videos.Media.EXTERNAL_CONTENT_URI
the benefit of this is that the Uri will work even for content that you cannot reach, such as images on removable storage
8:15 PM
Michaelidle
So when requesting the cursor, I'm essentially asking for the wrong thing? Path vs URI?
Mark M.
yes
and the path may not be usable (e.g., removable storage)
so, create the appropriate Uri, so you request the image from MediaStore itself
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.
I can save the content uri as a String correct?
Mark M.
um, well, you will eventually need it back as a Uri
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.
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
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.
**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)
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)
8:30 PM
Michaelidle
Okay. I'll keep at it. Thanks for the help.
Mark M.
OK
you might want to look at the book sample that I listed in that answer, from the RecyclerView chapter
that's a wrap for today's chat
the transcript will go up on https://commonsware.com/office-hours/ shortly
the next chat is tomorrow at 10am US Eastern
have a pleasant day!
Michaelidle
has left the room
Mark M.
turned off guest access

Saturday, June 25

 

Office Hours

People in this transcript

  • Mark Murphy
  • Michaelidle