How to get URI from an asset File?

2018-12-31 22:45发布

I have been trying to get the URI path for an asset file.

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

When I check if the file exists I see that file doesn't exist

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

Can some one tell me how I can mention the absolute path for a file existing in the asset folder

10条回答
深知你不懂我心
2楼-- · 2018-12-31 22:55

enter image description here

Be sure ,your assets folder put in correct position.

查看更多
人间绝色
3楼-- · 2018-12-31 22:56

Try out this : it works

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

if you have null value exception try this one:

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

TopBranData is a Class

查看更多
刘海飞了
4楼-- · 2018-12-31 22:58

Works for WebView but seems to fail on URL.openStream(). So you need to distinguish file:// protocols and handle them via AssetManager as suggested.

查看更多
高级女魔头
5楼-- · 2018-12-31 23:03

Please try this code working fine

 Uri imageUri = Uri.fromFile(new File("//android_asset/luc.jpeg"));

    /* 2) Create a new Intent */
    Intent imageEditorIntent = new AdobeImageIntent.Builder(this)
            .setData(imageUri)
            .build();
查看更多
明月照影归
6楼-- · 2018-12-31 23:06
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;
查看更多
还给你的自由
7楼-- · 2018-12-31 23:10

There is no "absolute path for a file existing in the asset folder". The content of your project's assets/ folder are packaged in the APK file. Use an AssetManager object to get an InputStream on an asset.

EDIT

To repair one of my comments below, the URL syntax for assets is file:///android_asset/... (note: three slashes).

查看更多
登录 后发表回答