Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.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 Processing_Compression - Fatal编程技术网

减少Android中的图像文件大小(图像类型未知)

减少Android中的图像文件大小(图像类型未知),android,image-processing,compression,Android,Image Processing,Compression,我想让用户选择他们想减少多少图像文件的大小,可以是低,中或高,然后上传到一个服务器,这部分很容易 下一部分是文件大小的实际缩减。我正在获取图像URI,我想减小文件的大小,图像类型可以是png、jpg或其他 我知道位图.compress(),这是实现的唯一方法还是现有的开源库 Bitmap bitmap; bitmap = MyBitmapFactory.decodeFile(PATHTOPICT, UPLOAD_REQUIRED_SIZE); if (bitmap != null) {

我想让用户选择他们想减少多少图像文件的大小,可以是低,中或高,然后上传到一个服务器,这部分很容易

下一部分是文件大小的实际缩减。我正在获取图像URI,我想减小文件的大小,图像类型可以是png、jpg或其他

我知道位图.compress(),这是实现的唯一方法还是现有的开源库

Bitmap bitmap;
bitmap = MyBitmapFactory.decodeFile(PATHTOPICT, UPLOAD_REQUIRED_SIZE);
if (bitmap != null) {
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
使用该方法(通过google找到):

公共静态位图解码文件(字符串文件路径,所需的最终整数大小){
BitmapFactory.Options o=新的BitmapFactory.Options();
o、 inJustDecodeBounds=true;
解码文件(文件路径,o);
内部宽度=o.向外宽度,高度=o.向外高度;
int标度=1;
while(true){

如果(width\u tmp)您的解决方案仅适用于JPEG?您能解释一下PNG和BMP吗?如何将结尾从*.jpg编辑为*.whatyouwant或尝试Bitmap.CompressFormat.PNG吗
public static Bitmap decodeFile(String filePath, final int REQUIRED_SIZE) {
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, o);
    int width_tmp = o.outWidth, height_tmp = o.outHeight;
    int scale = 1;
    while (true) {
        if (width_tmp <= REQUIRED_SIZE && height_tmp <= REQUIRED_SIZE)
            break;
        width_tmp /= 2;
        height_tmp /= 2;
        scale *= 2;
    }
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;
    Bitmap bitmap = BitmapFactory.decodeFile(filePath, o2);
    return bitmap;
}