mudit
mudit

Reputation: 25534

create a transparent image programmatically in android

i am getting an image from web server (which has transparent background).now i need to save this image on my sdcard and show it to user.but when i save the image to sdcard.. it saves with a black background..i need this image with background..

i hv tried following methods to save the image ---

Method 1:

Bitmap bitmap = BitmapFactory.decodeStream(is);
byte[] b;
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 75, bytes);
b = bytes.toByteArray();
    try {
        File myFile = new File("/sdcard/" + image_id + ".jpg");
        myFile.createNewFile();
        OutputStream filoutputStream = new FileOutputStream(myFile);
        filoutputStream.write(b);
        filoutputStream.flush();
        filoutputStream.close();
        }catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Log.d("file", "not created");
            }

Method 2:

MediaStore.Images.Media.insertImage(getContentResolver(),
            bitmap, "sample", "image/png");

plz help...

Upvotes: 0

Views: 2564

Answers (1)

Tughi
Tughi

Reputation: 1093

Why don't you save the stream to a file? It is already encoded as a transparent image.

Quick note: JPEG doesn't support transparency.

Upvotes: 7

Related Questions