问题
I am trying to upload picture using camera. I am uploading it to server
- Clicking image and showing it in imageview ( DONE)
- Compressing the image size to upload (Not Working)
Uploading image to S3 (with uncompressed image) (DONE)
This is my onActivityResult()
case CAMERA_REQUEST: { Uri selectedImage = data.getData(); String[] filePathColumn = { MediaStore.Images.Media.DATA }; Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null); cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); String picturePath = cursor.getString(columnIndex); cursor.close(); BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(picturePath, options); int imageHeight = options.outHeight; int imageWidth = options.outWidth; String imageType = options.outMimeType; options.inSampleSize = calculateInSampleSize(options,200,100);//512 and 256 whatever you want as scale options.inJustDecodeBounds = false; Bitmap yourSelectedImage = BitmapFactory.decodeFile(picturePath,options); File pngDir = new File(Environment.getExternalStorageDirectory(),"PicUploadTemp"); if (!pngDir.exists()) { pngDir.mkdirs(); } File pngfile = new File(pngDir,"texture1.jpg"); FileOutputStream fOut; try { fOut = new FileOutputStream(pngfile); yourSelectedImage.compress(Bitmap.CompressFormat.JPEG, 50,fOut); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } Drawable d = Drawable.createFromPath(pngfile.getPath().toString()); rlLayout.setBackground(d); yourSelectedImage.recycle(); // resizedBitmap.recycle(); xfile = pngfile; } break;
cam.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
来源:https://stackoverflow.com/questions/31229319/compression-of-image-not-working