压缩图像不起作用

时间:2015-07-05 10:19:00

标签: android image amazon-s3 imageview image-compression

我正在尝试使用相机上传图片。我正在将其上传到服务器

  1. 点击图片并在imageview(DONE)中显示
  2. 压缩要上传的图片尺寸(不工作)
  3. 将图像上传到S3(使用未压缩的图像)(完成)

    这是我的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;
    
  4. cam.setOnClickListener(new OnClickListener() {
    
                    @Override
                    public void onClick(View v) {
                        Intent cameraIntent = new Intent(
                                android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                        startActivityForResult(cameraIntent, CAMERA_REQUEST);
                    }
                });
    

0 个答案:

没有答案