0

I use this code for set bitmap to imageView and get image form imageView and save it in storage but when save it, the image is black! please help to solve this problem!

Bitmap bitmap = ink.getBitmap();
          img_signature.setImageBitmap(bitmap);
          ByteArrayOutputStream stream = new ByteArrayOutputStream();
          bitmap.compress(Bitmap.CompressFormat.JPEG, 50, stream);
          byte[] byteArray = stream.toByteArray();
          encodedString = Base64.encodeToString(byteArray, 0);
          createDirectoryAndSaveFile(bitmap,"signature.jpg");

and method for save image :

private void createDirectoryAndSaveFile(Bitmap imageToSave, String fileName) {

File direct = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/delivery/");

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

File file = new File(direct, fileName);
if (file.exists())
  file.delete();
try {
  FileOutputStream out = new FileOutputStream(file);
  imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, out);
  out.flush();
  out.close();

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

}

Hadi Khezrpor
  • 401
  • 5
  • 21
  • 1
    There does not appear to be any code here that has anything to do with your problem. Most of the code here is to create an `encodedString` that you do not use. Your problem would reside in `createDirectoryAndSaveFile()`, since that is where you are saving the image. We cannot help you with that method, since your question does not contain the source code for that method. However, since your filename ends in `.jpg`, I would guess that you are trying to save a JPEG. If so, switch to PNG, in case your `Bitmap` is using the alpha channel. – CommonsWare Aug 23 '17 at 14:19
  • What is `createDirectoryAndSaveFile`? What are you doing with base64 and the bytearray? Something like https://stackoverflow.com/questions/14053338/save-bitmap-in-android-as-jpeg-in-external-storage-in-a-folder may help you. Most JPEG support doesn't use alpha channel. – Jean-Baptiste Yunès Aug 23 '17 at 14:19
  • i use PNG and when save it black – Hadi Khezrpor Aug 23 '17 at 14:20
  • ok , i change JPEG in all method and it right work and save image but when read image from folder and set to image view and send to server black image send ? – Hadi Khezrpor Aug 23 '17 at 14:48

0 Answers0