在android中使用http请求在android上下载图像

在android中使用http请求在android上下载图像,android,Android,我使用了一个教程,得出了这个结果,它可以在模拟器中工作,但在真正的Android设备上不再工作 这里给出了代码,并设置了所需的所有其他Android权限;设置internet和对外部设备的写入 import java.io.BufferedInputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import

我使用了一个教程,得出了这个结果,它可以在模拟器中工作,但在真正的Android设备上不再工作

这里给出了代码,并设置了所需的所有其他Android权限;设置internet和对外部设备的写入

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import org.apache.http.util.ByteArrayBuffer;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

public class main extends Activity {

    private final String PATH = "/mnt/sdcard/Pictures/";
    TextView tv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView)findViewById(R.id.textView1);
        DownloadFromUrl(PATH + "dwnldimg.png");
        ImageView iv = (ImageView) findViewById(R.id.imageView1);
        Bitmap bmp = BitmapFactory.decodeFile(PATH  + "dwnldimg.png");
        iv.setImageBitmap(bmp);
    }
    public void DownloadFromUrl(String fileName) {
            try {
                    URL url = new URL("http://192.168.1.4/evilempire.jpg"); //you can write here any link
                    File file = new File(fileName);
                     long startTime = System.currentTimeMillis();
                    tv.setText("Starting download......from " + url);
                    HttpURLConnection conn = (HttpURLConnection)url.openConnection();;
                    conn.setDoInput(true);
                    conn.connect();
                    InputStream is = conn.getInputStream();
                    BufferedInputStream bis = new BufferedInputStream(is);
                    /*
                     * Read bytes to the Buffer until there is nothing more to read(-1).
                     */
                    ByteArrayBuffer baf = new ByteArrayBuffer(50);
                    int current = 0;
                    while ((current = bis.read()) != -1) {
                            baf.append((byte) current);
                    }

                    FileOutputStream fos = new FileOutputStream(file);
                    fos.write(baf.toByteArray());
                    fos.close();
                    tv.setText("Download Completed in" + ((System.currentTimeMillis() - startTime) / 1000) + " sec");
            } catch (IOException e) {
                 tv.setText("Error: " + e);
            }
    }

}

首先,在主ui线程上执行网络操作,这将导致3.0之前版本的anr和3.0+版本的NetworkOnMainThreadException。其次,我构建了一个更容易加载远程图像的应用程序。

Prime lib将帮助您,但要学习Android,您需要了解异步任务。Android有运行活动的UI线程,因此如果您执行复杂的操作,阻止Android操作系统,则会强制关闭。 因此,您必须知道的是,如果您需要执行下载任务或任何最终会阻止UI线程的操作,您可以使用线程。简单的Android方法是异步任务。 我认为这个简单的例子将引导您理解异步任务。