跪求!android 往sd卡里谢文件的源码 就是我有个文件是在assets文件夹里读取的,能显示出来,我想写到sd卡上的某个文件夹里 自己写的老是出错,现跪求一源码,谢谢
解决方案 »
- androidGridView --九宫格显示问题
- 如何把EditText与百度或谷歌的搜索关联???
- R.java中的字段程序无法使用
- android 定时器
- Menu复用的问题请教
- 如何在google map中得到周边信息,比如餐厅,加油站等
- 关于android 9patch 图原理
- 求助,listview的imgebutton显示问题。。
- 我这个是错哪里啦???
- android 高德地图 如何设置InfoWindow窗口的位置
- android 2.2 kernel(2.6.32) 到 android 4.0 kernel(3.0) kernel可以不升级么?
- 想做html5的手机网站,求比较好的html5的手机网站参考~
is = mContext.getAssets().open("test.mp3");
copy(is, "sdcard/test.txt");
//fileTo要拷贝到的文件路径
public static boolean copy(InputStream in, String fileTo) {
try {
// FileInputStream in = new java.io.FileInputStream(fileFrom);
FileOutputStream out = new FileOutputStream(fileTo);
byte[] bt = new byte[1024];
int count;
while ((count = in.read(bt)) > 0) {
out.write(bt, 0, count);
}
in.close();
out.close();
return true;
} catch (IOException ex) {
return false;
}
} sd卡路径自己google一下怎么获取,要判断的。
if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
if(!dataFile.exists()) {
try {
dataFile.createNewFile();
fos = new FileOutputStream(fileName,true);
String title = "content";
fos.write(title.getBytes());
fos.flush();
} catch (IOException e) {
Log.e(Constants.TAG_EXCEPTION, e.toString());
}
}}
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;import org.apache.http.util.EncodingUtils;import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Environment;
import android.widget.TextView;
public class reading extends Activity{
public static final String ENCODING = "UTF-8";
String InputStream=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv2=(TextView)findViewById(R.id.tv2);
tv2.setTextColor(Color.RED);
tv2.setTextSize(15.0f);
InputStream=getFromAssets("option.xml");
System.out.print("--------------读入");
tv2.setText(InputStream);
}
//从assets 文件夹中获取文件并读取数据
public String getFromAssets(String fileName){
String result = "";
try {
InputStream in = getResources().getAssets().open(fileName);
//获取文件的字节数
int lenght = in.available();
//创建byte数组
byte[] buffer = new byte[lenght];
//将文件中的数据读到byte数组中
in.read(buffer);
System.out.print("--------------读入");
result = EncodingUtils.getString(buffer, ENCODING);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/////////////
public class FileUtils {
private String SDCardRoot; public FileUtils() {
// 得到当前外部存储设备的目录
SDCardRoot = Environment.getExternalStorageDirectory()
.getAbsolutePath()
+ "/";
} /**
* 在SD卡上创建文件
*
* @param fileName
* @param dir
* @return
* @throws IOException
*/
public File creaeFileInSDCard(String fileName, String dir)
throws IOException {
File file = new File(SDCardRoot + dir + File.separator + fileName);
file.createNewFile();
return file;
} /**
* 在SD卡上创建目录
*
* @param dir
* @return
*/
public File createSDDir(String dir) {
File dirFile = new File(SDCardRoot + dir);
dirFile.mkdir();
return dirFile;
} /**
* 判断SD卡上的文件夹是否存在
*
* @param fileName
* @param path
* @return
*/
public boolean isFileExist(String fileName, String path) {
File file = new File(SDCardRoot + path + File.separator + fileName);
return file.exists();
} /**
* 将一个InputStream里面的数据写入到SD卡中
*
* @param path 路径
* @param fileName 文件名
* @param inputStream 输入流
* @return
*/
public File write2SDFromInput(String path, String fileName,
InputStream InputStream) {
File file = null;
OutputStream output = null;
try {
createSDDir(path);
file = creaeFileInSDCard(fileName, path);
output = new FileOutputStream(file);
byte buffer[] = new byte[4 * 1024];
int temp;
while ((temp = InputStream.read()) != -1) {
output.write(buffer, 0, temp);
}
output.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
output.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return file;
} } }
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />这个权限必须要有