android中如何在SD卡上进行文件读写操作
来源:互联网 发布:ubuntu使用搜狗输入法 编辑:程序博客网 时间:2024/04/30 09:38
很多知识只有真正理解掌握之后才能运用自如,举一反三。对Java中的文件操作和android系统SD卡里面的文件操作,你觉得有区别吗,显然没有本质区别,如果勉强说有,那也是不足为道滴,但我们在实际运用中却要注意如下几点,不然问题会缠上你。
1、首先想要对android系统SD卡里文件操作需要添加使用权限:
android系统是不会让外来程序随意动自己内存的,如果没有许可证,不好意思,不准你动我地盘,在我地盘得听我的
。在配置文件里AndroidManifest.xml里面添加SD卡读写数据权限。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
2、获取文件绝对路径:
这里有两种方法
方法1:直接使用/mnt/sdcard/+文件名
如: final String FILE_NAME = "/mnt/sdcard/00000.H264";
方法2:通过系统提供的方法获取SD的路径,然后在后面加上文件名称。这个方法有点啰嗦,暂没感受到它的妙处,不过在此还是罗列出来,供以后参考。见后面实例。
3、获取文件路径后的操作,跳不出对FileInputStream、FileOutputStream、FileReader、FileWriter四个类的使用。具体参看“Java中文件读写 ”篇
下附将数据写入SD卡文件的简单运用代码:
方法1运用代码:
public class FileTestActivity extends Activity
{
final String FILE_NAME = "/mnt/sdcard/007.test";
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button) findViewById(R.id.button1);
final byte[] buf = { 0 };
btn.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0)
{
try
{
FileOutputStream fout = new FileOutputStream(FILE_NAME,
true);
BufferedOutputStream bout = new BufferedOutputStream(fout);
bout.write(buf);
bout.flush();
bout.close();
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
});
}
}
方法2运用实例代码
public class FileTestActivity extends Activity
{
final String FILE_NAME = "/007.test";
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button) findViewById(R.id.button1);
final byte[] buf = { 0 };
btn.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0)
{
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED))
{
File sdDir = Environment.getExternalStorageDirectory();
System.out.println(sdDir);
FileOutputStream fout;
try
{
System.out.println(sdDir.getCanonicalPath() + FILE_NAME);
fout = new FileOutputStream(sdDir.getCanonicalPath()
+ FILE_NAME, true);
BufferedOutputStream bout = new BufferedOutputStream(
fout);
bout.write(buf);
bout.flush();
bout.close();
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
});
}
1、首先想要对android系统SD卡里文件操作需要添加使用权限:
android系统是不会让外来程序随意动自己内存的,如果没有许可证,不好意思,不准你动我地盘,在我地盘得听我的
。在配置文件里AndroidManifest.xml里面添加SD卡读写数据权限。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
2、获取文件绝对路径:
这里有两种方法
方法1:直接使用/mnt/sdcard/+文件名
如: final String FILE_NAME = "/mnt/sdcard/00000.H264";
方法2:通过系统提供的方法获取SD的路径,然后在后面加上文件名称。这个方法有点啰嗦,暂没感受到它的妙处,不过在此还是罗列出来,供以后参考。见后面实例。
3、获取文件路径后的操作,跳不出对FileInputStream、FileOutputStream、FileReader、FileWriter四个类的使用。具体参看“Java中文件读写 ”篇
下附将数据写入SD卡文件的简单运用代码:
方法1运用代码:
public class FileTestActivity extends Activity
{
final String FILE_NAME = "/mnt/sdcard/007.test";
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button) findViewById(R.id.button1);
final byte[] buf = { 0 };
btn.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0)
{
try
{
FileOutputStream fout = new FileOutputStream(FILE_NAME,
true);
BufferedOutputStream bout = new BufferedOutputStream(fout);
bout.write(buf);
bout.flush();
bout.close();
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
});
}
}
方法2运用实例代码
public class FileTestActivity extends Activity
{
final String FILE_NAME = "/007.test";
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button) findViewById(R.id.button1);
final byte[] buf = { 0 };
btn.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0)
{
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED))
{
File sdDir = Environment.getExternalStorageDirectory();
System.out.println(sdDir);
FileOutputStream fout;
try
{
System.out.println(sdDir.getCanonicalPath() + FILE_NAME);
fout = new FileOutputStream(sdDir.getCanonicalPath()
+ FILE_NAME, true);
BufferedOutputStream bout = new BufferedOutputStream(
fout);
bout.write(buf);
bout.flush();
bout.close();
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
});
}
}
注:本博客转载自http://blog.csdn.net/sunnyfans/article/details/7708933
0 0
- android中如何在SD卡上进行文件读写操作
- android中如何在SD卡上进行文件读写操作
- Android中SD卡读写操作
- Android在sd卡中读写联系人
- Android SD卡读写操作
- 在PB中如何打开一个非執行文件
- 在Android中对SD卡进行扇区级读写
- android SD卡上文件操作
- Android开发--SD卡上文件操作
- Android开发SD卡上文件操作
- 从网站上进行文件下载的方法
- Android的SD卡文件读写操作
- Android SD卡文件读写操作
- sd卡读写操作
- Java中通过输入输出流进行文件的读写
- android中读写sd卡文件
- Android开发中SD卡文件读写
- 在SD卡读写
- getsockopt的TCP层实现剖析
- linux 0.11版本内核中信号signal()和sigaction()的区别
- 《一问一世界》 杨澜
- openCV-傅里叶变换
- Oracle EBS R12 - Steps and Issues/Resolutions during R12.1.1 to R12.1.3 Upgration
- android中如何在SD卡上进行文件读写操作
- http文件下载
- SQLPlus命令详细说明
- 【POJ】【P3237】【Tree】【题解】【树链剖分+线段树】
- jquery 键盘enter事件的小例子
- 如何养成读书的好习惯
- u-boot-2009.08在飞凌OK2440/FL2440上的移植(四)——让u-boot支持nand flash识别与操作
- 36 双端堆
- 【OpenCV入门教程之十二】OpenCV边缘检测:Canny算子,Sobel算子,Laplace算子,Scharr滤波器合辑