Office Hours — Today, July 24

Yesterday, July 23

Jul 24
3:50 PM
Mark M.
has entered the room
Mark M.
turned on guest access
3:55 PM
EGHDK
has entered the room
Mark M.
hello, EGHDK
how can I help you today?
4:05 PM
Carlos
has entered the room
EGHDK
I have a bitmap that I'm scaling, but after like 200 x 200 it doesn't get any bigger
Mark M.
hello, Carlos
and how did you determine this?
(Carlos, I will take a question from you shortly!)
Carlos
np
EGHDK
If I set it to 50 x 50, it get's small. When you set it 200 x 200 it gets bigger. But 500 x 500 it stays the same 200 x 200 size
Mark M.
and how are you measuring the size?
EGHDK
I'm doing it programatically for now.
Mark M.
is this size of the Bitmap? size of the ImageView? size of something else?
EGHDK
I'm setting the size of the bitmap, through scaling.
4:10 PM
Mark M.
I repeat: is this size of the Bitmap? size of the ImageView? size of something else?
EGHDK
Size of the bitmap.
I'm not setting the size of the imageView specifically.
Mark M.
and you are determining this size via getHeight() and getWidth()?
EGHDK
The only thing I'm doing with the size is createScaledBitmap
and then I'm doing imageView.setBitmap.
Mark M.
earlier in this chat, you wrote: "But 500 x 500 it stays the same 200 x 200 size"
this implies that you called some methods on some object that returned a pair of 200's
what was the object, and what were the methods?
EGHDK
When I do createScaledBitmap( 50, 50) I get a tiny image. Then I try it with 200 x 200 and I get a bigger image. When I try it with 500 x 500 I'm not getting anything bigger.
4:15 PM
EGHDK
Anything between 200 - 500 seems to stay at the 200 size.
Mark M.
I am not a mind reader
please spend some time collecting your thoughts
I will work with Carlos for a bit, then swing back to you
EGHDK
The exact code for the bitmap is Bitmap resize = Bitmap.createScaledBitmap(image, 50, 50, false);
Mark M.
at that point, in order to help you, you need to be able to answer my questions
Carlos
Mark I have an app with a ListView backed by sqlite database (interfaced through Application object). I want to implement expanded notifications (actions). What I'm trying to figure out is how to update the database and ListView UI at the same time through the npending intent that I assign to the notification action's pending intent.
I can create a new IntentService that updates the database, but how do I update the database simulatanously. I've read about ContentProviders and it seems the have a mechanism to let the UI know when the UI changed, but I've been resisting implementing one since the Application object interface has worked thus far for the app's main functionality.
edits.....in 2nd post...meant how to update UI at the same time.
Mark M.
your service needs to do something to let the UI layer (if it exists) know that a change has occurred
LocalBroadcastManager or a third-party event bus (Square's Otto, greenrobot's EventBus, etc.) would seem like likely candidates
particularly for an IntentService (as that eliminates binding-related callbacks)
Carlos
Being my first app I've been trying to do everything without 3rd party apps in order to learn the framework :)
However...
Mark M.
LocalBroadcastManager is in the Android Support package from Google
Carlos
Since IntentService won't have the context associated with the acitivity hosting the ListFragment I won't have acccess to my ArrayADapter.
Mark M.
correct
you absolutely should not have access to an ArrayAdapter in a Service
4:20 PM
Mark M.
and so your service sends a message, to be picked up by the activity (if it exists), telling it to refresh itself
Carlos
So you are saying that LocalBroadcastManager would let me notify the activity about database changes?
Got it.
Mark M.
yes
it looks a lot like regular broadcasts, except that it is all in-process
Carlos
I'll give that a shot and report back if I have further questions. Thanks Mark!
That was my only question for today.
Mark M.
there's coverage of LocalBroadcastManager in the book, though I forget exactly which chapter
OK
Carlos
cool. I'll check it out
Mark M.
EGHDK: now, I understand how you are creating the scaled Bitmap, but I need to understand how you are determining the resulting sizes
specifically the source for your "But 500 x 500 it stays the same 200 x 200 size" statement
somehow, you are measuring size, and somehow, it is coming out 200 x 200
what are you measuring the size of, and how *specifically* are you measuring it?
EGHDK
Okay, I was getting my code from my other laptop.
Here is a pastebin
Mark M.
that's how you are creating and applying the Bitmap (and I'm skeptical that you need most of that code anyway)
4:25 PM
EGHDK
So I'm dynamically adding an a MyImageView (I extended imageView), and I'm just setting some attributes to it. Then I call addView. Again, in the bitmap scaling method is the only time I'm setting a height for it.
The bitmap is being retrieved from the gallery, and being passed into this method
Mark M.
somehow, you are measuring size, and somehow, it is coming out 200 x 200
what are you measuring the size of, and how *specifically* are you measuring it?
EGHDK
I am not measuring size. I am using this imageView as a thumbnail.
So I don't need it to be proportional or anything.
Mark M.
then please explain: "But 500 x 500 it stays the same 200 x 200 size"
how can you know it is 200 x 200?
if you are "not measuring size"?
EGHDK
I don't know it's 200. But it seems to stay the same size
Second line of the pastebin. I change those values, and run the app again.
With the 50, I get a tiny image. Setting it to 200, the image increases. Setting it beyond 200, it doesn't seem to increase in size at all.
Mark M.
right now, you are claiming that the problem is with the Bitmap
EGHDK
I don't know if I'm making any sense either.
Mark M.
however, since you are "not measuring size", you don't really have any proof of that
EGHDK
I'm claiming that my imageView that is being set dynamically doesn't visually appear to be increasing in size.
Mark M.
so, whether via breakpoints or Log statements or whatever, determine the size of the Bitmap you get out of createScaledBitmap()
4:30 PM
EGHDK
How do I get the size of a bitmap?
Mark M.
getWidth() and getHeight()
EGHDK
oh...
Mark M.
my guess is that your Bitmap is fine
and that the problem lies in how you are using the ImageView
EGHDK
I'm guessing my bitmap is fine too.
But I don't understand how my imageView visually does not increase when I pass it a larger bitmap
But my imageView decreases in size when I pass it a large bitmap
smaller bitmap*
Mark M.
I would fire up Hierarchy View and examine the actual size of the ImageView itself
I would suggest changing your flavor of addView() (I'd use the one that takes the LayoutParams as the second parameter)
EGHDK
?
Mark M.
I am also skeptical about the choice of WRAP_CONTENT, as that seems to run counter to your use of setScaleType()
4:35 PM
EGHDK
Yeah, I'm not really sure how to add it, but on stack overflow there was a question with a lot of upvotes.
All I want to do is add an imageView progamatically using a bitmap.
Mark M.
what are you adding it to?
(BTW, Carlos, if you come up with another question, chime in to let me know)
EGHDK
I'm adding it to a linear layout. Orientation horizontal.
Hieght and width of the linear layout is wrap_content
Mark M.
OK
ignoring things like scaled bitmaps and whatnot, how big are these ImageViews supposed to be?
EGHDK
I want them to be about 200dp x 200dp
Mark M.
OK
4:40 PM
EGHDK
setScaleType would be optional. I really don't care if the images are squished.
Carlos
has left the room
Mark M.
then why are you not setting them to be 200dp x 200dp?
EGHDK
Sorry, I'm just finding this to be really difficult. And I made it a long way since yesterday! Selecting images from the gallery doesn't crash anymore!
Mark M.
for example, you could inflate a layout with android:layout_width="200dp" and android:layout_height="200dp"
or do the equivalent calculations in Java)
rather than using WRAP_CONTENT
EGHDK
Oh! Because I want to be able to add these views to this layout dynamically.
Mark M.
so?
EGHDK
So I want to fit 2 images in the linear layout.
Mark M.
that does not prevent you from inflating layouts to describe the contents
EGHDK
Or 3.
?
I didn't know that was possible?
Mark M.
sure
getLayoutInflater()
returns a LayoutInflater
can be used to inflate layout files whenever you want
EGHDK
So instead of setting all of these attributes I could just create an xml for an imageView?
Mark M.
if you wanted, yes
EGHDK
...
That would be so much easier.
Mark M.
particularly for density-related stuff, yes, it tends to be simpler
EGHDK
Yeah, I mean I was originally gonna get device density and use an equation from Romain Guy to set the density programatically.
Mark M.
call the three-parameter inflate() method that takes the R.layout.whatever_you_want for the ImageView layout, the LinearLayout as the container to pour it into, and probably true for the third parameter
4:45 PM
Mark M.
the only thing tricky with how you're doing it is then getting your hands on that specific ImageView to fill in the image
as they will all have the same android:id value
you need to make sure that you use the View that is returned by inflate()
if that View is the ImageView itself, just cast it
EGHDK
Is there a chapter in your book about it?
Mark M.
if that View is something that contains the ImageView, call findViewById() on the View, not the activity, to find the specific ImageView you just inflated
layout inflation is covered in various spots, though usually in the context of something else (fragments, advanced ListViews, etc.)
EGHDK
Because I sort of understand you. I've never used inflate().
So I would do image.inflate(3 parameters)?
Mark M.
no, you would do getLayoutInflater().inflate(...)
EGHDK
Basically what object do I call inflate on
Mark M.
where getLayoutInflater() is a method on Activity
you do not have an ImageView up front -- that will be created for you by the inflation process
you call inflate() on a LayoutInflater
you get a LayoutInflater usually by calling getLayoutInflater() on your Activity
EGHDK
But in this case I will just do getLayoutInflater().inflate(3 params)?
Mark M.
assuming that you are in your activity, yes
EGHDK
Okay, next question.
I'm doing Bitmap.createScaledBitmap() but is there an easier way to make sure I don't distort the image?
like createScaledRatioBitmap or something?
Mark M.
I'm not even sure why you're bothering with createScaledBitmap()
once you fix the size of your ImageView, your scaleType should handle the scaling
and do it a <bleep>load faster than createScaledBitmap()
4:50 PM
EGHDK
Well, wait. I have to do that or my app get OOM
Mark M.
by definition, no, because your big image is already in memory by the time you call createScaledBitmap()
if you want to create a smaller image to avoid OOM, you do that at the BitmapFactory level
EGHDK
Oh.
Okay stand by. Let me get my code from and you can let me know what route I should take.
4:55 PM
EGHDK
Sorry
Mark M.
you almost always want to use a method on BitmapFactory that takes a BitmapFactory.Options as the last parameter
EGHDK
Wrong code
Mark M.
hmmm... that looks the same
at any rate
EGHDK
Is this line done improperly?
View paste
        rawBitmap = BitmapFactory.decodeStream(stream);
Because that's where I'm actually setting it.
Mark M.
it's not ideal, as you are not providing a BitmapFactory.Options to tell it what to do
EGHDK
So how would I rewrite that line?
Mark M.
add a BitmapFactory.Options instance as a second parameter
where you configure values on the BitmapFactory.Options to indicate how you want the decoding to be done
for example, inSampleSize is how you get it to create a smaller bitmap image for you
EGHDK
I added BitmapFactory.Options as a second param but it wont compile.
Cannot be resolved to a bariable
Mark M.
BitmapFactory.Options is a Java class
you need to add an import statement for it
EGHDK
That's what you meant by "add a BitmapFactory.Options instance as a second parameter" right?
Mark M.
and you need to create an instance of that class
5:00 PM
Mark M.
https://developer.android.com/reference/android..., android.graphics.Rect, android.graphics.BitmapFactory.Options)
or http://goo.gl/U6AaU1 since that URL got munged by the chat room
EGHDK
How do I create an "instance"?
Mark M.
new BitmapFactory.Options()
EGHDK
So BitmapFactory.Options factory = new BitmapFacotry.Options();
Mark M.
sure, other than typos
EGHDK
Awesome. Then just use factory as my second param?
Mark M.
after setting various values on the BitmapFactory.Options via its public fields, yes
EGHDK
And then you just recomend inSampleSize instead of scaling?
Mark M.
yes
EGHDK
Awesome.
I'm well on my way.
Mark M.
let's suppose that your photo is 8MB uncompressed
your current approach will take up, say, 8.8MB (8MB original, 0.8MB of the scaled version)
inSampleSize lets you take up 0.8MB, as you *only* get the scaled version
EGHDK
THATS AWESOME!
Thanks Mark. I guess that's it for today. I'll be here to bother you tomorrow
Mark M.
BTW, you may wish to read up on Picasso in my book
it would simplify a lot of this stuff
while I cover it in the context of downloading image from the Internet, it should work with content:// Uri values as well
EGHDK
Cool.
5:05 PM
EGHDK
I might be downloading images from the net soon.
Mark M.
anyway, that's a wrap for today's chat
EGHDK
You think volley is a good approach?
Mark M.
not really, as I don't like unpackaged, unsupported, undocumented blobs of code
EGHDK
Alright. Thanks again!
Mark M.
next chat is tomorrow 10am
transcript for this chat will appear on http://commonsware.com/office-hours/ shortly
have a pleasant day!
EGHDK
has left the room
Mark M.
turned off guest access

Yesterday, July 23

 

Office Hours

People in this transcript

  • Carlos
  • EGHDK
  • Mark Murphy