简体   繁体   中英

How to upload image to server in full quality in android using Httppost without compression

I am trying to upload image to a server without compression, to keep full quality.

I found the following method :

compress(Bitmap.CompressFormat.JPEG, 100, bao);

but it doesn't work in my case. The original image is 2 MB, but once on the server it's only 60 KB.

The image capture is using the following code

Intent i=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i,camdata);


protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode==RESULT_OK) {
        Bundle bn=data.getExtras();
        bm=(Bitmap)bn.get("data");
        imageview.setImageBitmap(bm);
        imageview.setClickable(false);
        ByteArrayOutputStream bao = new ByteArrayOutputStream();
        BitmapFactory.Options options=new BitmapFactory.Options();
        options.inSampleSize=6;
        final float scale=getResources().getDisplayMetrics().density;
        Display dis=getWindowManager().getDefaultDisplay();
        int width=dis.getWidth();
        scalebmp=Bitmap.createScaledBitmap(bm,400,300, true);
        scalebmp.compress(Bitmap.CompressFormat.JPEG, 100, bao);

        byte [] ba = bao.toByteArray();
        String imgstr=Base64.encodeToString(ba,Base64.DEFAULT);
    }
}
scalebmp.compress(Bitmap.CompressFormat.PNG, 100, bao);

use above line instead of below line

scalebmp.compress(Bitmap.CompressFormat.JPEG, 100, bao);

see JPEG/JPG is lossless image where PNG never lose his quality in compression hopefully it will work for you

Change value from 6 to 1 of options.inSampleSize

Try this

options.inSampleSize = 1;

instead of

 options.inSampleSize=6;

Happy Coading !!

Initialize Uri

Uri selectedImage;
private final int PICK_IMAGE_CAMERA = 1

Write Below code in Oncreate For Ignore Exception

StrictMode.VmPolicy.Builder builder1 = new StrictMode.VmPolicy.Builder(); StrictMode.setVmPolicy(builder1.build());

Put Below Code in Your Camera Listener

final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/Your Folder Name/";
                            File newdir = new File(dir);
                            newdir.mkdirs();

                            String file = dir + DateFormat.format("yyyy-MM-dd_hhmmss", new Date()).toString() + ".jpg";

                            File newfile = new File(file);
                            try {
                                newfile.createNewFile();
                            } catch (IOException ignored) {

                            }

                            selectedImage = Uri.fromFile(newfile);

                            Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, selectedImage);
                            startActivityForResult(cameraIntent, PICK_IMAGE_CAMERA);

Put Below Code in onActivityResult

try {
                        if (selectedImage != null) {
                            if (selectedImage.toString().startsWith("file:")) {

                                Your File Name = new File(selectedImage.getPath());

                                InputStream in = null;
                                try {
                                    final int IMAGE_MAX_SIZE = 1200000;
                                    in = getContentResolver().openInputStream(selectedImage);
                                    // Decode image size
                                    BitmapFactory.Options o = new BitmapFactory.Options();
                                    o.inJustDecodeBounds = true;
                                    BitmapFactory.decodeStream(in, null, o);
                                    try {
                                        in.close();
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                    int scale = 1;
                                    while ((o.outWidth * o.outHeight) * (1 / Math.pow(scale, 2)) >
                                            IMAGE_MAX_SIZE) {
                                        scale++;
                                    }
                                    Bitmap b = null;
                                    in = getContentResolver().openInputStream(selectedImage);
                                    if (scale > 1) {
                                        scale--;
                                        // scale to max possible inSampleSize that still yields an image
                                        // larger than target
                                        o = new BitmapFactory.Options();
                                        o.inSampleSize = scale;
                                        b = BitmapFactory.decodeStream(in, null, o);

                                        // resize to desired dimensions
                                        int height = b.getHeight();
                                        int width = b.getWidth();

                                        double y = Math.sqrt(IMAGE_MAX_SIZE
                                                / (((double) width) / height));
                                        double x = (y / height) * width;

                                        Bitmap scaledBitmap = Bitmap.createScaledBitmap(b, (int) x,
                                                (int) y, true);
                                        b.recycle();
                                        b = scaledBitmap;

                                        System.gc();
                                    } else {
                                        b = BitmapFactory.decodeStream(in);
                                    }
                                    in.close();
                                    Your Image View.setImageBitmap(b);
                                } catch (Exception ignored) {
                                }
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM