完成效果:点击保存按钮,布局中的图片存入到SD卡中。点击读取按钮,图片显示到布局下方。

点击获取网络图片按钮,网络图片显示到布局下方。点击保存网络图片按钮,网络图片存入SD卡中。


主布局:

<?xml version="1.0" encoding="utf-8"?>
布局效果:



代码:

package com.example.administrator.jreduch09;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.drawable.BitmapDrawable;import android.os.AsyncTask;import android.os.Bundle;import android.os.Environment;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;import android.widget.ImageView;import android.widget.Toast;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;public class ImageToSdCardActivity extends AppCompatActivity {    private Button save,read;    private ImageView img,showImg;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_image_to_sd_card);        save= (Button) findViewById(R.id.save);        read= (Button) findViewById(R.id.read);        img= (ImageView) findViewById(R.id.img);        showImg= (ImageView) findViewById(R.id.showImg);    }
将布局中图片存入到SD卡中

public void saveImg(View view){    BitmapDrawable bitmapDrawable= (BitmapDrawable) img.getDrawable();    Bitmap bitmap= bitmapDrawable.getBitmap();    //拿到图片    File file=Environment.getExternalStorageDirectory();    FileOutputStream fos= null;    try {        fos = new FileOutputStream(file+"/1.png");        bitmap.compress(Bitmap.CompressFormat.PNG,100,fos);    } catch (FileNotFoundException e) {        e.printStackTrace();    }finally {        if(fos!=null){            try {                fos.close();            } catch (IOException e) {                e.printStackTrace();            }        }    }}
效果展示:
将图片读到布局的showImg中
public void readImg(View view){        String path=Environment.getExternalStorageDirectory()+"/1.png";        Bitmap bitmap= BitmapFactory.decodeFile(path);        showImg.setImageBitmap(bitmap);    }
效果展示:
获取网络图片显示到布局的showImage中
public void getUriImg(View view){        new GetImg().execute("http://img04.sogoucdn.com/app/a/100520024" +                "/5eec73b7ec46da3a8b132ade22150f7c");    }    public class GetImg extends AsyncTask {        @Override        protected Bitmap doInBackground(String... params) {            Bitmap bitmap=null;            HttpURLConnection con=null;            InputStream is=null;            try {                URL url=new URL(params[0]);                con= (HttpURLConnection) url.openConnection();                con.setConnectTimeout(5 * 1000);                con.setReadTimeout(5 * 1000);                if(con.getResponseCode()==200){                    is= con.getInputStream();                    bitmap=BitmapFactory.decodeStream(is);                    return bitmap;                }            } catch (MalformedURLException e) {                e.printStackTrace();            } catch (IOException e) {                e.printStackTrace();            } finally {                if(is!=null){                    try {                        is.close();                    } catch (IOException e) {                        e.printStackTrace();                    }                }                if(con!=null){                    con.disconnect();                }            }            return null;        }        @Override        protected void onPostExecute(Bitmap bitmap) {            super.onPostExecute(bitmap);            showImg.setImageBitmap(bitmap);        }
效果展示:
将网络读片存入到SD卡中
public class SaveHttpImg extends AsyncTask{        @Override        protected String doInBackground(String... params) {            Bitmap bitmap=null;            HttpURLConnection con=null;            InputStream is=null;            try {                URL url=new URL(params[0]);                con= (HttpURLConnection) url.openConnection();                con.setConnectTimeout(5 * 1000);                con.setReadTimeout(5 * 1000);                File root=Environment.getExternalStorageDirectory();                FileOutputStream fos=new FileOutputStream(root+"/http.jpg");                if(con.getResponseCode()==200){                    is= con.getInputStream();                    int next=0;                    byte[] bytes=new byte[1024];                    while ((next=is.read(bytes))>0){                        fos.write(bytes,0,next);                    }                    fos.flush();                    fos.close();                    return root+"/http.jpg";                }            } catch (MalformedURLException e) {                e.printStackTrace();            } catch (IOException e) {                e.printStackTrace();            } finally {                if(is!=null){                    try {                        is.close();                    } catch (IOException e) {                        e.printStackTrace();                    }                }                if(con!=null){                    con.disconnect();                }            }            return null;        }        @Override        protected void onPostExecute(String s) {            super.onPostExecute(s);            if(s.equals("")){                Toast.makeText(ImageToSdCardActivity.this,"保存路径",Toast.LENGTH_SHORT).show();            }else{                Toast.makeText(ImageToSdCardActivity.this,"保存失败",Toast.LENGTH_SHORT).show();            }        }    }    public void saveHttpImg(View view){        new SaveHttpImg().execute("http://img04.sogoucdn.com/app/a/100520024" +                "/5eec73b7ec46da3a8b132ade22150f7c");    }
效果展示:



更多相关文章

  1. Mac下Android(安卓)Studio简单使用
  2. Android(安卓)Layout之一:FrameLayout
  3. android橡皮擦擦图片功能
  4. 图片加载框架(一):Glide
  5. Android(安卓)五大布局
  6. Android——自定义ImageView实现圆形图片
  7. [置顶] Android(安卓)GridView宫格视图 之 BaseAdapter
  8. Android(安卓)CoordinatorLayout+AppBarLayout+ToolBar实现标题
  9. 【Android】android4.4从图库选择图片,获取图片路径并裁剪

随机推荐

  1. android 正则表达式
  2. Android中的BinderProxy.finalize()或Pla
  3. android 开源项目推荐
  4. android 悬浮窗口
  5. Android(安卓)Studio中配置代码和资源的
  6. Android(安卓)导入多个外部静态链接库, 进
  7. 安卓的真机测试minsdk(API19)>DeviceSdk(1
  8. android 自定义弹出窗口
  9. 小知识:android
  10. 用TextView自定义按钮