Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在将照片上传到服务器之前,先减小Android中的照片大小_Android_Image - Fatal编程技术网

在将照片上传到服务器之前,先减小Android中的照片大小

在将照片上传到服务器之前,先减小Android中的照片大小,android,image,Android,Image,在将照片上传到服务器之前,我想减小android中的照片大小。如何在不影响图像优化的情况下实现这一点? 我还想允许用户裁剪图像,所以我找到了一些教程,但通过评论,我知道用户在一些android版本中会出错。那么,是否有任何方法允许用户裁剪在所有android版本中都可用的图像。试试这个,它对我有用: private Bitmap getBitmap(String path) { Uri uri = getImageUri(path); InputStream in = null; try {



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


我还想允许用户裁剪图像,所以我找到了一些教程,但通过评论,我知道用户在一些android版本中会出错。那么,是否有任何方法允许用户裁剪在所有android版本中都可用的图像。

试试这个,它对我有用:

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

在从设备的多媒体资料中拾取图像时,请使用以下代码裁剪并缩小图像的大小

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");
希望有帮助..快乐编码:)

公共位图解码和大小文件(文件f){
试一试{
//解码图像大小
BitmapFactory.Options o=新的BitmapFactory.Options();
o、 inJustDecodeBounds=true;
解码流(新的FileInputStream(f),null,o);
//我们要扩展到的新尺寸
所需的最终int_尺寸=70;
//找到正确的刻度值。它应该是2的幂。
内部宽度=o.向外宽度,高度=o.向外高度;
int标度=1;
while(true){
如果(宽度\u tmp/2<要求的\u尺寸
||高度(tmp/2<所需尺寸)
打破
宽度_tmp/=2;
高度_tmp/=2;
比例*=2;
}
BitmapFactory.Options o2=新的BitmapFactory.Options();
o2.inSampleSize=刻度;
返回BitmapFactory.decodeStream(新文件输入流(f),null,o2);
}catch(filenotfounde异常){
}
返回null;
}
将位图对象转换为file on=bject并使用以下命令:->
File File=新文件(“位图路径”);
位图bmpp=解码文件(文件);
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);