【问题标题】:Reduce Photo Size in Android before uploading it on server在将照片上传到服务器之前减小 Android 中的照片大小
【发布时间】:2015-08-31 19:29:44
【问题描述】:



我想在将照片上传到服务器之前减小 android 中的照片大小。如何在不影响图像优化的情况下做到这一点?

我还想允许用户裁剪图像,所以我找到了一些教程,但是通过 cmets,我知道用户在某些 android 版本中会出错。那么有什么方法可以让用户裁剪可以在所有安卓版本中使用的图像。

【问题讨论】:

标签: android image


【解决方案1】:

从您设备的图库中选择图片时,使用以下代码裁剪和缩小图片尺寸

Intent photoPickerIntent = new Intent(
                            Intent.ACTION_PICK,
                            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    // photoPickerIntent.setType("image/*");
                    photoPickerIntent.putExtra("crop", "true");
                    photoPickerIntent.putExtra("outputX", 512);
                    photoPickerIntent.putExtra("outputY", 512);
                    photoPickerIntent.putExtra("aspectX", 1);
                    photoPickerIntent.putExtra("aspectY", 1);
                    photoPickerIntent.putExtra("scale", true);
                    File f = new File(android.os.Environment
                            .getExternalStorageDirectory(), "tt.jpg");

希望它有所帮助..快乐编码:)

【讨论】:

    【解决方案2】:
    public Bitmap decodeAndResizeFile(File f) {
            try {
                // Decode image size
                BitmapFactory.Options o = new BitmapFactory.Options();
                o.inJustDecodeBounds = true;
                BitmapFactory.decodeStream(new FileInputStream(f), null, o);
    
                // The new size we want to scale to
                final int REQUIRED_SIZE = 70;
    
                // Find the correct scale value. It should be the power of 2.
                int width_tmp = o.outWidth, height_tmp = o.outHeight;
                int scale = 1;
                while (true) {
                    if (width_tmp / 2 < REQUIRED_SIZE
                            || height_tmp / 2 < REQUIRED_SIZE)
                        break;
                    width_tmp /= 2;
                    height_tmp /= 2;
                    scale *= 2;
                }
                BitmapFactory.Options o2 = new BitmapFactory.Options();
                o2.inSampleSize = scale;
                return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
            } catch (FileNotFoundException e) {
            }
            return null;
        }
    
    
    convert your bitmap object to file on=bject and use these ::->
    
    File file = new File("your bitmap path ");
                Bitmap bmpp = decodeAndResizeFile(file);
    

    【讨论】:

      【解决方案3】:

      试试这个它对我有用:

      private Bitmap getBitmap(String path) {
      
      Uri uri = getImageUri(path);
      InputStream in = null;
      try {
          final int IMAGE_MAX_SIZE = 1200000; // 1.2MP
          in = mContentResolver.openInputStream(uri);
      
          // Decode image size
          BitmapFactory.Options o = new BitmapFactory.Options();
          o.inJustDecodeBounds = true;
          BitmapFactory.decodeStream(in, null, o);
          in.close();
      
      
      
          int scale = 1;
          while ((o.outWidth * o.outHeight) * (1 / Math.pow(scale, 2)) > 
                IMAGE_MAX_SIZE) {
             scale++;
          }
          Log.d(TAG, "scale = " + scale + ", orig-width: " + o.outWidth + ", 
             orig-height: " + o.outHeight);
      
          Bitmap b = null;
          in = mContentResolver.openInputStream(uri);
          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();
              Log.d(TAG, "1th scale operation dimenions - width: " + width + ",
                 height: " + height);
      
              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();
      
          Log.d(TAG, "bitmap size - width: " +b.getWidth() + ", height: " + 
             b.getHeight());
          return b;
      } catch (IOException e) {
          Log.e(TAG, e.getMessage(),e);
          return null;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-24
        • 1970-01-01
        • 1970-01-01
        • 2012-10-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多