图片从网址到你的Andr​​oid应用程序

图片从网址到你的Andr​​oid应用程序

本文介绍了如何下载文件/图片从网址到你的Andr​​oid应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要我的Andr​​oid应用程序,使请求的URL从这个网址下载图片所以我建立这个类来帮助我,但没有奏效???

 公共类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();
        }赶上(IOException异常E){
            Log.d(ImageManager,错误:+ E);
        }
    }

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

        返回null;
    }

    保护无效onPostExecute(){
        尝试 {
            的OutputStream OS =新的FileOutputStream(文件);
            byte []的数据=新的字节[is.available()];
            is.read(数据);
            os.write(数据);
            is.close();
            os.close();

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

和我有,在活动类上的onclick(),这个函数:

 公共无效下来(视图v){
    // ImageManager OB =新ImageManager();
    // ob.DownloadFromUrl();

     新MyAsnyc()执行()。
}
 

虽然我写的权限在manfiest.xml

 <使用-SDK安卓的minSdkVersion =7/>
<使用-权限的Andr​​oid:名称=android.permission.INTERNET对>< /使用-许可>
<使用-权限的Andr​​oid:名称=android.permission.ACCESS_NETWORK_STATE>< /使用-许可>
<使用-权限的Andr​​oid:名称=android.permission.READ_PHONE_STATE>< /使用-许可>
<使用-权限的Andr​​oid:名称=android.permission.WRITE_EXTERNAL_STORAG​​E/>
 

解决方案

试试这个

 公共类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 urlso 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();
        }

    }
}

这篇关于如何下载文件/图片从网址到你的Andr​​oid应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 19:28