簡體   English   中英

從“下載網址” Android下載圖片

[英]Download image from 'download url' Android

網址代碼中有大量下載圖片。 但是是否可以從已執行動作下載的網址下載圖片? 我的意思是 ;

有關從該鏈接下載此圖像的大量代碼

但我想從此鏈接將圖像下載到android設備

可能嗎 ?

謝謝

嘗試查看本教程 ,對我很有幫助。 他通過公共方法DisplayImage(String url, int loader, ImageView imageView)使用一個名為ImageLoader的自定義類,您可以通過以下方式調用它:

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);

沒什么難的。

公共類DownloadImage擴展Activity {

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卡中。 不要忘記在清單中授予外部存儲的權限。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM