2010-07-05 28 views
7

我的要求是,從通過內容提供商另一個應用中打開一個應用程序的資源文件。(我揭露與ContentProvider的實現,文件)問題,同時打開資產文件與內容提供商的幫助下

我能開幾文件和閱讀,但同時打開一些文件,我收到異常。請找到打開資產文件的實施。

@Override 
public AssetFileDescriptor openAssetFile(Uri uri, String mode) throws FileNotFoundException { 
    AssetManager am = getContext().getAssets(); 
    String file_name = uri.getLastPathSegment(); 
    if(file_name == null) 
     throw new FileNotFoundException(); 
    AssetFileDescriptor afd = null; 
    try { 
     afd = am.openFd(file_name); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return afd;//super.openAssetFile(uri, mode); 
} 

有些時候,am.openFd(file_name);拋出一個異常說,

java.io.FileNotFoundException: This file can not be opened as a file descriptor; it is probably compressed 
at android.content.res.AssetManager.openAssetFd(Native Method) 
at android.content.res.AssetManager.openFd(AssetManager.java:329) 
at com.pineone.swfinstaller.SwfProvider.openAssetFile(SwfProvider.java:25) 
at android.content.ContentProvider$Transport.openAssetFile(ContentProvider.java:218) 
at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:234) 
at android.os.Binder.execTransact(Binder.java:288) 
at dalvik.system.NativeStart.run(Native Method) 

但是,相同的文件,我可以在我的電腦,甚至在一些其他的方式Android設備打開。

任何人都可以指出,在什麼情況下,我們會得到這個例外。

在此先感謝。

回答

12

Android嘗試壓縮所有資產,除非它無用,如* .mp3文件。 我不知道是否有一個文件類型沒有被壓縮的完整列表,但是如果您只是將文件重命名爲.mp3(壓縮與否僅基於擴展名),您應該沒問題。

小谷歌搜索顯示:

http://osdir.com/ml/android-ndk/2010-04/msg00086.html

如果在構建過程足夠的控制,你可以使用 「-0」標誌用zip或AAPT添加的資產。

-0 specifies an additional extension for which such files will not 
    be stored compressed in the .apk. An empty string means to not 
    compress any files at all. 

AAPT位於您

Android的SDK \平臺\ Android的版本\工具

目錄

從上面的鏈接引用:

static const char* kNoCompressExt[] = { 
".jpg", ".jpeg", ".png", ".gif", 
".wav", ".mp2", ".mp3", ".ogg", ".aac", 
".mpg", ".mpeg", ".mid", ".midi", ".smf", ".jet", 
".rtttl", ".imy", ".xmf", ".mp4", ".m4a", 
".m4v", ".3gp", ".3gpp", ".3g2", ".3gpp2", 
".amr", ".awb", ".wma", ".wmv" 
}; 
+0

如果我們像這樣更改擴展名,那麼在應用程序中使用該文件時,如果期待其他內容,這不會引起問題嗎? – clu 2015-11-11 04:24:03