SD Card 外部存儲使用詳解

和你一起終身學習,這里是程序員Android
經(jīng)典好文推薦,通過閱讀本文,您將收獲以下知識點:
一、保存外部存儲需要申請權(quán)限
二、外部存儲使用案例(保存,讀取,刪除圖片)
一、 保存外部存儲需要申請權(quán)限
Android設(shè)備支持外部存儲,比如SD卡等,保存在外部存儲的數(shù)據(jù)具有全局可讀性,可供在其他設(shè)備比如電腦上閱讀,修改等。使用外部存儲需要獲取外部存儲的訪問權(quán)限
。
這個很重要,不然無法操作SD 卡,
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
二、外部存儲使用案例(保存,讀取,刪除圖片)
1. 實現(xiàn)效果

外部存儲保存圖片的方法
2. 判斷是否掛載 SD 卡方法
/**
* 1.判斷SD卡是否掛載
* **/
public static boolean isMounted() {
String state = Environment.getExternalStorageState();
return state.equals(Environment.MEDIA_MOUNTED);
}
SD 保存圖片,刪除圖片、顯示圖片的方法
3. 保存圖片到SD卡
保存圖片到SD卡 實現(xiàn)代碼如下:
// 保存圖片的方法
public void BtnSaveImage(View view) {
// 獲取圖片類型 bitmap
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// 將bitmap 壓縮成byte類型 并保存到outputstream中
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
bitmap.recycle();
boolean saveimg = SaveImg(getApplicationContext(), "photo.png",
baos.toByteArray());
if (saveimg) {
Toast.makeText(getApplicationContext(), "保存成功" + store_path,
Toast.LENGTH_SHORT).show();
}
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 保存圖片的方法
public static boolean SaveImg(Context context, String filename, byte[] data) {
// 判斷是否掛載SD卡
if (!isMounted()) {
Toast.makeText(context, "SD卡未安裝", Toast.LENGTH_SHORT).show();
return false;
}
File dir = new File(store_path);
// 創(chuàng)建文件目錄
if (!dir.exists()) {
dir.mkdirs();
}
try {
// 向文件目錄 dir中寫文件filename
FileOutputStream fos = new FileOutputStream(new File(dir, filename));
fos.write(data);
fos.close();
return true;
} catch (IOException e) {
e.printStackTrace();
Log.i("TAG", "IOException..." + e);
return false;
}
}
4. 刪除圖片的方法
刪除圖片 代碼實現(xiàn)代碼實現(xiàn)如下:
public void BtnDeleteImage(View view) {
DeletleImg(getApplicationContext(), "photo.png");
}
// 刪除圖片
public static void DeletleImg(Context context, String filename) {
File dirfile = new File(store_path + filename);
// 判斷文件是否存在
if (!dirfile.exists()) {
Toast.makeText(context, "文件不存在", Toast.LENGTH_SHORT).show();
return;
}
if (dirfile.isDirectory()) {
String[] childdir = dirfile.list();
for (int i = 0; i < childdir.length; i++) {
new File(dirfile, childdir[i]).delete();
}
}
dirfile.delete();
}
5.讀取顯示圖片的方法
讀取顯示圖片代碼實現(xiàn)如下:
// 讀取圖片
public void BtnReadImage(View view) {
Bitmap readImg = ReadImg(getApplicationContext(), "photo.png");
if (readImg == null) {
Toast.makeText(getApplicationContext(), "讀取失敗" + store_path,
Toast.LENGTH_SHORT).show();
} else {
((ImageView) findViewById(R.id.img_external))
.setImageBitmap(readImg);
}
}
// 讀取圖片
public static Bitmap ReadImg(Context context, String filename) {
// 判斷是否掛載SD卡
if (!isMounted()) {
Toast.makeText(context, "SD卡未安裝", Toast.LENGTH_SHORT).show();
return null;
}
// 獲取文件路徑下的文件名稱
File imgFile = new File(store_path, filename);
if (imgFile.exists()) {
Log.i("TAG", "imgFile" + imgFile.getAbsolutePath());
// 將路徑下的文件轉(zhuǎn)換成 bitmap
return BitmapFactory.decodeFile(imgFile.getAbsolutePath());
} else {
Toast.makeText(context, "文件不存在", Toast.LENGTH_SHORT).show();
}
return null;
}
6. 布局如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/img_external"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_external_save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="BtnSaveImage"
android:text="保存圖片到SD卡" />
<Button
android:id="@+id/btn_external_delete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="BtnDeleteImage"
android:text="刪除SD卡 圖片" />
<Button
android:id="@+id/btn_external_read"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="BtnReadImage"
android:text="顯示SD卡 圖片" />
LinearLayout>
至此,本篇已結(jié)束。轉(zhuǎn)載網(wǎng)絡(luò)的文章,小編覺得很優(yōu)秀,歡迎點擊閱讀原文,支持原創(chuàng)作者,如有侵權(quán),懇請聯(lián)系小編刪除。同時感謝您的閱讀,期待您的關(guān)注。
評論
圖片
表情
