Android 图片的读取与写入
来源:互联网 发布:且夫知不知 编辑:程序博客网 时间:2024/06/06 07:29
完成效果:点击保存按钮,布局中的图片存入到SD卡中。点击读取按钮,图片显示到布局下方。
点击获取网络图片按钮,网络图片显示到布局下方。点击保存网络图片按钮,网络图片存入SD卡中。
主布局:
<?xml version="1.0" encoding="utf-8"?><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" tools:context="com.example.administrator.jreduch09.ImageToSdCardActivity"><Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/save" android:text="保存" android:onClick="saveImg" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/read" android:text="读取" android:layout_below="@+id/save" android:onClick="readImg" /> <ImageView android:layout_width="100dp" android:layout_height="100dp" android:id="@+id/showImg" android:layout_below="@+id/img" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/httpImg" android:text="获取网络图片" android:layout_below="@+id/read" android:onClick="getUriImg" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/httpSaveImg" android:text="保存网络图片" android:layout_below="@+id/httpImg" android:onClick="saveHttpImg" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ruzi" android:id="@+id/img" android:layout_below="@+id/httpSaveImg" android:layout_centerHorizontal="true" /></RelativeLayout>布局效果:
代码:
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<String,Void,Bitmap> { @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<String,Void,String>{ @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"); }效果展示:
0 0
- Android 图片的读取与写入
- Android 文件的读取与写入
- android文件的写入与读取
- Android 数据的读取与写入
- Android 数据的读取与写入2
- 在数据库中进行图片的写入与读取
- 【C#】对文本、图片、音频文件的读取与写入
- Java读取与写入图片文件
- android写入数据库、读取sqlite中的图片
- android 文件的读取与写入以及TextView的滚动
- android 文件的读取与写入以及TextView
- Android 私有文件夹 文件的写入与读取
- Unity3D IOS Android支持中文与本地文件的读取写入
- android 私有文件夹 文件的写入与读取
- Android持久化之文件的读取与写入
- android 私有文件夹、文件的写入与读取
- Android中对SD卡的读取与写入
- Android 私有文件夹 文件的写入与读取
- TabLayout添加图片
- 8-15ASR
- 2016.08.15【初中部 NOIP提高组 】模拟赛C
- java面试的知识点
- cpufreq之ondemand governer的实现
- Android 图片的读取与写入
- Xilinx Virtex6 GTX Transceiver设计总结
- UVA 562 Dividing coins (01背包)
- [HDU 4609] 3-idiots (计数+FFT优化卷积)
- 【学习笔记】抽象类
- 视频播放功能模块
- 大整数-综述-加法
- UIView中的各种坐标转换convertPoint:toView OR convertPoint:fromView
- HTML5基础篇之html(二)