2014-01-28 9 views
2



Я хочу уменьшить размер фото в андроиде, прежде чем загружать его на сервер. Как это сделать без какого-либо эффекта, например, оптимизации на изображении?

Я также хочу разрешить пользователю обрезать изображение, поэтому я нашел несколько учебников, но по комментариям, я узнал, что Пользователь получает ошибку в некоторых версиях Android. Итак, есть ли способ разрешить пользователю обрезать изображения, которые могут быть работоспособны во всех версиях Android.Уменьшить размер фото в Android перед загрузкой на сервер

+0

http://developer.android.com/training/displaying-bitmaps/load-bitmap.html –

ответ

1

Попробуйте это Это работает для меня:

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; 
} 
2

Используйте следующий код, чтобы обрезать и уменьшить размер изображения при выборе его из галереи вашего устройства

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"); 

Надежда это helps..Happy кодирование:

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); 
Смежные вопросы