如何获得巴纽到Android手机(external_sd) [英] How to get an .png to an android phone (external_sd)

查看:184
本文介绍了如何获得巴纽到Android手机(external_sd)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题想获得一个PNG图片到我的Andr​​oid手机。
我现在很远的:

I have a problem trying to get a .png image to my android phone. I am now this far:

URL url = new URL ("http://oranjelan.nl/oranjelan-bg.png");
  InputStream input = url.openStream();
  try { 
     String storagePath = Environment.getExternalStorageDirectory();
     OutputStream output = new FileOutputStream (storagePath + "/oranjelangbg.png");
     try {
    byte[] buffer = new byte[1000000];
        int bytesRead = 0;
        while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) {
        output.write(buffer, 0, bytesRead);
        }
     } finally {
      output.close();
        }
    } finally {
   input.close();
 }

不过,我得到以下错误@字符串storagePath = Environment.getExternalStorageDirectory();该compiller说,不能将文件转换为字符串。

But I get the following error @ String storagePath = Environment.getExternalStorageDirectory(); The compiller says cannot convert file to string.

然后,如果我愚弄了一下周围,并尝试这块code的:

Then if i fool around a bit and try this piece of code:

URL url;

    void setup() {
    try {

        URL url = new URL ("http://oranjelan.nl/oranjelan-bg.png");
        InputStream input = url.openStream();{
        try {

            String storagePath = Environment.getExternalStorageDirectory().getName();
            OutputStream output = new FileOutputStream (storagePath + "/oranjelangbg.png");
                try {


byte[] buffer = new byte[1000000];
                    int bytesRead = 0;
                    while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) {
                    output.write(buffer, 0, bytesRead);
                    }
                    } finally {
            output.close();
            }
            } catch (IOException e) {
            // TODO Auto-generated catch block
            throw new RuntimeException(e);
    } finally {
        try {
            input.close();
            } catch (IOException e) {
            // TODO Auto-generated catch block
            throw new RuntimeException(e);
     }
   }    
 }    
    } catch (MalformedURLException ex) {
      throw new RuntimeException(ex);
    } catch (IOException e) {
      // TODO Auto-generated catch block
      throw new RuntimeException(e);
    }
  }
}

然后我的Andr​​oid冻结当我启动应用程序。

then my android freezes when I launch the app.

有没有人有什么想法?

推荐答案

在除了什么Tanis.7x指出的那样,你将需要移动网络运营出来的UI线程,并进入作为AyncTask这样的东西。否则,你将无法直到它完成下载与应用程序交互 - 这可能会导致应用程序无响应的消息和强制关闭。在最近的Andr​​oid版本,这样做在主线程上的网络是一个自动例外,即使它不会导致延迟。

In addition to what Tanis.7x pointed out, you will need to move the network operations out of the UI thread and into something such as an AyncTask. Otherwise you will not be able to interact with the application until it finishes downloading - that may result in an application unresponsive message and a force close. In more recent android versions, doing networking on the main thread is an automatic exception, even if it does not result in delay.

这篇关于如何获得巴纽到Android手机(external_sd)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆