首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从“下载url”Android下载图像

从“下载url”Android下载图像
EN

Stack Overflow用户
提问于 2014-11-28 08:56:37
回答 2查看 1.4K关注 0票数 0

有大量从url代码下载的图像。但是,可以从已经操作下载的url下载图像吗?我是说;

关于从那个链接下载这张图像的大量代码

但是我想从这个链接下载图像到android设备上。

有可能吗?

谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-11-28 12:24:49

这里面没什么难的。

公共类DownloadImage扩展活动{

代码语言:javascript
复制
String image_URL=http://www.hdwallpapers.in/download/minions_2015-1280x720.jpg; // give image   name to save in sd card

String extStorageDirectory;
String sdCard;
Bitmap bitmap;
File file;
String savedFilePath;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button buttonSave = (Button)findViewById(R.id.save);

    ImageView bmImage = (ImageView)findViewById(R.id.image);
    buttonSave.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            myAsyncTask myWebFetch = new myAsyncTask();
            myWebFetch.execute();
        }
    });
}

class myAsyncTask extends AsyncTask<Void, Void, Void>    {
    TextView tv;

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        dialog.dismiss();

    }
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
          public ProgressDialog dialog;
        dialog = new ProgressDialog(DownloadImage.this);
        dialog.setMessage("Loading...");
        dialog.setCancelable(false);
        dialog.setIndeterminate(true);
        dialog.show();
    }
    protected Void doInBackground(Void... arg0) {
        try {
            //set the download URL, a url that points to a file on the internet
            //this is the file to be downloaded
            URL url = new URL(image_URL);

            //create the new connection
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

            //set up some things on the connection
            urlConnection.setRequestMethod("GET");
            urlConnection.setDoOutput(true);

            //and connect!
            urlConnection.connect();

            //set the path where we want to save the file
            //in this case, going to save it on the root directory of the
            //sd card.
            File SDCardRoot = Environment.getExternalStorageDirectory();
            //create a new file, specifying the path, and the filename
            //which we want to save the file as.
            File file = new File(SDCardRoot,"somefile.jpg");

            //this will be used to write the downloaded data into the file we created
            FileOutputStream fileOutput = new FileOutputStream(file);

            //this will be used in reading the data from the internet
            InputStream inputStream = urlConnection.getInputStream();

            //this is the total size of the file
            int totalSize = urlConnection.getContentLength();
            //variable to store total downloaded bytes
            int downloadedSize = 0;

            //create a buffer...
            byte[] buffer = new byte[1024];
            int bufferLength = 0; //used to store a temporary size of the buffer

            //now, read through the input buffer and write the contents to the file
            while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
                //add the data in the buffer to the file in the file output stream (the file on the sd card
                fileOutput.write(buffer, 0, bufferLength);
                //add up the size so we know how much is downloaded
                downloadedSize += bufferLength;
                //this is where you would do something to report the prgress, like this maybe
                //updateProgress(downloadedSize, totalSize);

            }
            //close the output stream when done
            fileOutput.close();

        //catch some possible errors...
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }  
}

}

现在您直接在sd卡中下载了图像。不要忘记在清单中为外部存储提供权限。

票数 0
EN

Stack Overflow用户

发布于 2014-11-28 09:14:37

试着检查一下这个教程,对我很有帮助。他使用一个名为ImageLoader的自定义类和一个公共方法DisplayImage(String url, int loader, ImageView imageView),您可以这样称呼它:

代码语言:javascript
复制
int loader = R.drawable.loader;

// Imageview to show
ImageView image = (ImageView) findViewById(R.id.image);

// Image url
String image_url = "http://www.example.com/images/sample.jpg";

// ImageLoader class instance
ImageLoader imgLoader = new ImageLoader(getApplicationContext());

imgLoader.DisplayImage(image_url, loader, image);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27184995

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档