【问题标题】:Downloaded png files not readable下载的 png 文件不可读
【发布时间】:2013-08-21 05:57:19
【问题描述】:

我已经通过服务下载了一些 png 文件,现在我正在尝试使用它们。它们进入用户的 SD 卡。我已经确认每个文件都在卡上。但是当我尝试将它们中的任何一个设置为 ImageView 时,我会得到空白。

然后,我通过使用手机的图片查看器手动尝试在手机上显示文件来查看文件是否完好无损。不会打开任何文件。我想知道的是,在下载需要使它们以 png 文件(或位图文件)形式查看的文件后,我是否遗漏了任何步骤。这是我用于下载文件的服务中的代码:

public class DownloadPicture extends IntentService {

    private int result = Activity.RESULT_CANCELED;
    public static final String FILENAME = "filename";
    public static final String FILEPATH = "filepath";
    public static final String RESULT = "result";
    public static final String NOTIFICATION "com.mydomain.myapp.MyBroadcastReceiver";

    public DownloadPicture() {
        super("DownloadPicture");
    }

    public DownloadPicture(String name) {
        super(name);
    }

    @Override
    protected void onHandleIntent(Intent intent) {

        String fileName = intent.getStringExtra(FILENAME);
        String urlPath = this.getResources().getString(R.string.imagesURL) + fileName;
        System.err.println("starting download of: " + fileName);
        System.err.println(urlPath);
        File output = new File(Environment.getExternalStorageDirectory(), fileName);
        if (output.exists()) {output.delete();}

        InputStream stream = null;
        FileOutputStream fos = null;
        try {
          URL url = new URL(urlPath);
          stream = url.openConnection().getInputStream();
          InputStreamReader reader = new InputStreamReader(stream);
          fos = new FileOutputStream(output.getPath());
          int next = -1;
          while ((next = reader.read()) != -1) {
            fos.write(next);
          }

          //Maybe I need to do something else here????

          // Successful finished
          result = Activity.RESULT_OK;

        } catch (Exception e) {
          e.printStackTrace();
        } finally {
          if (stream != null) {
            try {
              stream.close();
            } catch (IOException e) {
              e.printStackTrace();
            }
          }
          if (fos != null) {
            try {
              fos.close();
            } catch (IOException e) {
              e.printStackTrace();
            }
          }
        }

        publishResults(output.getName(), output.getAbsolutePath(), result);
    }
}

【问题讨论】:

  • 代码似乎是正确的。您确定 urlPath 指向 png 文件吗?你试过用浏览器下载吗?
  • 两个问题都是。此外,当我使用手机的文件管理器应用程序直接导航到 SD 卡时,它的扩展名为 .png。
  • 尝试在您的 PC 中下载,然后与您设备中下载的文件进行比较。可能存在连接问题并且文件可能已损坏。而且我认为这不会有什么坏处,但是尝试读取输入流并使用几个字节而不是一次一个字节的缓冲区写入输出流,并尝试在关闭输出流之前刷新。

标签: android image png


【解决方案1】:

不要使用InputStreamReader,它会将字节流转换为字符流。 http://docs.oracle.com/javase/6/docs/api/java/io/InputStreamReader.html

在您的情况下,图像必须保留字节流,因此您可以使用 URL 对象返回的 InputStream

while ((next = stream.read()) != -1) {
    fos.write(next);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-06
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-15
    • 2019-06-04
    相关资源
    最近更新 更多