Thanks for sharing your successful result with us.
I'll look further into the ability to use a stream from an HTTP
request with BitmapFactory.decodeStream. Some HTTP stream
implementations don't provide sensible answers for things like
InputStream.available() and other methods and this might be causing
BitmapFactory issues.
Cheers,
Justin
Android Team @ Google
On Aug 19, 7:13 am, kingkung <[EMAIL PROTECTED]> wrote:
> Mark and Justin, thanks for all of your help. Finally got everything
> to work, for anyone who runs into these problems in the future:
>
> 1) I took Mark's advice and used the HttpClient to retrieve the image,
> but still it was producing inconsistent results. Finally, instead of
> using BitmapFactory.decodeStream(), I tried
> BitmapFactory.decodeByteArray(), and that seems to work perfectly.
> Final code:
>
> public static Bitmap retrieveImageFromURL(String urlString) throws
> Exception
> {
> Logger.log("Getting URL string "+urlString);
> Bitmap bm = null;
> HttpClient hc = new HttpClient();
> GetMethod gm = new GetMethod(urlString);
> Logger.log("Executing get request");
> hc.executeMethod(gm);
> Logger.log("Decoding response");
> bm = BitmapFactory.decodeByteArray(gm.getResponseBody(), 0,
> gm.getResponseBody().length);
> return bm;
> }
>
> 2) Justin, you were correct about the openFileInput. This one, I had
> to use BitmapFactory.decodeStream(), but it seems to work for internal
> files. Final code:
>
> public Bitmap retrieveFile ()
> {
> return
> BitmapFactory.decodeStream(context.openFileInput("a.png"));
> }
>
> Thanks for all your help!
>
> On Aug 16, 9:45 am, "Justin (Google Employee)" <[EMAIL PROTECTED]>
> wrote:
>
> > > I think
> > > this should actually cause a problem
>
> > Sorry, I meant to say I think this should actually *not* cause a
> > problem.
>
> > Cheers,
> > Justin
> > Android Team @ Google
>
> > On Aug 15, 5:03 pm, "Justin (Google Employee)" <[EMAIL PROTECTED]>
> > wrote:
>
> > > > The first problem I'm having is that the following code works 50% of
> > > > the time:
>
> > > You seem to be doing something odd here. Why are you creating a
> > > BufferedInputStream from your URLConnection object and then handing
> > > the input stream from url.openStream() to the BitmapFactory? I think
> > > this should actually cause a problem, but all you really need to do
> > > here is
>
> > > URL url = new URL(urlString);
> > > Bitmap bm = BitmapFactory.decodeString(url.openStream());
>
> > > With your file issue, try using
> > > BitmapFactory.decodeFile(context.openFileInput("a.png")) . My guess
> > > would be that BitmapFactory wants an absolute path name, and "a.png"
> > > is a relative one. The context.openFileInput call expects a relative
> > > path.
>
> > > Cheers,
> > > Justin
> > > Android Team @ Google
>
> > > On Aug 14, 6:06 pm,kingkung<[EMAIL PROTECTED]> wrote:
>
> > > > I'm having infinite trouble storing/retrieving images.
>
> > > > The first problem I'm having is that the following code works 50% of
> > > > the time:
>
> > > > public static Bitmap retrieveImageFromURL(String urlString)
> > > > throws
> > > > Exception
> > > > {
> > > > URL url = new URL(urlString);
> > > > URLConnection conn = url.openConnection();
> > > > conn.connect();
> > > > BufferedInputStream bis = new
> > > > BufferedInputStream(conn.getInputStream());
> > > > Bitmap bm =
> > > > BitmapFactory.decodeStream(url.openStream());
> > > > if (bm==null) Logger.log("!!!!!DAMMIT");
> > > > return bm;
> > > > }
>
> > > > I receive the DAMMIT many times when the pictures are anywhere over
> > > > 100x100 px. But it does work sometimes, so I know my code is
> > > > correct. Is there a reason for this? I understand that it should
> > > > take a long time, but it shouldn't crash.
>
> > > > The second problem I have is the following code, which should save/
> > > > retrieve a file. Save *seems* to work (returns true), but everytime I
> > > > try to retrieve it, it returns null:
>
> > > > //Store the file -- returns true everytime.
> > > > public boolean storeFile(Bitmap b)
> > > > {
> > > > fOut =
> > > > context.openFileOutput("a.png",Context.MODE_PRIVATE);
> > > > return b.compress(Bitmap.CompressFormat.PNG, 100, fOut);
> > > > }
>
> > > > //Retrieve the file -- returns null everytime
> > > > public Bitmap retrieveFile ()
> > > > {
> > > > return BitmapFactory.decodeFile("a.png");
> > > > }
>
> > > > This is simple code. Is there something seriously wrong with the
> > > > android SDK with regards to files?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[EMAIL PROTECTED]
Announcing the new Android 0.9 SDK beta!
http://android-developers.blogspot.com/2008/08/announcing-beta-release-of-android-sdk.html
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---