关于android:如何从资产文件获取URI?

How to get URI from an asset File?

我一直在尝试获取资产文件的URI路径。

1
uri = Uri.fromFile(new File("//assets/mydemo.txt"));

当我检查文件是否存在时,我看到该文件不存在

1
2
3
4
5
6
File f = new File(filepath);
if (f.exists() == true) {
    Log.e(TAG,"Valid :" + filepath);
} else {
    Log.e(TAG,"InValid :" + filepath);
}

有人可以告诉我如何提及资产文件夹中现有文件的绝对路径吗?


没有"资产文件夹中现有文件的绝对路径"。项目的assets/文件夹的内容打包在APK文件中。使用AssetManager对象在资产上获取InputStream

对于WebView,可以使用与使用URL几乎相同的方式使用file Uri方案。资产的语法为file:///android_asset/...(注意:三个斜杠),其中省略号是assets/文件夹中文件的路径。


正确的网址是:

1
file:///android_asset/RELATIVEPATH

其中RELATIVEPATH是资源相对于资产文件夹的路径。

注意方案中的3 /。如果没有3,则Web视图将无法加载我的任何资产。我尝试了2(以前)由CommonsWare进行了评论,但它不起作用。然后,我在github上查看了CommonsWare的源代码,并注意到了额外的正斜杠。

尽管此测试仅在1.6 Android模拟器上进行,但我怀疑它在真实设备或更高版本上是否有所不同。

编辑:CommonsWare更新了他的答案以反映这一微小变化。因此,我对其进行了编辑,因此对于他当前的回答仍然有意义。


enter image description here

确保将您的资产文件夹放置在正确的位置。


请尝试此代码正常工作

1
2
3
4
5
6
 Uri imageUri = Uri.fromFile(new File("//android_asset/luc.jpeg"));

    /* 2) Create a new Intent */
    Intent imageEditorIntent = new AdobeImageIntent.Builder(this)
            .setData(imageUri)
            .build();

适用于WebView,但在URL.openStream()上似乎失败。因此,您需要区分file://协议,并根据建议通过AssetManager处理它们。


尝试一下:有效

1
InputStream in_s = getClass().getClassLoader().getResourceAsStream("TopBrands.xml");

如果您有null值异常,请尝试以下一种方法:

1
InputStream in_s1 =   TopBrandData.class.getResourceAsStream("/assets/TopBrands.xml");

TopBranData是一个类


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
InputStream is = getResources().getAssets().open("terms.txt");
    String textfile = convertStreamToString(is);

public static String convertStreamToString(InputStream is)
            throws IOException {
            Writer writer = new StringWriter();

            char[] buffer = new char[2048];
            try {
                Reader reader = new BufferedReader(new InputStreamReader(is,
                       "UTF-8"));
                int n;
                while ((n = reader.read(buffer)) != -1) {
                    writer.write(buffer, 0, n);
                }
            } finally {
                is.close();
            }
            String text = writer.toString();
            return text;

最后,我找到了一种从Kotlin的答案中获取资产中存在的文件路径的方法。在这里,我们将资产文件复制到缓存,并从该缓存文件获取文件路径。

1
2
3
4
5
@Throws(IOException::class)
fun getFileFromAssets(context: Context, fileName: String): File = File(context.cacheDir, fileName)
    .also {
        it.outputStream().use { cache -> context.assets.open(fileName).use { it.copyTo(cache) } }
    }

获取文件的路径,例如:

1
val filePath =  getFileFromAssets(context,"fileName.extension").absolutePath


尝试这个 :

1
Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.cat);

我做到了,它奏效了


是的,您无法从Android手机或仿真器访问驱动器文件夹,因为您的计算机和android是两个不同的操作系统。我会选择android的res文件夹,因为它具有良好的资源管理方法。直到且除非您有充分的理由将文件放入资产文件夹。相反,您可以这样做

1
2
3
4
5
6
7
8
9
10
try {
      Resources res = getResources();
      InputStream in_s = res.openRawResource(R.raw.yourfile);

      byte[] b = new byte[in_s.available()];
      in_s.read(b);
      String str = new String(b);
    } catch (Exception e) {
      Log.e(LOG_TAG,"File Reading Error", e);
 }

为我工作尝试此代码

1
2
3
4
5
6
7
   uri = Uri.fromFile(new File("//assets/testdemo.txt"));
    File f = new File(testfilepath);
    if (f.exists() == true) {
    Toast.makeText(getApplicationContext(),"valid :" + testfilepath, 2000).show();
    } else {
   Toast.makeText(getApplicationContext(),"invalid :" + testfilepath, 2000).show();
 }