Office Hours — Today, August 8

Tuesday, August 6

Aug 8
8:55 AM
Mark M.
has entered the room
Mark M.
turned on guest access
9:00 AM
Ed T.
has entered the room
Ed T.
Good morning sir
Mark M.
hello, Ed!
how can I help you today?
Ed T.
I've been struggling with a defect that I need to fix
Mark M.
reclassify it as a feature!
Ed T.
what makes it a pita.... is I can only use KitKat to fix this issue :(
lol
Mark M.
you can only use KitKat to *fix* the issue, or to reproduce it?
Ed T.
I've got a kiosk type of product... it is actually a Pipo
Mark M.
Ed T.
yes sir
a user needs to stick in a jump drive and get an image
auto mount works... and the image is there
I get something like this back as a uri
9:05 AM
Ed T.
I'm not able to create a file with that and do anything
Mark M.
what are you trying to do with the image?
Ed T.
the part that is confusing and why I'm glad I came to see you today. :-)
Mark M.
put it in an ImageView? copy it to the device? something else?
Ed T.
I need to put it in an imageView, then save it to a folder on the device after validation
Mark M.
you can load it into the ImageView easily enough using your favorite image-loading library: Glide, Picasso, etc.
they know how to handle that sort of Uri
Ed T.
all of the 'standard ways of doing this... call API calls I can't use
Mark M.
those libraries have versions that work on API Level 19, even if they are not the latest versions
Ed T.
View paste
        addV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent()
                        .setType("image/*")
                        .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
                        .addCategory(Intent.CATEGORY_OPENABLE)
                        .setAction(Intent.ACTION_OPEN_DOCUMENT);

                startActivityForResult(Intent.createChooser(intent, "Select an image"), ADD_V_REQUEST_CODE);
            }
        });
Mark M.
OK, that's how you are getting the Uri
Ed T.
the flag might be over kill
Mark M.
yes, that's unnecessary
you use that when you are the one giving a Uri to somebody else
so, again, for displaying the image, Glide and Picasso can handle that Uri
for when you eventually want to make a copy, use ContentResolver and openInputStream() to get an InputStream on the content identified by the Uri, then copy the bytes from the InputStream to some FileOutputStream on your desired file
Ed T.
I'm guessing do that in an AsyncTask correct?
Mark M.
a background thread of some form, yes
Ed T.
I went dumpster diving in your large GitHub repot. :-)
lot there
Mark M.
I write a lot :-)
9:10 AM
Ed T.
Your parents should be proud
you've definitely got the golden star on great Android examples. :-)
Mark M.
I try to be useful
Ed T.
thanks for doing that
Mark M.
you're welcome!
Ed T.
so basically... The Uri resource I have to use to copy only to an area where I have full rights to the file?
anything else I try to do and I've tried lots of things...
Mark M.
think of the Uri as a URL to a Web site, and think of the "copy" as "download"
Ed T.
I end up with a file that not only doesn't do anything.... file.exists() always returns FALSE
Mark M.
that Uri is not a file
Ed T.
the iOS examples where a little cleaner... and made more sense to me
Mark M.
just as a URL is not a file
Ed T.
because they give uri resources and everything is locked down... and The Uri has a short 'time to live' with no guarantees
the weird part.. maybe just because Android supports remote media
Mark M.
the Uri is useful for the life of your activity that gets onActivityResult()
(assuming the user doesn't yank out the thumb drive in the middle of your using it)
Ed T.
so that is the other rub... I'm not in an Activity
Mark M.
that ACTION_OPEN_DOCUMENT bit is in an activity
Ed T.
I'm actually in a DialogFragment
Mark M.
from the standpoint of the Uri, that's part of the activity
Ed T.
but I pass a reference to a context
9:15 AM
Mark M.
your DialogFragment already has a reference to the activity that is displaying it
Ed T.
it's actually being displayed by... class BorderEditLayout extends RelativeLayout
Mark M.
sorry, I'm not certain what "it's" refers to in that sentence
Ed T.
it is rather ugly but sadly... the folks who wrote this didn't read your book first. :-)
the dialog class
Mark M.
that should not change the fact that the DialogFragment knows the activity that hosts it, and it should not affect your use of the Uri
Ed T.
and it has a constructor to a context... where it gets created on the MainActivity
Mark M.
if you wanted to pass the Uri to a service, then there's an additional step or two
FWIW, https://gitlab.com/commonsguy/cw-jetpack-java/t... and https://gitlab.com/commonsguy/cw-jetpack-java/t... are my two most recent examples showing ACTION_OPEN_DOCUMENT, though they are reading in text files rather than copying image files
both are profiles in *Elements of Android Jetpack*
er, "profiled"
Ed T.
thanks Mark!
at least I know the direction and path you've put me on is not a dead end
googling for a kit Kat solution... is 'painful'
happy Thursday!
Mark M.
thanks, you too!
9:25 AM
Ed T.
has left the room
10:00 AM
Mark M.
turned off guest access

Tuesday, August 6

 

Office Hours

People in this transcript

  • Ed Tidwell
  • Mark Murphy