1

I have an ImageView that is not wanting to display a JPEG, but will display all the other PNG's in the ListView. I have tried things such as adding a transparent background, and nothing works. To get the data, I downloaded all of the images, and stored them into a blob in a SQLite database. I retrieved them by fetching the info from the database, and according to the database, their is binary JPEG data available.

Here is the image I am trying to display (I have it already downloaded in binary data): http://image-store.slidesharecdn.com/1a4f1444-02e2-11e4-9166-22000a901256-large.jpeg

Here is the code I am using to try and convert the binary data back to a Bitmap:

            try 
            { 
                String profilePictureBytes = submittedImageTemp; // this string holds the binary jpeg, png data, etc.
                byte[] encoded;

                try 
                {
                    encoded = profilePictureBytes.getBytes("ISO-8859-1");
                    ByteArrayInputStream imageStream = new ByteArrayInputStream(encoded);
                    Bitmap theBitmap = BitmapFactory.decodeStream(imageStream); 

                    attachPreviewOne.setBackgroundColor(Color.TRANSPARENT);
                    attachPreviewOne.setImageBitmap(theBitmap); 
                } 
                catch (UnsupportedEncodingException e) 
                {
                    e.printStackTrace();
                }
            }
user3776241
  • 501
  • 8
  • 20
  • have you get any kind of exception ? – Haresh Chhelana Jul 04 '14 at 07:25
  • The conversion between string an byte array is questionable. Why do you get the binary data as a string in the first place? How is this input string encoded? – Henry Jul 04 '14 at 07:26
  • @Henry The string is there because I had to pass the data to an intent, and converted it to a string. I converted it back to a byte array, and it works for all the others except the JPEG – user3776241 Jul 04 '14 at 07:27

1 Answers1

0

A. Extract your jpeg from the blob to a temp file. You might want to use the getCacheDir() folder for that

B. Load it from temp file:

 BitmapFactory.Options options = new BitmapFactory.Options();
 Bitmap bm = BitmapFactory.decodeFile(jpegTemFile, options);  //<------------
 imageView.setImageBitmap(bm);


The above should work fine. Still - few things you might want to consider:

C. Depending on your jpeg, the loading operation - decodeFile - might be lengthy. You might want to do it in an AsyncTask.

D. You might also want to consider setting a sampleSize so to reduce size of loaded image:

options.inSampleSize = 2; // setting to N will reduce in-memory size by factor of N*N


Or, you might use Picasso for the job. I used it in several projects and was happy with what I got:

Picasso.with(context)
  .load(url)
  .resize(50, 50)
  .centerCrop()
  .into(imageView)


Community
  • 1
  • 1
Gilad Haimov
  • 5,767
  • 2
  • 26
  • 30