Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/219.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在内容提供商的帮助下打开资产文件时出现问题_Java_Android_Android Assets - Fatal编程技术网

Java 在内容提供商的帮助下打开资产文件时出现问题

Java 在内容提供商的帮助下打开资产文件时出现问题,java,android,android-assets,Java,Android,Android Assets,我的要求是通过ContentProvider从另一个应用程序打开一个应用程序的资产文件。(我使用ContentProvider实现公开该文件) 我能够打开几个文件并读取,但在打开一些文件时,我遇到了异常。请查找打开资产文件的实现 @Override public AssetFileDescriptor openAssetFile(Uri uri, String mode) throws FileNotFoundException { AssetManager am = getContex

我的要求是通过ContentProvider从另一个应用程序打开一个应用程序的资产文件。(我使用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(文件名)
正在抛出一个异常,表示

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会尝试压缩所有资产,除非它没有用,比如用于*.mp3文件。 我不知道是否有一个不被压缩的文件类型的完整列表,但是如果您只是将文件重命名为.mp3(压缩与否的决定仅取决于扩展名),您应该不会有问题

谷歌搜索显示:

如果对构建过程有足够的控制,则可以使用 带有zip或aapt的“-0”标志以添加资产

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

如果我们像这样更改扩展名,在应用程序中使用该文件时,如果它需要其他东西,这不会导致问题吗?