如何通过其资产文件夹到文件(字符串路径)的文件路径? [英] How to pass a file path which is in assets folder to File(String path)?

查看:208
本文介绍了如何通过其资产文件夹到文件(字符串路径)的文件路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  <一href="http://stackoverflow.com/questions/4744169/android-how-to-determine-the-absolute-path-for-specific-file-from-assets">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.

不过,你可以尝试创建文件使用缓冲区和<一href="http://developer.android.com/reference/android/content/res/AssetManager.html"><$c$c>AssetManager (它提供了访问应用程序的原始资源文件)。

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

让我知道你的进度。

这篇关于如何通过其资产文件夹到文件(字符串路径)的文件路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆