有梦请追

加油!加油!加油!加油!加油!

图片下载
package com.example.tupian;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;


public class MainActivity<IcsTestActivity> extends Activity {


    private final static String TAG = "MainActivity";
    private final static String ALBUM_PATH
            = Environment.getExternalStorageDirectory()+"/TT/";
    private ImageView mImageView;
    private ImageView mImageView1;
    private Button mBtnSave;
    private ProgressDialog mSaveDialog = null;
    private Bitmap mBitmap;
    private String mFileName;
    private String mSaveMessage;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        mImageView = (ImageView)findViewById(R.id.IV);
        mImageView1 = (ImageView)findViewById(R.id.IV1);
        mBtnSave = (Button)findViewById(R.id.BT);

        new Thread(connectNet).start();


//下载图片
mBtnSave.setOnClickListener(new Button.OnClickListener(){
    public void onClick(View v) {
        mSaveDialog = ProgressDialog.show(MainActivity.this, "保存图片", "图片正在保存中,请稍等...", true);
        new Thread(saveFileRunnable).start();
        showiv();
}

    
});

    }
    private void showiv() {
        // TODO Auto-generated method stub
        BitmapFactory factory= new BitmapFactory();
        Bitmap bm= factory.decodeFile(ALBUM_PATH+"test.jpg");
        mImageView1.setImageBitmap(bm);
    }
    
    public byte[] getImage(String path) throws Exception{
        URL url = new URL(path);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setConnectTimeout(5 * 1000);
        conn.setRequestMethod("GET");
        InputStream inStream = conn.getInputStream();
        if(conn.getResponseCode() == HttpURLConnection.HTTP_OK){
            return readStream(inStream);
        }
        return null;
    }

   
    public InputStream getImageStream(String path) throws Exception{
        URL url = new URL(path);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setConnectTimeout(5 * 1000);
        conn.setRequestMethod("GET");
        if(conn.getResponseCode() == HttpURLConnection.HTTP_OK){
            return conn.getInputStream();
        }
        return null;
    }
   
    public static byte[] readStream(InputStream inStream) throws Exception{
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while( (len=inStream.read(buffer)) != -1){
            outStream.write(buffer, 0, len);
        }
        outStream.close();
        inStream.close();
        return outStream.toByteArray();
    }

   
    public void saveFile(Bitmap bm, String fileName) throws IOException {
        File dirFile = new File(ALBUM_PATH);
        if(!dirFile.exists()){
            dirFile.mkdir();
        }
        File myCaptureFile = new File(ALBUM_PATH + fileName);
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
        bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);
        bos.flush();
        bos.close();
    }

    private Runnable saveFileRunnable = new Runnable(){
        @Override
        public void run() {
            try {
                saveFile(mBitmap, mFileName);
                mSaveMessage = "图片保存成功!";
            } catch (IOException e) {
                mSaveMessage = "图片保存失败!";
                e.printStackTrace();
            }
            messageHandler.sendMessage(messageHandler.obtainMessage());
        }

    };

    private Handler messageHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            mSaveDialog.dismiss();
            Log.d(TAG, mSaveMessage);
            Toast.makeText(MainActivity.this, mSaveMessage, Toast.LENGTH_SHORT).show();
        }
    };


    private Bitmap getImageBitmap(String url){
        URL imgUrl = null;
        Bitmap bitmap = null;
        try {
            imgUrl = new URL(url);
            HttpURLConnection conn = (HttpURLConnection)imgUrl.openConnection();
            conn.setDoInput(true);
            conn.connect();
            InputStream is = conn.getInputStream();
            bitmap = BitmapFactory.decodeStream(is);
            is.close();
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
        }
        return bitmap;
    }
   
    private Runnable connectNet = new Runnable(){
        @Override
        public void run() {
            try {
                String filePath = "https://ss0.baidu.com/73t1bjeh1BF3odCf/it/u=1014815171,2992716709&fm=96&s=8010CD3314A5D7153FACE9CD0300A0A3";
                mFileName = "test.jpg";

                //以下是取得图片的两种方法
                //////////////// 方法1:取得的是byte数组, 从byte数组生成bitmap
              /*  byte[] data = getImage(filePath);
                if(data!=null){
                    mBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);// bitmap
                }else{
                    Toast.makeText(IcsTestActivity.this, "Image error!", 1).show();
                }
                ////////////////////////////////////////////////////////
*/
                //******** 方法2:取得的是InputStream,直接从InputStream生成bitmap ***********/
                mBitmap = BitmapFactory.decodeStream(getImageStream(filePath));
                //********************************************************************/

                // 发送消息,通知handler在主线程中更新UI
                connectHanlder.sendEmptyMessage(0);
                Log.d(TAG, "set image ...");
            } catch (Exception e) {
                Toast.makeText(MainActivity.this,"无法链接网络!", 1).show();
                e.printStackTrace();
            }

        }

    };

    private Handler connectHanlder = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            Log.d(TAG, "display image");
            // 更新UI,显示图片
            if (mBitmap != null) {
                mImageView.setImageBitmap(mBitmap);// display image
            }
        }
    };
   
}






<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
 >

    <ImageView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:id="@+id/IV" />
<Button  android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/BT"
        android:layout_below="@id/IV"/>
<ImageView
    android:layout_below="@id/BT"
      android:layout_width="200dp"
        android:layout_height="600dp"
        android:id="@+id/IV1" />
</RelativeLayout>


权限

    <uses-permission android:name="android.permission.INTERNET"/>
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>  
 




阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34308476/article/details/51558575
想对作者说点什么? 我来说一句

GetWebPics

2010年02月22日 2.69MB 下载

批量图片下载批量图片下载

2009年03月05日 3KB 下载

PicsGet (批量图片下载) 8.3

2009年03月13日 2.32MB 下载

一键下载月宫图片工具

2010年08月15日 9KB 下载

百度图片下载器python版

2014年05月09日 1.57MB 下载

手机SIM图片下载 SIM图片下载

2010年10月30日 53KB 下载

图片下载 C#

2018年01月29日 176KB 下载

美空的图片下载软件使用

2011年03月31日 490KB 下载

是关于后台管理图片下载

2011年06月09日 117KB 下载

没有更多推荐了,返回首页

不良信息举报

图片下载

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭