您的位置:首页 > 其它

从另一个程序获得 Assests 文件夹下的东西

2012-04-10 13:57 344 查看
Resources r = this.getResources(); 
AssetManager a = r.getAssets(); 
String[] list = a.list("/"); 
Log.d("test", "Length
of / is "+list.length); 
for (String s : list) { 

    Log.d("test", s); 


按照上面的写法是错误的 String[] names = am.list(""); 中间是空格

也可以AssetManager am = this.getAssets(); 
String[] names = am.list(""); 

 

如果是asset下的子文件夹String[] names= am.list("subfolder"); 

如果是asset下的文件InputStream in = am.open("file.png");

子文件夹下的文件 InputStream in = am.open("subfolder/file.png");

然后使用

Bitmap bitmap = BitmapFactory.decodeStream(inputStream); 

3.

void displayFiles (AssetManager mgr, String path) { 

    try { 

        String list[] = mgr.list(path); 

        if (list != null) 

            for (int i=0; i<list.length; ++i) 

                { 

                    Log.v("Assets:", path +"/"+ list[i]); 

                    displayFiles(mgr, path + "/" + list[i]); 

                } 

    } catch (IOException e) { 

        Log.v("List error:", "can't
list" + path); 

    } 

 

一定注意加/

4.这个问题还会遇到解析出来的是系统目录

http://groups.google.com/group/android-developers/browse_thread/thread/2b110c3c30d8df93/7d8191d6302bca2c?pli=1

参看这里

Did this code:

                try 

                { 

                        String fileArray[] = context.getAssets().list( "" );

                        if( fileArray != null) 

                                for( int i = 0; i < fileArray.length; ++i ) 

                                { 

                                        Log.v( " ASSETS_DIR File", fileArray[i] ); 

                                        try 

                                        { 

                                                String subfileArray[] = 

context.getAssets().list( fileArray[i] );

                                                if( subfileArray != null) 

                                                        for( int j = 0; j < subfileArray.length; ++j ) 

                                                        { 

                                                                Log.v( " ASSETS_SUBDIR File", fileArray[i] + ":" + 

subfileArray[j] ); 

                                                        }

                                        } 

                                        catch( IOException e) 

                                        { 

                                                Log.e( "ASSETS_SUBDIR", "Can't List files!" ); 

                                        }

                                }

                } 

                catch( IOException e) 

                { 

                        Log.e( "ASSETS_DIR", "Can't List files!" ); 

                }

And got the following result.

DUMP from logcat !

V/ASSETS_DIR File(7570): images 

V/ASSETS_SUBDIR File(7570): images:corky.png 

V/ASSETS_SUBDIR File(7570): images:cylon_dot.png 

V/ASSETS_SUBDIR File(7570): images:cylon_left.png 

V/ASSETS_SUBDIR File(7570): images:cylon_right.png 

V/ASSETS_SUBDIR File(7570): images:violet.png 

V/ASSETS_DIR File(7570): skins 

V/ASSETS_SUBDIR File(7570): skins:contact-andy-rubin.png 

V/ASSETS_SUBDIR File(7570): skins:contact-larry-page.png 

V/ASSETS_DIR File(7570): sounds 

V/ASSETS_SUBDIR File(7570): sounds:bootanim0.raw 

V/ASSETS_SUBDIR File(7570): sounds:bootanim1.raw 

V/ASSETS_DIR File(7570): webkit 

V/ASSETS_SUBDIR File(7570): webkit:missingImage.png 

V/ASSETS_SUBDIR File(7570): webkit:nullplugin.png 

V/ASSETS_SUBDIR File(7570): webkit:textAreaResizeCorner.png

void displayFiles (AssetManager mgr, String path) { 

    try { 

        String list[] = mgr.list(path); 

        if (list != null) 

            for (int i=0; i<list.length; ++i) 

                { 

                    Log.v("Assets:", path +"/"+ list[i]); 

                    displayFiles(mgr, path + list[i]); 

                } 

    } catch (IOException e) { 

        Log.v("List error:", "can't list" + path); 

    }

 



AssetManager mgr = new AssetManager(); 

displayFiles(mgr, "/");

prints:

V/Assets: (13257): //AndroidManifest.xml 

V/Assets: (13257): //META-INF 

V/Assets: (13257): //assets 

V/Assets: (13257): //res 

V/Assets: (13257): //resources.arsc

displayFiles(mgr, "");

prints:

displayFiles(mgr, "");

prints:

V/Assets: (14399): /images 

V/Assets: (14399): images/corky.png 

V/Assets: (14399): images/cylon_dot.png 

V/Assets: (14399): images/cylon_left.png 

V/Assets: (14399): images/cylon_right.png 

V/Assets: (14399): images/violet.png 

V/Assets: (14399): /skins 

V/Assets: (14399): skins/contact-andy-rubin.png 

V/Assets: (14399): skins/contact-larry-page.png 

V/Assets: (14399): /sounds 

V/Assets: (14399): sounds/bootanim0.raw 

V/Assets: (14399): sounds/bootanim1.raw 

V/Assets: (14399): /webkit 

V/Assets: (14399): webkit/missingImage.png 

V/Assets: (14399): webkit/nullplugin.png 

V/Assets: (14399): webkit/textAreaResizeCorner.png
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  file list string path webkit null