问题描述
可能重复:
Android - 如何确定从资产特定文件的绝对路径
我想传递一个文件到文件(字符串路径)类。有没有办法找到资源文件夹中的文件的绝对路径,并把它传递到文件()。我试过文件:/// android_asset / myfoldername / mYfILEname的
的路径字符串,但它没有工作。你知道吗?
I am trying to pass a file to File(String path) class. Is there a way to find absolute path of the file in assets folder and pass it to File(). I tried file:///android_asset/myfoldername/myfilename
as path string but it didnt work. Any idea?
推荐答案
AFAIK,你不能创建一个文件
从资产的文件,因为这些都存储在APK ,这意味着有一个资产文件夹没有路径。
AFAIK, you can't create a File
from an assets file because these are stored in the apk, that means there is no path to an assets folder.
不过,你可以尝试创建文件
使用缓冲区和<$c$c>AssetManager$c$c> (它提供了访问应用程序的原始资源文件)。
But, you can try to create that File
using a buffer and the AssetManager
(it provides access to an application's raw asset files).
试着做这样的事情:
AssetManager am = getAssets();
InputStream inputStream = am.open("myfoldername/myfilename");
File file = createFileFromInputStream(inputStream);
private File createFileFromInputStream(InputStream inputStream) {
try{
File f = new File(my_file_name);
OutputStream outputStream = new FileOutputStream(f);
byte buffer[] = new byte[1024];
int length = 0;
while((length=inputStream.read(buffer)) > 0) {
outputStream.write(buffer,0,length);
}
outputStream.close();
inputStream.close();
return f;
}catch (IOException e) {
//Logging exception
}
return null;
}
让我知道你的进度。
这篇关于如何通过其资产文件夹到文件(字符串路径)的文件路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!