Compression of image not working

怎甘沉沦 提交于 2019-12-13 05:32:42

问题


I am trying to upload picture using camera. I am uploading it to server

  1. Clicking image and showing it in imageview ( DONE)
  2. Compressing the image size to upload (Not Working)
  3. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!