2

我从 sd 获得了一张照片,开始了一个新的意图:

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

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

4

2 回答 2

1

尝试使用这个

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

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

于 2012-06-20T00:51:58.607 回答
1

我找到了解决这个问题的方法,我使用下一个方法来使用位图选项调整位图的大小。您可以设置最大尺寸(实际上是 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;
}

}

于 2012-06-20T11:07:17.463 回答