gpt4 book ai didi

java - 以编程方式将文件从 R.raw 目录移动到内部存储

转载 作者:行者123 更新时间:2023-11-29 02:27:41 25 4
gpt4 key购买 nike

我在 R.raw 中有很多文件(两个扩展名,.ppn 和 .pv)。我正在尝试将它们移动到内部存储空间。

我试过这样做:

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);
}
}
}
}

它应该保存在data/0/com.package.name

当我这样做时:

String keywordFilePath = new File(this.getFilesDir(), ".ppn")
.getAbsolutePath();
String modelFilePath = new File(this.getFilesDir(), "params.pv").getAbsolutePath();

它给了我路径:

/data/user/0/com.package.name/files/alexa.ppn

但是,当我使用文件管理器查看或尝试以编程方式检索文件时,它不存在。

最佳答案

您的文件位于 data/data/packagename/files/ 目录中,但用户不可见。如果你想检查该文件是否在路径中,那么

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());
  1. 使用以下命令使用 adb shell 将文件复制到您的 SD 卡

转到 adb shell 并输入$ cd/data/data/packagename

$ cp data/data/packagename/files/sample.txt/storage/emulated/0/您将在 /storage/emulated/0/目录中获得您的文件 3. 如果您想查看该文件,请在您的代码中使用以下路径,但您的文件将对用户公开

File file=new File(Environment.getExternalStorageDirectory(),"sample.txt");

有关详细信息,请参阅此 link

希望对你有帮助

关于java - 以编程方式将文件从 R.raw 目录移动到内部存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51316311/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com