您的位置:首页 > 移动开发 > Android开发

Android 图片的读取与写入

2016-08-15 19:56 246 查看
完成效果:点击保存按钮,布局中的图片存入到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