android assetManager

2019-02-04 05:38发布

问题:

I need to get Absolute path to Folder in Assets. Some like this for sd-card:

final String sdDir = Environment.getExternalStorageDirectory() + "Files";

What i do incorrect?

First I try to get path (in green ractangle) this way but I alwase get "False". Then I comment this block and try to get path from getAssets().list(); But I get 3 folders witch I see first time.

I want to make massive like this "green" but I need to use files from assets:

Look the image

Help me to get absolute path to my Files folder.

回答1:

I'm not exactly sure what you're trying to do, so I'll try to cover the bases, and maybe this will help.

  • If you are just trying to get a list of what's in your assets, then use getAssets().list("Files"). (You have to use the subdirectory, because of this).

  • If you are trying to get an absolute path to your assets directory (or any subdirectory), you can't. Whatever is in the assets directory is in the APK. It's not in external storage like on an SD card.

  • If you are trying to open up files in the assets directory, use AssetManager.open(filename) to get the InputStream. Here, filename should be the relative path from the assets directory.


EDIT

I'm not sure what you mean by "massive", but if you want to load the file black.png from assets instead of the SD card, then write this:

// must be called from Activity method, such as onCreate()
AssetManager assetMgr = this.getAssets();
mColors = new Bitmap[] {
    BitmapFactory.decodeStream(assetMgr.open("black.png"));
    // and the rest
};


回答2:

Assets are stored in the APK file so there are no absolute path than your application can use. But, I would suggest to take a look at file:///android_asset. It might fits your needs. Here is a good example on how to display an Asset in a WebView.