dhian
dhian

Reputation: 3

Android.Downloaded image changes transparency

I face a problem when downloading an image from remote server. The image is initially transparent but when I download it the transparency changes into a black background.

Callable.invoke(new Callable<Response>() {
     @Override
     public Response call() throws Exception {
         return service.getImage(image);
     }
   }, new TaskCallback<Response>() {
          @Override
          public void success(final Response result) {

              try {
                  InputStream in = result.getBody().in();
                  Bitmap b = BitmapFactory.decodeStream(in);

                  String mDirectoryPathname = Environment
                           .getExternalStoragePublicDirectory(
                              Environment.DIRECTORY_PICTURES)+ "/images/";
                  String timestamp = new SimpleDateFormat("yyyyMMdd'_'HHmmssSSS")
                                               .format(new Date());
                  String filename = timestamp + ".png";
                  final Uri uri = ImageDownloadUtils.createDirectoryAndSaveFile(
                                        context,
                                        b,
                                        filename,
                                        mDirectoryPathname);

             } catch (IOException e) {
                   e.printStackTrace();
             }
         }

         @Override
         public void error(Exception e) {
           Log.e(TAG, "Error getting image via OAuth.", e);
         }
});

Here is the factory method which is called to save the image.

 public static Uri createDirectoryAndSaveFile(Context context,
                                            Bitmap imageToSave,
                                            String fileName,
                                            String directoryPathname) {

    if (imageToSave == null)
        return null;

    File directory = new File(directoryPathname);

    if (!directory.exists()) {
      directory.mkdirs();
    }

    File file = new File(directory, getTemporaryFilename(fileName));

    if (file.exists())
      file.delete();

    try  {
      FileOutputStream outputStream = new FileOutputStream(file);
      imageToSave.compress(Bitmap.CompressFormat.JPEG,
      100,
      outputStream);
      outputStream.flush();
    } catch (Exception e) {
      return null;
    }

    String absolutePathToImage = file.getAbsolutePath();

    ContentValues values = new ContentValues();
    values.put(MediaStore.Images.Media.TITLE, fileName);
    values.put(MediaStore.Images.Media.DESCRIPTION, fileName);
    values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());
    values.put(MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME,
    file.getName().toLowerCase(Locale.US));
    values.put("_data",
    absolutePathToImage);

    ContentResolver cr = context.getContentResolver();

    cr.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

    return Uri.parse(absolutePathToImage);
 }

Upvotes: 0

Views: 146

Answers (1)

Msp
Msp

Reputation: 2493

This is not the problem with image's transparency. When you download an image and view it in Gallery, it is showing the image in an ImageView. That ImageView has a default background of black colour. That's why the transparent part of the downloaded image appears as black.

If you are showing the image in your application, set the background of ImageView to transparent.

android:background="@android:color/transparent"

in XML or

imageView.setBackgroundColor(Color.TRANSPARENT);

through code.

EDIT: If still the problem persists, try this :

InputStream in = result.getBody().in();
Bitmap b = BitmapFactory.decodeStream(in);

String mDirectoryPathname = Environment
        .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/images/";
String timestamp = new SimpleDateFormat("yyyyMMdd'_'HHmmssSSS").format(new Date());
String filename = timestamp + ".png";

try {
    File file = new File(mDirectoryPathname, filename);
    FileOutputStream outputStream = new FileOutputStream(file);
    b.compress(Bitmap.CompressFormat.PNG, 100, outputStream);

    outputStream.flush();
    outputStream.close();
} catch (IOException e) {
    e.printStackTrace();
}

final Uri uri = Uri.fromFile(file);

Upvotes: 1

Related Questions