0

Im using Facebook-Parse to upload a bitmap to the server (so in this case I'd like to use PNG/JPEG format since WEBP gives an error). Im able to upload the image successfully, what I want to know is if it can be made more efficient and if I can reduce the quality of the JPEG further? (NOTE: The images are going to be viewed fullscreen).

    public void save_DetailStory_ToParse()
{
    DisplayMetrics displaymetrics = new DisplayMetrics();
    getActivity().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    int height = displaymetrics.heightPixels;
    int width = displaymetrics.widthPixels;
    String filePath = iDataSave_path;
    final Bitmap bitmap = decodeSampledBitmapFromFilePath(getResources(), width, height, filePath.toString());


    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 70, stream);
    byte[] image = stream.toByteArray();
    ParseFile file = new ParseFile("androidbegin.jpeg", image);

    pObject_storyDetail.put(KEY_IMAGE_FILE, file);


    pObject_storyDetailList.add(pObject_storyDetail);
}

public static Bitmap decodeSampledBitmapFromFilePath(Resources res, int reqWidth, int reqHeight, String filePath)
{

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;

    return BitmapFactory.decodeFile(filePath, options);
}

public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) 
{
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;

    //int inSampleSize = 1;
    int inSampleSize = 2;

    if (height > reqHeight || width > reqWidth) 
    {

        final int halfHeight = height / 2;
        final int halfWidth = width / 2;

        // Calculate the largest inSampleSize value that is a power of 2 and keeps both
        // height and width larger than the requested height and width.
        while ((halfHeight / inSampleSize) > reqHeight && (halfWidth / inSampleSize) > reqWidth) 
        {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}
Zen
  • 2,698
  • 4
  • 28
  • 41
  • You mean other than decreasing the quality of the compression from the value of 70 that you show here? – Doug Stevenson Mar 03 '16 at 01:26
  • refer http://stackoverflow.com/questions/26928597/image-compression-like-whatsapp-and-other-messengers-on-android/33825795#33825795 – saeed Mar 03 '16 at 03:33
  • @DougStevenson Yes. Also, if I can go lower than 70? How low can I go without compromising image quality on all phone-screens – Zen Mar 03 '16 at 20:55
  • That sounds like something for you to experiment with to see for yourself what is acceptable. – Doug Stevenson Mar 03 '16 at 22:01

0 Answers0