How to get URI from an asset File?

2018-12-31 23:00发布

问题:

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

回答1:

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).



回答2:

The correct url is:

file:///android_asset/RELATIVEPATH

where RELATIVEPATH is the path to your resource relative to the assets folder.

Note the 3 /\'s in the scheme. Web view would not load any of my assets without the 3. I tried 2 as (previously) commented by CommonsWare and it wouldn\'t work. Then I looked at CommonsWare\'s source on github and noticed the extra forward slash.

This testing though was only done on the 1.6 Android emulator but I doubt its different on a real device or higher version.

EDIT: CommonsWare updated his answer to reflect this tiny change. So I\'ve edited this so it still makes sense with his current answer.



回答3:

\"enter

Be sure ,your assets folder put in correct position.



回答4:

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:

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:

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



回答7:

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;


回答8:

try this :

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

I had did it and it worked



回答9:

Yeah you can\'t access your drive folder from you android phone or emulator because your computer and android are two different OS.I would go for res folder of android because it has good resources management methods. Until and unless you have very good reason to put you file in assets folder. Instead You can do this

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


回答10:

Worked for me Try this code

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