0

I'm uploading image to server using Bitmap, but unable to upload the full-quality image, image does not look the same quality. Any idea or solution would be really appreciated.

I have tried changing Bitmap.CompressFormat.JPEG to PNG and increase the quality to 100.

public Map<String, DataPart> getByteData() {
    Map<String, DataPart> params = new HashMap<>();

    myBitmap = ((BitmapDrawable) ivImage5.getDrawable()).getBitmap();

    Log.i("Info", "Bitmap coversion value for Image : " + myBitmap);

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    myBitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);

    System.out.println("myBitmap:    " + myBitmap);
    System.out.println("imageName" + ivImage);

    long imagename = System.currentTimeMillis();
    params.put("file", new DataPart(
            imagename + ".jpg", getFileDataFromDrawable(myBitmap)));
}
Boken
  • 4,825
  • 10
  • 32
  • 42
B.S. Dash
  • 39
  • 9

1 Answers1

1

Save a full-size photo and provide a fully qualified file name where the camera app will save the photo.

  String mCurrentPhotoPath;  

  private void takePicture() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // Ensure that there's a camera activity to handle the intent
    if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
        // Create the File where the photo should go
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            // Error occurred while creating the File
            Log.d("Error", "" + ex.getMessage());
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            Uri photoURI = null;
            if ((Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT)) {
                photoURI = FileProvider.getUriForFile(getActivity(),
                        "com.your_packageName.provider",
                        photoFile);
            } else {
                photoURI = Uri.fromFile(photoFile);
            }
            if (photoURI != null) {
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
            }
        }
    }
}



    private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir =    getActivity().getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = image.getAbsolutePath();
    Log.d("path", mCurrentPhotoPath);
    return image;
}



 @Override
 public void onActivityResult(int requestCode, int resultCode, Intent data) {

    switch (requestCode) {
        case REQUEST_TAKE_PHOTO: {
            if (resultCode == RESULT_OK) {
                File file = new File(mCurrentPhotoPath);
                Bitmap bitmap = null;
                try {
                    bitmap = MediaStore.Images.Media
                            .getBitmap(getActivity().getContentResolver(), Uri.fromFile(file));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (bitmap != null) {
                    // Glide.with(this).load(Uri.fromFile(file)).into(imageView);
                    Uri fileUri = Uri.fromFile(file);
                    Log.d("URI", fileUri.toString());
                    // Get length of file in bytes
                    long fileSizeInBytes = file.length();
                    // Convert the bytes to Kilobytes (1 KB = 1024 Bytes)
                    long fileSizeInKB = fileSizeInBytes / 1024;
                    // Convert the KB to MegaBytes (1 MB = 1024 KBytes)
                    long fileSizeInMB = fileSizeInKB / 1024;

                } else {
                    Log.d("file", "URI is null");
                }
            }
        }
        break;
 }
}

Android developer guide on saving a full size photo with camera app

Rakhi Dhavale
  • 1,196
  • 12
  • 19