[英]Moving files from R.raw directory to internal storage programmatically
I have a lot of files (of two extensions, .ppn and .pv) in R.raw
. 我在
R.raw
有很多文件(两个扩展名.ppn和.pv)。 I'm trying moving them into internal storage. 我正在尝试将它们移入内部存储。
I've tried doing this: 我尝试这样做:
private static void copyPorcupineConfigFiles(Context context) {
int[] resIds = {R.raw.alexa, R.raw.americano, R.raw.avocado, R.raw.blueberry,
R.raw.bumblebee, R.raw.caterpillar, R.raw.christina, R.raw.dragonfly,
R.raw.flamingo, R.raw.francesca, R.raw.grapefruit, R.raw.grasshopper,
R.raw.iguana, R.raw.picovoice, R.raw.pineapple, R.raw.porcupine,
R.raw.raspberry, R.raw.terminator, R.raw.vancouver, R.raw.params};
Resources resources = context.getResources();
for (int resId : resIds) {
String filename = resources.getResourceEntryName(resId);
String fileExtension = resId == R.raw.params ? ".pv" : ".ppn";
InputStream is = null;
OutputStream os = null;
try {
is = new BufferedInputStream(resources.openRawResource(resId),
256);
os = new BufferedOutputStream(context.openFileOutput(filename + fileExtension,
Context.MODE_PRIVATE), 256);
int r;
while ((r = is.read()) != -1) {
os.write(r);
}
os.flush();
} catch (IOException e) {
showErrorToast(context);
} finally {
try {
if (is != null) {
is.close();
}
if (os != null) {
os.close();
}
} catch (IOException e) {
showErrorToast(context);
}
}
}
}
It should save in data/0/com.package.name
它应该保存在
data/0/com.package.name
When I do: 当我做:
String keywordFilePath = new File(this.getFilesDir(), ".ppn")
.getAbsolutePath();
String modelFilePath = new File(this.getFilesDir(), "params.pv").getAbsolutePath();
It gives me the path: 它给了我路径:
/data/user/0/com.package.name/files/alexa.ppn
However, when I look with File Manager or try retrieving the file programatically, it's not there. 但是,当我使用文件管理器或尝试以编程方式检索文件时,它不存在。
Your files are there in data/data/packagename/files/
directory but its invisible to user. 您的文件位于
data/data/packagename/files/
目录中,但对用户不可见。 if you want to check whether that file is at the path or not then 如果要检查该文件是否在路径中,则
1.You can read file from that directory 1.您可以从该目录读取文件
FileOutputStream fileout=openFileOutput("sample.txt", MODE_PRIVATE);
OutputStreamWriter outputWriter=new OutputStreamWriter(fileout);
InputStream inputStream = this.getResources().openRawResource(R.raw.sample);
BufferedReader r = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder total = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
total.append(line).append('\n');
}
outputWriter.write(total.toString());
outputWriter.close();
System.out.println("file copied");
//code to read the file contents
FileInputStream fis = openFileInput("sample.txt");
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader bufferedReader = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String line1;
while ((line1 = bufferedReader.readLine()) != null) {
sb.append(line1);
}
System.out.println("contents:"+sb.toString());
go to adb shell
and type $ cd /data/data/packagename
转到
adb shell
并输入$ cd /data/data/packagename
$ cp data/data/packagename/files/sample.txt /storage/emulated/0/
and you will get your file in /storage/emulated/0/ directory
3. If you want to see that file then use the following path in your code but your file will become public to user $
cp data/data/packagename/files/sample.txt /storage/emulated/0/
,您将在/storage/emulated/0/ directory
获取文件。如果要查看该文件,请在以下路径中使用您的代码,但您的文件将对用户公开
File file=new File(Environment.getExternalStorageDirectory(),"sample.txt");
for details refer this link 有关详细信息,请参阅此链接
hope it helps you 希望对你有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.