Android 开发笔记___SD卡基本操作__图片读取写入

  1 package com.example.alimjan.hello_world.Utils;
  2 
  3 import android.graphics.Bitmap;
  4 import android.graphics.BitmapFactory;
  5 
  6 import java.io.BufferedInputStream;
  7 import java.io.BufferedOutputStream;
  8 import java.io.File;
  9 import java.io.FileInputStream;
 10 import java.io.FileOutputStream;
 11 import java.io.FilenameFilter;
 12 import java.util.ArrayList;
 13 import java.util.Locale;
 14 
 15 public class FileUtil {
 16 
 17     public static void saveText(String path, String txt) {
 18         try {
 19             FileOutputStream fos = new FileOutputStream(path);
 20             fos.write(txt.getBytes());
 21             fos.close();
 22         } catch (Exception e) {
 23             e.printStackTrace();
 24         }
 25     }
 26 
 27     public static String openText(String path) {
 28         String readStr = "";
 29         try {
 30             FileInputStream fis = new FileInputStream(path);
 31             byte[] b = new byte[fis.available()];
 32             fis.read(b);
 33             readStr = new String(b);
 34             fis.close();
 35         } catch (Exception e) {
 36             e.printStackTrace();
 37         }
 38         return readStr;
 39     }
 40 
 41     public static void saveImage(String path, Bitmap bitmap) {
 42         try {
 43             BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(path));
 44             bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos);
 45             bos.flush();
 46             bos.close();
 47         } catch (Exception e) {
 48             e.printStackTrace();
 49         }
 50     }
 51     
 52     public static Bitmap openImage(String path) {
 53         Bitmap bitmap = null;
 54         try {
 55             BufferedInputStream bis = new BufferedInputStream(new FileInputStream(path));
 56             bitmap = BitmapFactory.decodeStream(bis);
 57             bis.close();
 58         } catch (Exception e) {
 59             e.printStackTrace();
 60         }
 61         return bitmap;
 62     }
 63     
 64     public static ArrayList<File> getFileList(String path, String[] extendArray) {
 65         ArrayList<File> displayedContent = new ArrayList<File>();
 66         File[] files = null;
 67         File directory = new File(path);
 68         if (extendArray != null && extendArray.length>0) {
 69             FilenameFilter fileFilter = getTypeFilter(extendArray);
 70             files = directory.listFiles(fileFilter);
 71         } else {
 72             files = directory.listFiles();
 73         }
 74 
 75         if (files != null) {
 76             for (File f : files) {
 77                 if (!f.isDirectory() && !f.isHidden()) {
 78                     displayedContent.add(f);
 79                 }
 80             }
 81         }
 82         return displayedContent;
 83     }
 84 
 85     public static FilenameFilter getTypeFilter(String[] extendArray) {
 86         final ArrayList<String> fileExtensions = new ArrayList<String>();
 87         for (int i=0; i<extendArray.length; i++) {
 88             fileExtensions.add(extendArray[i]);
 89         }
 90         FilenameFilter fileNameFilter = new FilenameFilter() {
 91             @Override
 92             public boolean accept(File directory, String fileName) {
 93                 boolean matched = false;
 94                 File f = new File(String.format("%s/%s",
 95                         directory.getAbsolutePath(), fileName));
 96                 matched = f.isDirectory();
 97                 if (!matched) {
 98                     for (String s : fileExtensions) {
 99                         s = String.format(".{0,}\\%s$", s);
100                         s = s.toUpperCase(Locale.getDefault());
101                         fileName = fileName.toUpperCase(Locale.getDefault());
102                         matched = fileName.matches(s);
103                         if (matched) {
104                             break;
105                         }
106                     }
107                 }
108                 return matched;
109             }
110         };
111         return fileNameFilter;
112     }
113 
114 }
  1 package com.example.alimjan.hello_world;
  2 
  3 /**
  4  * Created by alimjan on 7/5/2017.
  5  */
  6 
  7         import android.content.Context;
  8         import android.content.Intent;
  9         import android.graphics.Bitmap;
 10         import android.os.Bundle;
 11         import android.os.Environment;
 12         import android.support.v7.app.AppCompatActivity;
 13         import android.view.View;
 14         import android.view.View.OnClickListener;
 15         import android.widget.AdapterView;
 16         import android.widget.ArrayAdapter;
 17         import android.widget.EditText;
 18         import android.widget.LinearLayout;
 19         import android.widget.Spinner;
 20         import android.widget.TextView;
 21         import android.widget.Toast;
 22         import android.widget.AdapterView.OnItemSelectedListener;
 23 
 24         import com.example.alimjan.hello_world.Utils.DateUtil;
 25         import com.example.alimjan.hello_world.Utils.FileUtil;
 26 
 27 
 28 public class class_4_3_3 extends AppCompatActivity implements OnClickListener {
 29 
 30     private LinearLayout ll_info;
 31     private EditText et_name;
 32     private EditText et_age;
 33     private EditText et_height;
 34     private EditText et_weight;
 35     private boolean bMarried = false;
 36 
 37     private String mPath;
 38     private TextView tv_path;
 39 
 40     @Override
 41     protected void onCreate(Bundle savedInstanceState) {
 42         super.onCreate(savedInstanceState);
 43         setContentView(R.layout.code_4_3_3);
 44         ll_info = (LinearLayout) findViewById(R.id.ll_info);
 45         et_name = (EditText) findViewById(R.id.et_name);
 46         et_age = (EditText) findViewById(R.id.et_age);
 47         et_height = (EditText) findViewById(R.id.et_height);
 48         et_weight = (EditText) findViewById(R.id.et_weight);
 49         tv_path = (TextView) findViewById(R.id.tv_path);
 50         findViewById(R.id.btn_save).setOnClickListener(this);
 51 
 52         ArrayAdapter<String> typeAdapter = new ArrayAdapter<String>(this,
 53                 R.layout.item_select, typeArray);
 54         typeAdapter.setDropDownViewResource(R.layout.item_dropdown);
 55         Spinner sp_married = (Spinner) findViewById(R.id.sp_married);
 56         sp_married.setPrompt("请选择婚姻状况");
 57         sp_married.setAdapter(typeAdapter);
 58         sp_married.setSelection(0);
 59         sp_married.setOnItemSelectedListener(new TypeSelectedListener());
 60 
 61         mPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/";
 62     }
 63 
 64     private String[] typeArray = {"未婚", "已婚"};
 65     class TypeSelectedListener implements OnItemSelectedListener {
 66         public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
 67             bMarried = (arg2==0)?false:true;
 68         }
 69 
 70         public void onNothingSelected(AdapterView<?> arg0) {
 71         }
 72     }
 73 
 74     @Override
 75     public void onClick(View v) {
 76         if (v.getId() == R.id.btn_save) {
 77             String name = et_name.getText().toString();
 78             String age = et_age.getText().toString();
 79             String height = et_height.getText().toString();
 80             String weight = et_weight.getText().toString();
 81             if (name==null || name.length()<=0) {
 82                 showToast("请先填写姓名");
 83                 return;
 84             }
 85             if (age==null || age.length()<=0) {
 86                 showToast("请先填写年龄");
 87                 return;
 88             }
 89             if (height==null || height.length()<=0) {
 90                 showToast("请先填写身高");
 91                 return;
 92             }
 93             if (weight==null || weight.length()<=0) {
 94                 showToast("请先填写体重");
 95                 return;
 96             }
 97 
 98             if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) == true) {
 99                 Bitmap bitmap = ll_info.getDrawingCache();
100                 String file_path = mPath + DateUtil.getCurDateStr("") + ".png";
101                 FileUtil.saveImage(file_path, bitmap);
102                 bitmap.recycle();
103                 tv_path.setText("用户注册信息图片的保存路径为:\n"+file_path);
104                 showToast("图片已存入SD卡文件");
105             } else {
106                 showToast("未发现已挂载的SD卡,请检查");
107             }
108         }
109     }
110 
111     @Override
112     protected void onStart() {
113         super.onStart();
114         ll_info.setDrawingCacheEnabled(true);
115     }
116 
117     @Override
118     protected void onStop() {
119         super.onStop();
120         ll_info.setDrawingCacheEnabled(false);
121     }
122 
123     private void showToast(String desc) {
124         Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
125     }
126 
127     public static void startHome(Context mcontext){
128         Intent intent = new Intent(mcontext,class_4_3_3.class);
129         mcontext.startActivity(intent);
130     }
131 
132 }
  1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2     android:layout_width="match_parent"
  3     android:layout_height="match_parent"
  4     android:focusable="true"
  5     android:focusableInTouchMode="true"
  6     android:orientation="vertical"
  7     android:padding="10dp" >
  8 
  9     <LinearLayout
 10         android:id="@+id/ll_info"
 11         android:layout_width="match_parent"
 12         android:layout_height="wrap_content"
 13         android:background="#f9f9f9"
 14         android:orientation="vertical" >
 15 
 16         <RelativeLayout
 17             android:layout_width="match_parent"
 18             android:layout_height="50dp" >
 19 
 20             <TextView
 21                 android:id="@+id/tv_name"
 22                 android:layout_width="wrap_content"
 23                 android:layout_height="match_parent"
 24                 android:layout_alignParentLeft="true"
 25                 android:gravity="center"
 26                 android:text="姓名:"
 27                 android:textColor="@color/black"
 28                 android:textSize="17sp" />
 29 
 30             <EditText
 31                 android:id="@+id/et_name"
 32                 android:layout_width="match_parent"
 33                 android:layout_height="match_parent"
 34                 android:layout_marginBottom="5dp"
 35                 android:layout_marginTop="5dp"
 36                 android:layout_toRightOf="@+id/tv_name"
 37                 android:background="@drawable/editext_selector"
 38                 android:gravity="left|center"
 39                 android:hint="请输入姓名"
 40                 android:inputType="text"
 41                 android:maxLength="12"
 42                 android:textColor="@color/black"
 43                 android:textColorHint="@color/grey"
 44                 android:textCursorDrawable="@drawable/text_cursor"
 45                 android:textSize="17sp" />
 46         </RelativeLayout>
 47 
 48         <RelativeLayout
 49             android:layout_width="match_parent"
 50             android:layout_height="50dp" >
 51 
 52             <TextView
 53                 android:id="@+id/tv_age"
 54                 android:layout_width="wrap_content"
 55                 android:layout_height="match_parent"
 56                 android:layout_alignParentLeft="true"
 57                 android:gravity="center"
 58                 android:text="年龄:"
 59                 android:textColor="@color/black"
 60                 android:textSize="17sp" />
 61 
 62             <EditText
 63                 android:id="@+id/et_age"
 64                 android:layout_width="match_parent"
 65                 android:layout_height="match_parent"
 66                 android:layout_marginBottom="5dp"
 67                 android:layout_marginTop="5dp"
 68                 android:layout_toRightOf="@+id/tv_age"
 69                 android:background="@drawable/editext_selector"
 70                 android:gravity="left|center"
 71                 android:hint="请输入年龄"
 72                 android:inputType="number"
 73                 android:maxLength="2"
 74                 android:textColor="@color/black"
 75                 android:textColorHint="@color/grey"
 76                 android:textCursorDrawable="@drawable/text_cursor"
 77                 android:textSize="17sp" />
 78         </RelativeLayout>
 79 
 80         <RelativeLayout
 81             android:layout_width="match_parent"
 82             android:layout_height="50dp" >
 83 
 84             <TextView
 85                 android:id="@+id/tv_height"
 86                 android:layout_width="wrap_content"
 87                 android:layout_height="match_parent"
 88                 android:layout_alignParentLeft="true"
 89                 android:gravity="center"
 90                 android:text="身高:"
 91                 android:textColor="@color/black"
 92                 android:textSize="17sp" />
 93 
 94             <EditText
 95                 android:id="@+id/et_height"
 96                 android:layout_width="match_parent"
 97                 android:layout_height="match_parent"
 98                 android:layout_marginBottom="5dp"
 99                 android:layout_marginTop="5dp"
100                 android:layout_toRightOf="@+id/tv_height"
101                 android:background="@drawable/editext_selector"
102                 android:gravity="left|center"
103                 android:hint="请输入身高"
104                 android:inputType="number"
105                 android:maxLength="3"
106                 android:textColor="@color/black"
107                 android:textColorHint="@color/grey"
108                 android:textCursorDrawable="@drawable/text_cursor"
109                 android:textSize="17sp" />
110         </RelativeLayout>
111 
112         <RelativeLayout
113             android:layout_width="match_parent"
114             android:layout_height="50dp" >
115 
116             <TextView
117                 android:id="@+id/tv_weight"
118                 android:layout_width="wrap_content"
119                 android:layout_height="match_parent"
120                 android:layout_alignParentLeft="true"
121                 android:gravity="center"
122                 android:text="体重:"
123                 android:textColor="@color/black"
124                 android:textSize="17sp" />
125 
126             <EditText
127                 android:id="@+id/et_weight"
128                 android:layout_width="match_parent"
129                 android:layout_height="match_parent"
130                 android:layout_marginBottom="5dp"
131                 android:layout_marginTop="5dp"
132                 android:layout_toRightOf="@+id/tv_weight"
133                 android:background="@drawable/editext_selector"
134                 android:gravity="left|center"
135                 android:hint="请输入体重"
136                 android:inputType="numberDecimal"
137                 android:maxLength="5"
138                 android:textColor="@color/black"
139                 android:textColorHint="@color/grey"
140                 android:textCursorDrawable="@drawable/text_cursor"
141                 android:textSize="17sp" />
142         </RelativeLayout>
143 
144         <RelativeLayout
145             android:layout_width="match_parent"
146             android:layout_height="50dp" >
147 
148             <TextView
149                 android:id="@+id/tv_married"
150                 android:layout_width="wrap_content"
151                 android:layout_height="match_parent"
152                 android:layout_alignParentLeft="true"
153                 android:gravity="center"
154                 android:text="婚否:"
155                 android:textColor="@color/black"
156                 android:textSize="17sp" />
157 
158             <Spinner
159                 android:id="@+id/sp_married"
160                 android:layout_width="match_parent"
161                 android:layout_height="match_parent"
162                 android:layout_toRightOf="@+id/tv_married"
163                 android:gravity="left|center"
164                 android:spinnerMode="dialog" />
165         </RelativeLayout>
166     </LinearLayout>
167 
168     <Button
169         android:id="@+id/btn_save"
170         android:layout_width="match_parent"
171         android:layout_height="wrap_content"
172         android:text="保存图片到SD卡"
173         android:textColor="@color/black"
174         android:textSize="20sp" />
175 
176     <TextView
177         android:id="@+id/tv_path"
178         android:layout_width="wrap_content"
179         android:layout_height="match_parent"
180         android:textColor="@color/black"
181         android:textSize="17sp" />
182 
183 </LinearLayout>

 

  1 package com.example.alimjan.hello_world;
  2 
  3 import java.io.File;
  4 import java.util.ArrayList;
  5 
  6 /**
  7  * Created by alimjan on 7/5/2017.
  8  */
  9 
 10 import android.content.Context;
 11 import android.content.Intent;
 12 import android.graphics.Bitmap;
 13         import android.os.Bundle;
 14         import android.os.Environment;
 15         import android.support.v7.app.AppCompatActivity;
 16         import android.util.Log;
 17         import android.view.View;
 18         import android.view.View.OnClickListener;
 19         import android.widget.AdapterView;
 20         import android.widget.ArrayAdapter;
 21         import android.widget.Spinner;
 22         import android.widget.ImageView;
 23         import android.widget.Toast;
 24         import android.widget.AdapterView.OnItemSelectedListener;
 25 
 26         import com.example.alimjan.hello_world.Utils.FileUtil;
 27 
 28 /**
 29  * Created by ouyangshen on 2016/10/1.
 30  */
 31 public class class_4_3_3_1 extends AppCompatActivity implements OnClickListener {
 32 
 33     private final static String TAG = "ImageReadActivity";
 34     private ImageView iv_image;
 35     private Spinner sp_file;
 36     private String mPath;
 37 
 38     @Override
 39     protected void onCreate(Bundle savedInstanceState) {
 40         super.onCreate(savedInstanceState);
 41         setContentView(R.layout.code_4_3_3_1);
 42         iv_image = (ImageView) findViewById(R.id.iv_image);
 43         sp_file = (Spinner) findViewById(R.id.sp_file);
 44         findViewById(R.id.btn_delete).setOnClickListener(this);
 45         mPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/";
 46         if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) == true) {
 47             refreshSpinner();
 48         } else {
 49             showToast("未发现已挂载的SD卡,请检查");
 50         }
 51     }
 52 
 53     private void refreshSpinner() {
 54         ArrayList<File> fileAlllist = FileUtil.getFileList(mPath, new String[]{".png", ".jpg"});
 55         if (fileAlllist.size() > 0) {
 56             fileArray = new String[fileAlllist.size()];
 57             for (int i=0; i<fileAlllist.size(); i++) {
 58                 fileArray[i] = fileAlllist.get(i).getName();
 59             }
 60             ArrayAdapter<String> typeAdapter = new ArrayAdapter<String>(this,
 61                     R.layout.item_select, fileArray);
 62             typeAdapter.setDropDownViewResource(R.layout.item_dropdown);
 63             sp_file.setPrompt("请选择图片文件");
 64             sp_file.setAdapter(typeAdapter);
 65             sp_file.setSelection(0);
 66             sp_file.setOnItemSelectedListener(new FileSelectedListener());
 67         } else {
 68             fileArray = null;
 69             fileArray = new String[1];
 70             fileArray[0] = "";
 71             ArrayAdapter<String> typeAdapter = new ArrayAdapter<String>(this,
 72                     R.layout.item_select, fileArray);
 73             sp_file.setPrompt(null);
 74             sp_file.setAdapter(typeAdapter);
 75             sp_file.setOnItemSelectedListener(null);
 76             iv_image.setImageDrawable(null);
 77         }
 78     }
 79 
 80     private String[] fileArray;
 81     class FileSelectedListener implements OnItemSelectedListener {
 82         public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
 83             String file_path = mPath + fileArray[arg2];
 84             Bitmap bitmap = FileUtil.openImage(file_path);
 85             iv_image.setImageBitmap(bitmap);
 86         }
 87 
 88         public void onNothingSelected(AdapterView<?> arg0) {
 89         }
 90     }
 91 
 92     @Override
 93     public void onClick(View v) {
 94         if (v.getId() == R.id.btn_delete) {
 95             for (int i=0; i<fileArray.length; i++) {
 96                 String file_path = mPath + fileArray[i];
 97                 File f = new File(file_path);
 98                 boolean result = f.delete();
 99                 if (result != true) {
100                     Log.d(TAG, "file_path="+file_path+", delete failed");
101                 }
102             }
103             refreshSpinner();
104             showToast("已删除临时目录下的所有图片文件");
105         }
106     }
107 
108     private void showToast(String desc) {
109         Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
110     }
111 
112     public static void startHome(Context mcontext){
113         Intent intent = new Intent(mcontext,class_4_3_3_1.class);
114         mcontext.startActivity(intent);
115     }
116 
117 }
 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:layout_width="match_parent"
 3     android:layout_height="match_parent"
 4     android:focusable="true"
 5     android:focusableInTouchMode="true"
 6     android:orientation="vertical"
 7     android:padding="10dp" >
 8 
 9     <Button
10         android:id="@+id/btn_delete"
11         android:layout_width="match_parent"
12         android:layout_height="wrap_content"
13         android:text="删除所有图片文件"
14         android:textColor="@color/black"
15         android:textSize="20sp" />
16 
17     <RelativeLayout
18         android:layout_width="match_parent"
19         android:layout_height="50dp" >
20 
21         <TextView
22             android:id="@+id/tv_file"
23             android:layout_width="wrap_content"
24             android:layout_height="match_parent"
25             android:layout_alignParentLeft="true"
26             android:gravity="center"
27             android:text="文件名:"
28             android:textColor="@color/black"
29             android:textSize="17sp" />
30 
31         <Spinner
32             android:id="@+id/sp_file"
33             android:layout_width="match_parent"
34             android:layout_height="match_parent"
35             android:layout_toRightOf="@+id/tv_file"
36             android:gravity="left|center"
37             android:spinnerMode="dialog" />
38     </RelativeLayout>
39 
40     <ImageView
41         android:id="@+id/iv_image"
42         android:layout_width="match_parent"
43         android:layout_height="wrap_content"
44         android:scaleType="fitCenter" />
45 
46 </LinearLayout>

 

posted @ 2017-07-05 17:43  alm  阅读(341)  评论(0编辑  收藏  举报