-2

It Compresses in low quality but i want to send high quality image

         bitmap = BitmapFactory.decodeFile(str);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 40, stream);
        byte[] byte_v = stream.toByteArray();
        if(bitmap!=null)
        {
            bitmap.recycle();
        }

        encod = Base64.encodeToString(byte_v,Base64.DEFAULT);
        ImageMulti();

    }
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
vsking
  • 1
  • 3

2 Answers2

1

Set 100 insted of 40

 bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
Jay Thummar
  • 2,281
  • 1
  • 14
  • 22
  • one more little detail : change the CompressFormat from `PNG` to `JPEG` it will increase the speed of the transaction – user 007 Jun 19 '18 at 11:47
0

If you want the actual image taken camera to server u need to create the image

Try this code

Delcare this Variable

private String actualPictureImagePath = "";

Then call this method on button click cameraIntent()

private void cameraIntent() {
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = timeStamp + ".jpg";
    File storageDir = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES);
    actualPictureImagePath = storageDir.getAbsolutePath() + "/" + imageFileName;
    File file = new File(pictureImagePath);
    Uri outputFileUri = Uri.fromFile(file);
    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);               
    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
    startActivityForResult(cameraIntent, 1);
}

and Then in onActivityResult() handle this

@override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1) {
    File imgFile = new  File(actualPictureImagePath);
        if(imgFile.exists()){        
       Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
       // Now use this bitmap to send to server 
       // Code to convert bitmap to Base64 
      ByteArrayOutputStream baos = new ByteArrayOutputStream();  
      myBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm 
      is the bitmap object   
      byte[] byteArrayImage = baos.toByteArray(); 
      String encodedImage = Base64.encodeToString(byteArrayImage, 
      Base64.DEFAULT);

        }
    }

}

Alternate Solution for this

@override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1) {
        File imgFile = new  File(actualPictureImagePath);
            if(imgFile.exists()){        
          InputStream inputStream = null;//You can get an inputStream using any IO API
inputStream = new FileInputStream(imgFile.getAbsolutePath());
byte[] buffer = new byte[8192];
int bytesRead;
ByteArrayOutputStream output = new ByteArrayOutputStream();
Base64OutputStream output64 = new Base64OutputStream(output, Base64.DEFAULT);
try {
    while ((bytesRead = inputStream.read(buffer)) != -1) {
        output64.write(buffer, 0, bytesRead);
    }
} catch (IOException e) {
    e.printStackTrace();
}
output64.close();

String base64String = output.toString();

            }
        }

    }

This is the code to use for Bitmap to Base64

ByteArrayOutputStream baos = new ByteArrayOutputStream();  
          myBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm 
          is the bitmap object   
          byte[] byteArrayImage = baos.toByteArray(); 
          String encodedImage = Base64.encodeToString(byteArrayImage, 
          Base64.DEFAULT);

Alternate Solution

NOTE:-

Do not forget to Add runtime permissions and in manifest also

1)Read and Write Persmission

2)Camera persmission

Quick learner
  • 10,632
  • 4
  • 45
  • 55