android 文件的操作 android 的应用程序实现,由/sdcard/要拷贝的文件XXXX.X 到/system/XXXX 下,代码怎么样实现,要注意哪些操作!谢谢!!Android应用 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 java 文件流操作会吧,网上一堆代码需要注意的是估计app没有权限写到system/XXX下面 这是我实现的方法,src参数是原始文件绝对地址,dest是目标地址public static File copyFile(String src, String dest) { File file = new File(src); if (!file.exists() || file.isDirectory()) { Log.v(LOG_TAG, "copyFile: file not exist or is directory, " + src); return null; } FileInputStream fi = null; FileOutputStream fo = null; try { fi = new FileInputStream(file); File destPlace = new File(dest); if (!destPlace.exists()) { if (!destPlace.mkdirs()) return null; } String tempName = Util.makeNewName(Util.getNameFromFilename(file.getName())); String destPath = Util.makePath(dest, file.getName()); File destFile = new File(destPath); int i = 1; while (destFile.exists()) { String destName = tempName + "_" + i++ + "." + Util.getExtFromFilename(file.getName()); destPath = Util.makePath(dest, destName); destFile = new File(destPath); } if (!destFile.createNewFile()) return null; fo = new FileOutputStream(destFile); int count = 102400; byte[] buffer = new byte[count]; int read = 0; while ((read = fi.read(buffer, 0, count)) != -1) { fo.write(buffer, 0, read); } // TODO: set access privilege return destFile; } catch (FileNotFoundException e) { Log.e(LOG_TAG, "copyFile: file not found, " + src); e.printStackTrace(); } catch (IOException e) { Log.e(LOG_TAG, "copyFile: " + e.toString()); } finally { try { if (fi != null) fi.close(); if (fo != null) fo.close(); } catch (IOException e) { e.printStackTrace(); } } return null; } 我晕,原来要拷到system目录下,需要获取系统root权限的 是的要获取root权限,其实我应该问一下,怎样让我的apk程序临时获取根权限? http://blog.chinaunix.net/uid-1718717-id-2886368.html试试这个 uses-permission 也要处理下。 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 请问,大家开发android是在windows下呢?还是在linux下? 关于屏幕显示方向问题 有关Android.mk文件的一些问题 getSystemService无法获取传感器服务,是怎么回事呢? 以下角本是什么意思? 关于android android视频录制 请问Android实时播放流媒体的问题 如何切换回程序主Activity时让其自动重新创建调用oncreate()方法? setBackgroundResource导致界面消失的问题 android如何返回到退出前的activity GridView 的item高度很高,快崩溃了
需要注意的是估计app没有权限写到system/XXX下面
public static File copyFile(String src, String dest) {
File file = new File(src);
if (!file.exists() || file.isDirectory()) {
Log.v(LOG_TAG, "copyFile: file not exist or is directory, " + src);
return null;
}
FileInputStream fi = null;
FileOutputStream fo = null;
try {
fi = new FileInputStream(file);
File destPlace = new File(dest);
if (!destPlace.exists()) {
if (!destPlace.mkdirs())
return null;
}
String tempName = Util.makeNewName(Util.getNameFromFilename(file.getName()));
String destPath = Util.makePath(dest, file.getName());
File destFile = new File(destPath);
int i = 1;
while (destFile.exists()) {
String destName = tempName + "_" + i++ + "."
+ Util.getExtFromFilename(file.getName());
destPath = Util.makePath(dest, destName);
destFile = new File(destPath);
} if (!destFile.createNewFile())
return null; fo = new FileOutputStream(destFile);
int count = 102400;
byte[] buffer = new byte[count];
int read = 0;
while ((read = fi.read(buffer, 0, count)) != -1) {
fo.write(buffer, 0, read);
} // TODO: set access privilege return destFile;
} catch (FileNotFoundException e) {
Log.e(LOG_TAG, "copyFile: file not found, " + src);
e.printStackTrace();
} catch (IOException e) {
Log.e(LOG_TAG, "copyFile: " + e.toString());
} finally {
try {
if (fi != null)
fi.close();
if (fo != null)
fo.close();
} catch (IOException e) {
e.printStackTrace();
}
} return null;
}