User
User

Reputation: 1236

Convert bitmap to jpeg android

I have a bitmap captured from taking photo in android. The photo is in Bitmap. I have to convert this bitmap into JPEG without any loss. I tried the below code,

val file = File(filePath)
profileImageBitmap.compress(Bitmap.CompressFormat.JPEG,100,FileOutputStream(file))

but when I set the jpeg image using RoundedBitmapFactoryDrawable, the captured image is very small. I want to the exact bitmap to jpeg. Can someone help me with this? Thanks.

Upvotes: 1

Views: 3661

Answers (1)

piotrek1543
piotrek1543

Reputation: 19351

I think that might be useful:

Use this:

Bitmap bmp = null;
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.jpeg, 100, stream);
byte[] byteArray = stream.toByteArray();

for that you can use this:

    FileInputStream fileInputStream=null;

    File file = new File("yourfile");

    byteArray = new byte[(int) file.length()];

    try {
        //convert file into array of bytes
  fileInputStream = new FileInputStream(file);
  fileInputStream.read(bFile);
  fileInputStream.close();

  //convert array of bytes into file
  FileOutputStream fileOuputStream = 
              new FileOutputStream("C:\\testing2.txt"); 
  fileOuputStream.write(bFile);
  fileOuputStream.close();

  System.out.println("Done");
    }catch(Exception e){
        e.printStackTrace();
    }

and also for more info go with here

Please read How to convert a bitmap to a jpeg file in Android? for more solutions

Hope it help

Upvotes: 1

Related Questions