【问题标题】:android downloaded images are not in correct formatandroid下载的图像格式不正确
【发布时间】:2015-03-31 22:09:22
【问题描述】:

我正在使用下面的代码从我的网络服务下载一些图像,它工作得非常好,但是在将它们下载并复制到 sd 卡后,它们是未知的文件类型,尽管它们最后有 .jpg 并且我的应用程序仍然可以将它们加载到图像视图中,但我无法通过文件管理器中的设备库打开任何下载的图像。有什么想法吗?

这是我下载和保存图像的方法:

public void downloadImage(String url, String fileName) {
    try {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                .permitAll().build();
        StrictMode.setThreadPolicy(policy);
        String[] fileNameSplit = fileName.split("\\.");
        String fileNameMobile = fileNameSplit[0] + "-mob." + fileNameSplit[1];

        URL urlConnection = new URL(url);
        URLConnection connection = urlConnection.openConnection();

        InputStream in = connection.getInputStream();

        File outFile = new File(file.getPath() + "/" + fileNameMobile);
        OutputStream out = new FileOutputStream(outFile);

        copyFile(in, out);
        out.close();
        in.close();


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

}

文件管理器浏览的目录中的示例文件名:

_58df8aa6dbb44ff7b870b49a4b2d8efb-mob.jpg

【问题讨论】:

  • 关心解释否决票??
  • 您是否在文件名中添加了“.jpg”扩展名?
  • @BlackMagic 是的,它就在那里。应用程序可以将图像加载到图像视图中,但是通过使用文件管理器,它们是未知文件,尽管它们最后有 .jpg。

标签: android android-imageview outputstream


【解决方案1】:

试试这个

 bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outStream);

这是一个小例子

File file = new File(file.getPath() + "/" + fileNameMobile);
FileOutputStream fOut = new FileOutputStream(file);

bmp.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
fOut.flush();
fOut.close();

【讨论】:

    【解决方案2】:

    似乎在我从 url 读取下载的文件名后,在代码末尾添加了一个 \n,导致该文件未知,而且我以后使用相同的文件也无法读取该文件没有\n 的名称,但现在通过从文件名末尾删除\n,它现在可以正常工作了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-11
      • 1970-01-01
      • 2016-12-20
      相关资源
      最近更新 更多