【问题标题】:Android : Download images from server in real sizeAndroid:从服务器下载真实大小的图像
【发布时间】:2015-10-12 05:39:29
【问题描述】:

我正在从服务器下载图像并将其存储在我的 internal_storage 文件夹中。但是下载的图像大小为 (Width 800 * Height 534) 以像素为单位, internal_storage 图像大小为 (Width 400 * 高度 267)。为什么不存储图像尺寸是 (Width 800 * Height 534) 以像素为单位的实际尺寸。

这是我从服务器下载图像的代码

str_DownLoadUrl = "http://103.24.4.60/CLASSNK1/MobileService.svc/DownloadFile/FileName/3_20150928162252018.png";

 download_PngFile(str_DownLoadUrl);

    void download_PngFile(String fileUrl) {

            try {
                URL ImgUrl = new URL(fileUrl);
                HttpURLConnection conn = (HttpURLConnection) ImgUrl.openConnection();
                conn.connect();

                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 2;
                Bitmap imagenObtenida = BitmapFactory.decodeStream(conn.getInputStream(), null, options);


                File file = new File(newFolder, imageName);

                if (file.exists()) file.delete();
                try
                {
                    FileOutputStream out = new FileOutputStream(file);
                    imagenObtenida.compress(Bitmap.CompressFormat.PNG, 100, out);
                    out.flush();
                    out.close();
                    int imagenObtenidaW = imagenObtenida.getWidth();
                    int imagenObtenidaH = imagenObtenida.getHeight();
                    Log.e("imagenObtenidaW " ," = +" + imagenObtenidaW + " imagenObtenidaH = " + imagenObtenidaH);

                } catch (Exception e) {

                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

【问题讨论】:

  • 我的图像查看器说这不是一个有效的 PNG 文件。 file 实用程序将其识别为 JPEG。
  • options.inSampleSize = 2; -- 文档说 -- 如果设置为 > 1 的值,则请求解码器对原始图像进行二次采样,返回较小的图像以节省内存。 ----developer.android.com/reference/android/graphics/…
  • @Tasos 您应该将其发布为答案。
  • 我给了我的答案试试这种方式,让我知道它是否有效?

标签: android


【解决方案1】:

它发生了,因为这里你使用了BitmapFacotory 选项inSampleSize 的选项属性。当您使用inSampleSize = 2 时,您的图像将变为原始高度和宽度的1/2。因此,为了保持原始大小,您需要将其设置为 1,或者将其删除并在 decodeStream 函数中作为 null 传递。

【讨论】:

    【解决方案2】:
    options.inSampleSize = 2;
    

    docs 说 -- 如果设置为大于 1 的值,则请求解码器对原始图像进行二次采样,返回较小的图像以节省内存。

    所以尝试将其设置为 1 看看是否有帮助

    【讨论】:

    • 但不是原图。最好不要使用 Bitmap 和 BitmapFactory 下载文件。
    【解决方案3】:

    您为什么不使用Picasso 而不是编写自定义代码,请看一下它迄今为止非常简单且功能强大的库。

    【讨论】:

      【解决方案4】:

      试试这个方法,更多请关注http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

      BitmapFactory.Options options = new BitmapFactory.Options();
          options.inJustDecodeBounds = true;
          BitmapFactory.decodeResource(getResources(), R.id.myimage, options);
          int imageHeight = options.outHeight;
          int imageWidth = options.outWidth;
          String imageType = options.outMimeType;
      

      Aquery 也用于图像加载,下载 aquery jar 并将其导入您的项目并尝试这种方式

       public class MainActivity extends Activity {
      
          private ImageView img;
          private AQuery aq;
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              aq = new AQuery(this);
              img=(ImageView)findViewById(R.id.simpleLoadImg);
              aq.id(R.id.simpleLoadImg).image("103.24.4.60/CLASSNK1/MobileService.svc/DownloadFile/FileName/3_20150928162252018.png",false,false);
      
          }
      
      
      }
      

      【讨论】:

        猜你喜欢
        • 2015-09-10
        • 2012-09-22
        • 1970-01-01
        • 2016-01-21
        • 2017-08-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多