且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何下载文件/图片从网址到你的Andr​​oid应用程序

更新时间:2023-01-02 23:32:14

试试这个

 公共类MyAsnyc扩展的AsyncTask<虚空,虚空,虚空> {
    公共静态文件的文件;
    InputStream的是;

    保护无效doInBackground()抛出IOException异常{

        文件路径= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        文件=新的文件(路径,DemoPicture.jpg);
        尝试 {
            //确保图片目录存在。
            path.mkdirs();

            网址URL =新的URL(http://androidsaveitem.appspot.com/downloadjpg);
            / *打开到URL的连接。 * /
            URLConnection的UCON = url.openConnection();

            / *
             *定义InputStreams从URLConnection的阅读。
             * /
            是= ucon.getInputStream();

            的OutputStream OS =新的FileOutputStream(文件);
            byte []的数据=新的字节[is.available()];
            is.read(数据);
            os.write(数据);
            is.close();
            os.close();

        }赶上(IOException异常E){
            Log.d(ImageManager,错误:+ E);
        }
    }

    @覆盖
    保护无效doInBackground(虚空...... PARAMS){
        // TODO自动生成方法存根
        尝试 {
            doInBackground();
        }赶上(IOException异常E){
            // TODO自动生成的catch块
            e.printStackTrace();
        }

        返回null;
    }

    保护无效onPostExecute(){
        尝试 {
            //告诉媒体扫描器有关新的文件,以便它是
            //立即提供给用户。
            MediaScannerConnection.scanFile(空,
                    新的String [] {file.toString()},空,
                    新MediaScannerConnection.OnScanCompletedListener(){
                        公共无效onScanCompleted(字符串路径,开放的URI){
                            Log.i(ExternalStorage,扫描+路径+:);
                            Log.i(ExternalStorage, - >的uri =+ URI);
                        }
                    });
        }赶上(IOException异常E){
            // TODO自动生成的catch块
            e.printStackTrace();
        }

    }
}
 

I need my android app to make request to url to download an image from this url so I have built this class to help me, BUT it didn't work ???

public class MyAsnyc extends AsyncTask<Void, Void, Void> {
public static File file;
InputStream is;

    protected void doInBackground() throws IOException {
        File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        file = new File(path, "DemoPicture.jpg");

        try{
            // Make sure the Pictures directory exists.
            path.mkdirs();

            URL url = new URL("http://androidsaveitem.appspot.com/downloadjpg");

            // Open a connection to that URL.
            URLConnection ucon = url.openConnection();

            // Define InputStreams to read from the URLConnection.
            is = ucon.getInputStream();
        } catch (IOException e) {
            Log.d("ImageManager", "Error: " + e);
        }
    }

    @Override
    protected Void doInBackground(Void... params) {
        try {
            doInBackground();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }

    protected void onPostExecute() {
        try {
            OutputStream os = new FileOutputStream(file);
            byte[] data = new byte[is.available()];
            is.read(data);
            os.write(data);
            is.close();
            os.close();

            // Tell the media scanner about the new file so that it is
            // immediately available to the user.
            MediaScannerConnection.scanFile(
                null,
                new String[] { file.toString() },
                null,
                new MediaScannerConnection.OnScanCompletedListener() {
                    public void onScanCompleted(String path, Uri uri) {
                        Log.i("ExternalStorage", "Scanned " + path + ":");
                        Log.i("ExternalStorage", "-> uri=" + uri);
                    }
                }
            );
        }
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

And I have, in the Activity class on onclick(), this function:

public void down(View v) {
    // ImageManager ob=new ImageManager();
    // ob.DownloadFromUrl("");

     new MyAsnyc().execute();
}

Although I have written the permissions in the manfiest.xml

<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

try this

public class MyAsnyc extends AsyncTask<Void, Void, Void> {
    public static File file;
    InputStream is;

    protected void doInBackground() throws IOException {

        File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        file = new File(path, "DemoPicture.jpg");
        try {    
            // Make sure the Pictures directory exists.
            path.mkdirs();

            URL url = new URL("http://androidsaveitem.appspot.com/downloadjpg");
            /* Open a connection to that URL. */
            URLConnection ucon = url.openConnection();

            /*
             * Define InputStreams to read from the URLConnection.
             */
            is = ucon.getInputStream();

            OutputStream os = new FileOutputStream(file);
            byte[] data = new byte[is.available()];
            is.read(data);
            os.write(data);
            is.close();
            os.close();

        } catch (IOException e) {
            Log.d("ImageManager", "Error: " + e);
        }
    }

    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub
        try {
            doInBackground();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }

    protected void onPostExecute() {
        try {
            // Tell the media scanner about the new file so that it is
            // immediately available to the user.
            MediaScannerConnection.scanFile(null,
                    new String[]{file.toString()}, null,
                    new MediaScannerConnection.OnScanCompletedListener() {
                        public void onScanCompleted(String path, Uri uri) {
                            Log.i("ExternalStorage", "Scanned " + path + ":");
                            Log.i("ExternalStorage", "-> uri=" + uri);
                        }
                    });
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}