0
将非文本文件保存到/ data/files文件夹的方法。 如果文件资源来自资产文件夹。如何在android中的data/files文件夹中保存非txt文件
将非文本文件保存到/ data/files文件夹的方法。 如果文件资源来自资产文件夹。如何在android中的data/files文件夹中保存非txt文件
这应该做到这一点。
private void writeToSDCard() {
try {
File root = Environment.getExternalStorageDirectory();
if (root.canWrite()){
InputStream from = myContext.getResources().openRawResource(rID);
File dir = new java.io.File (root, "pdf");
dir.mkdir();
File writeTo = new File(root, "pdf/" + attachmentName);
FileOutputStream to = new FileOutputStream(writeTo);
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = from.read(buffer)) != -1)
to.write(buffer, 0, bytesRead); // write
to.close();
from.close();
} else {
Log.d(TAG, "Unable to access SD card.");
}
} catch (Exception e) {
Log.d(TAG, "writeToSDCard: " + e.getMessage());
}
}
}
很酷。 我也发现了这一点: http://stackoverflow.com/questions/4447477/android-how-to-copy-files-in-assets-to-sdcard ... 差不多了。我修改了代码,以便将其保存到/ data/data/[PACKAGE_NAME] /文件并且它可以工作。非常感谢。 – eunique0216