Android 图片的读取与写入
2016-08-15 19:56
246 查看
完成效果:点击保存按钮,布局中的图片存入到SD卡中。点击读取按钮,图片显示到布局下方。
点击获取网络图片按钮,网络图片显示到布局下方。点击保存网络图片按钮,网络图片存入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, 4000 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"); }效果展示:
相关文章推荐
- android 向数据库写入图片信息 读取图片信息
- android写入数据库、读取sqlite中的图片
- android 读取,写入图片到sd卡源码
- Android从文件目录中写入和读取图片
- android 文件的读取与写入以及TextView
- ASP.NET针对MSSQL里面的IMAGE字段的图片写入和读取操作代码
- 将图片写入数据库和读取
- c# winform下sql图片二进制存储/读取/显示/写入XML/读取XML显示
- 从数据库中读取图片文件和将图片和写入到数据库中
- android根据不同的分辨率来读取图片
- 往sqlite中写入和读取图片二进制数据的代码
- 写入和读取图片(c# asp.net sqlserver)
- c# 图片二进制存储SQL/读取SQL/显示Image/写入XML/读取XML显示
- 写入和读取图片(c# asp.net sqlserver)[转]
- android 从sdcard 读取图片并进行剪切,粘贴操作
- Android对文件的操作(简单的文件读取与写入)
- 写入和读取图片(c# asp.net sqlserver)
- 在数据库中进行图片的写入与读取
- 写入和读取图片(c# asp.net sqlserver)
- 用JAVA写入和读取MYSQL的图片的实例