从SD过滤大小或分辨率中对照片进行操作选择



我从 sd 那里得到一张照片,开始一个新的意图:

Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO); 

这很好用!但在某些情况下,图像尺寸太大,移动设备崩溃。如果是正常尺寸的图像没有问题,但我想有一种过滤或避免较大尺寸图像的方法。

尝试使用这个

Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            intent.addCategory(Intent.CATEGORY_OPENABLE);
            startActivityForResult(
                    Intent.createChooser(intent, "Select Picture"), 0);

希望它会有所帮助,它对我有用。

我找到了这个问题的解决方案,我使用下一个方法使用位图选项调整位图的大小。您可以设置最大尺寸(实际上是 1.2MP),但那里的结果很棒。

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

}

最新更新