【问题标题】:Lower uri image quality to reduce file size降低 uri 图像质量以减小文件大小
【发布时间】:2016-12-24 19:01:36
【问题描述】:

我有一个高分辨率imageUri,我想降低图像质量和大小,因为我将它上传到服务器,然后将其作为原始图像的较小版本加载。

我试过这个:

Picasso.with(context).load(image).resize(100,100).get();

但它不起作用,因为您需要从单独的Thread 执行此操作,并且我没有找到一个监听器来告诉您调整大小何时结束。

有没有人可以降低图像质量以获得更小的文件大小的解决方案?

【问题讨论】:

    标签: java android uri


    【解决方案1】:
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 50;
    Bitmap bmpSample = BitmapFactory.decodeFile(fileUri.getPath(), options);
    
    ByteArrayOutputStream out = new ByteArrayOutputStream();                
    bmpSample.compress(Bitmap.CompressFormat.JPEG, 1, out);
    byte[] byteArray = out.toByteArray();
    return convertToUri(byteArray);
    

    谢谢Android: Compress Bitmap from Uri

    【讨论】:

    • convertToUri中如何将byteArray转为Uri?
    【解决方案2】:

    第 1 步:在 ContentResolver 上调用 openInputStream(),并传入您的 Uri

    第 2 步:创建一个 BitmapFactory.Options 对象,使用 inSampleSize 控制您想要执行的下采样程度,创建一个较低分辨率的 Bitmap

    步骤#3:将流和BitmapFactory.Options 对象传递给BitmapFactory 上的decodeStream(),以获得下采样的Bitmap

    第 4 步:上传 Bitmap,其中的详细信息会因您用于上传的 API(byte[]File 等)而有所不同。

    第 5 步:在后台线程上执行第 1-4 步,因为您不应该在主应用程序线程上执行磁盘 I/O、位图下采样和网络操作。

    【讨论】:

      猜你喜欢
      • 2018-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多