中查找“解析如何获取SDCard 内存”更多相关内容
中查找“解析如何获取SDCard 内存”更多相关内容- ·上一篇文章:电脑上安装安卓模拟器,运行apk文件
- ·下一篇文章:已经没有了
解析如何获取SDCard 内存
1、讲述 Environment 类。
2、讲述 StatFs 类。
3、完整例子读取 SDCard 内存
1、讲述 Environment 类
Environment 是一个提供访问环境变量的类。
Environment 包含常量:
MEDIA_BAD_REMOVAL
解释:返回getExternalStorageState() ,表明SDCard 被卸载前己被移除
MEDIA_CHECKING
解释:返回getExternalStorageState() ,表明对象正在磁盘检查。
MEDIA_MOUNTED
解释:返回getExternalStorageState() ,表明对象是否存在并具有读/写权限
MEDIA_MOUNTED_READ_ONLY
解释:返回getExternalStorageState() ,表明对象权限为只读
MEDIA_NOFS
解释:返回getExternalStorageState() ,表明对象为空白或正在使用不受支持的文件系统。
MEDIA_REMOVED
解释:返回getExternalStorageState() ,如果不存在 SDCard 返回
MEDIA_SHARED
解释:返回getExternalStorageState() ,如果 SDCard 未安装 ,并通过 USB 大容量存储共享 返回
MEDIA_UNMOUNTABLE
解释:返回getExternalStorageState() ,返回 SDCard 不可被安装 如果 SDCard 是存在但不可以被安装
MEDIA_UNMOUNTED
解释:返回getExternalStorageState() ,返回 SDCard 已卸掉如果 SDCard 是存在但是没有被安装
Environment 常用方法:
方法:getDataDirectory()
解释:返回 File ,获取 Android 数据目录。
方法:getDownloadCacheDirectory()
解释:返回 File ,获取 Android 下载/缓存内容目录。
方法:getExternalStorageDirectory()
解释:返回 File ,获取外部存储目录即 SDCard
方法:getExternalStoragePublicDirectory(String type)
解释:返回 File ,取一个高端的公用的外部存储器目录来摆放某些类型的文件
方法:getExternalStorageState()
解释:返回 File ,获取外部存储设备的当前状态
方法:getRootDirectory()
解释:返回 File ,获取 Android 的根目录
2、讲述 StatFs 类
StatFs 一个模拟linux的df命令的一个类,获得SD卡和手机内存的使用情况
StatFs 常用方法:
getAvailableBlocks()
解释:返回 Int ,获取当前可用的存储空间
getBlockCount()
解释:返回 Int ,获取该区域可用的文件系统数
getBlockSize()
解释:返回 Int ,大小,以字节为单位,一个文件系统
getFreeBlocks()
解释:返回 Int ,该块区域剩余的空间
restat(String path)
解释:执行一个由该对象所引用的文件系统
3、完整例子读取 SDCard 内存
存储卡在 Android 手机上是可以随时插拔的,每次的动作都对引起操作系统进行 ACTION_BROADCAST,本例子将使用上面学到的方法,计算出 SDCard 的剩余容量和总容量。代码如下:
package com.terry;
import java.io.File;
import java.text.DecimalFormat;
import android.R.integer;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.os.StatFs;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
public class getStorageActivity extends Activity {
private Button myButton;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findView();
viewHolder.myButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
getSize();
}
});
}
void findView(){
viewHolder.myButton=(Button)findViewById(R.id.Button01);
viewHolder.myBar=(ProgressBar)findViewById(R.id.myProgressBar);
viewHolder.myTextView=(TextView)findViewById(R.id.myTextView);
}
void getSize(){
viewHolder.myTextView.setText("");
viewHolder.myBar.setProgress(0);
//判断是否有插入存储卡
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
File path =Environment.getExternalStorageDirectory();
//取得sdcard文件路径
StatFs statfs=new StatFs(path.getPath());
//获取block的SIZE
long blocSize=statfs.getBlockSize();
//获取BLOCK数量
long totalBlocks=statfs.getBlockCount();
//己使用的Block的数量
long availaBlock=statfs.getAvailableBlocks();
String[] total=filesize(totalBlocks*blocSize);
String[] availale=filesize(availaBlock*blocSize);
//设置进度条的最大值
int maxValue=Integer.parseInt(availale[0])
*viewHolder.myBar.getMax()/Integer.parseInt(total[0]);
viewHolder.myBar.setProgress(maxValue);
String Text="总共:"+total[0]+total[1]+"\n"
+"可用:"+availale[0]+availale[1];
viewHolder.myTextView.setText(Text);
}else if(Environment.getExternalStorageState().equals(Environment.MEDIA_REMOVED)){
Toast.makeText(getStorageActivity.this, "没有sdCard", 1000).show();
}
}
//返回数组,下标1代表大小,下标2代表单位 KB/MB
String[] filesize(long size){
String str="";
if(size>=1024){
str="KB";
size/=1024;
if(size>=1024){
str="MB";
size/=1024;
}
}
DecimalFormat formatter=new DecimalFormat();
formatter.setGroupingSize(3);
String result[] =new String[2];
result[0]=formatter.format(size);
result[1]=str;
return result;
}
}
android自动获取SD卡中音乐列表作者:Zhukoo 来源:博客园 发布时间:2011-09-25 20:16 阅读:90 次 原文链接 [收藏]
首先大家要了解MediaStore这个类,MediaStore是android系统提供的一个多媒体数据库,android中多媒体信息都可以从这里提取。这个MediaStore包括了多媒体数据库的所有信息,包括音频,视频和图像,android把所有的多媒体数据库接口进行了封装,所有的数据库不用自己进行创建,直接调用利用ContentResolver去掉用那些封装好的接口就可以进行数据库的操作了。
以下是我测试的代码:
main.xml 这个没什么意思
View Code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:text="Button1"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:text="Button2"
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
MediaContentproviderActivity.java 类
View Code
1 package hzx.one;
2
3 import hzx.three.Song;
4 import java.util.ArrayList;
5
6 import android.app.Activity;
7 import android.content.BroadcastReceiver;
8 import android.content.Context;
9 import android.content.Intent;
10 import android.content.IntentFilter;
11 import android.database.Cursor;
12 import android.net.Uri;
13 import android.os.Bundle;
14 import android.os.Environment;
15 import android.provider.MediaStore;
16 import android.util.Log;
17 import android.view.View;
18 import android.widget.Button;
19
20 public class MediaContentproviderActivity extends Activity {
21 private Button button1;
22 private Button button2;
23 private static final String TAG="showsong";
24 @Override
25 public void onCreate(Bundle savedInstanceState) {
26 super.onCreate(savedInstanceState);
27 setContentView(R.layout.main);
28 button1=(Button) findViewById(R.id.button1);
29 button1.setOnClickListener(new View.OnClickListener() {
30 @Override
31 public void onClick(View v) {
32 // TODO Auto-generated method stub
33 //Log.d(TAG, readDataFromSD(MediaContentproviderActivity.this).toString());
34 readDataFromSD(MediaContentproviderActivity.this);
35 }
36 });
37 button2=(Button) findViewById(R.id.button2);
38 button2.setOnClickListener(new View.OnClickListener() {
39 @Override
40 public void onClick(View v) {
41 // TODO Auto-generated method stub
42 //Log.d(TAG, readDataFromSD(MediaContentproviderActivity.this).toString());
43 Log.d(TAG,"update list");
44 scanSdCard();
45 }
46 });
47 }
48 public static ArrayList<Song> readDataFromSD(Context context){
49 Log.d(TAG, "scanFile");
50 Cursor cursor = context.getContentResolver().query(
51 MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
52 new String[] { MediaStore.Audio.Media._ID, MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.DURATION, MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.ALBUM, MediaStore.Audio.Media.YEAR, MediaStore.Audio.Media.MIME_TYPE, MediaStore.Audio.Media.SIZE, MediaStore.Audio.Media.DATA}
53 , MediaStore.Audio.Media.MIME_TYPE+"=? or " + MediaStore.Audio.Media.MIME_TYPE+"=?", new String[]{"audio/mpeg","audio/x-ms-wma"},null);
54
55 while (cursor.moveToNext()) {
56 Log.d(TAG, "show--->"+cursor.getString(1));
57 }
58 return null;
59 }
60 private class ScanSdFilesReceiver extends BroadcastReceiver {
61 @Override
62 public void onReceive(Context arg0, Intent arg1) {
63 // TODO Auto-generated method stub
64 String action = arg1.getAction();
65 if (Intent.ACTION_MEDIA_SCANNER_STARTED.equals(action)) {
66 //当系统开始扫描sd卡时,为了用户体验,可以加上一个等待框
67 //scanHandler.sendEmptyMessage(STARTED);
68 Log.d(TAG, "update--->started");
69 }
70 if (Intent.ACTION_MEDIA_SCANNER_FINISHED.equals(action)) {
71 //当系统扫描完毕时,停止显示等待框,并重新查询ContentProvider
72 //scanHandler.sendEmptyMessage(FINISHED);
73 Log.d(TAG, "update--->finished");
74 }
75 }
76 }
77 private void scanSdCard() {
78 Log.d(TAG, "intent---> start");
79 IntentFilter intentFilter = new IntentFilter(Intent.ACTION_MEDIA_SCANNER_STARTED);
80 intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
81 intentFilter.addDataScheme("file");
82 ScanSdFilesReceiver scanReceiver = new ScanSdFilesReceiver();
83 registerReceiver(scanReceiver, intentFilter);
84 sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
85 Uri.parse("file://" + Environment.getExternalStorageDirectory().getAbsolutePath())));
86 Log.d(TAG, "intent---> finish");
87 }
88 }
