的问题,同时打开资产文件与内容提供商的帮助 [英] Problem while opening Asset File with the help of Content Provider

查看:323
本文介绍了的问题,同时打开资产文件与内容提供商的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的要求是,通过从内容提供商另一个应用程序打开一个应用程序的资源文件。(我用暴露的ContentProvider执行该文件)

我能够打开一些文件和阅读,但在打开某些文件,我得到异常。请找到实现开放资产文件。

  @覆盖
公共AssetFileDescriptor openAssetFile(URI URI,字符串模式)抛出FileNotFoundException异常{
    AssetManager AM =的getContext()getAssets()。
    串FILE_NAME = uri.getLastPathSegment();
    如果(FILE_NAME == NULL)
        抛出新的FileNotFoundException异常();
    AssetFileDescriptor AFD = NULL;
    尝试 {
        AFD = am.openFd(FILE_NAME);
    }赶上(IOException异常E){
        e.printStackTrace();
    }
    返回法国开发署; // super.openAssetFile(URI,模式);
}
 

有些时候, am.openFd(FILE_NAME); 抛出一个异常说,

  java.io.FileNotFoundException:此文件不能打开的文件描述符;它可能是COM pressed
在android.content.res.AssetManager.openAssetFd(本机方法)
在android.content.res.AssetManager.openFd(AssetManager.java:329)
在com.pineone.swfinstaller.SwfProvider.openAssetFile(SwfProvider.java:25)
在android.content.ContentProvider $ Transport.openAssetFile(ContentProvider.java:218)
在android.content.ContentProviderNative.onTransact(ContentProviderNative.java:234)
在android.os.Binder.execTransact(Binder.java:288)
在dalvik.system.NativeStart.run(本机方法)
 

不过,同一个文件,我可以在我的电脑,甚至在一些其他的方式Android设备打开。

任何人都可以指出我,在什么情况下,我们会得到这个例外。

在此先感谢。

解决方案

Android的尝试COM preSS的所有资产,除非它是没有用的,像* .mp3文件。 我不知道是否有文件类型不得到COM pressed的完整列表,但你应该罚款,如果你只是重命名文件为.mp3(决定COM preSS与否是只是基于扩展名)

一个小谷歌搜索显示:

<一个href="http://osdir.com/ml/android-ndk/2010-04/msg00086.html">http://osdir.com/ml/android-ndk/2010-04/msg00086.html

  

如果你有足够的控制权的构建过程中,可以使用   -0标志用zip或AAPT添加的资产。

  -0指定一个额外的扩展名,文件将不会为其
    是pssed的apk文件存储玉米$ P $。空字符串表示不
    COM preSS在所有的任何文件。
 

AAPT位于您的

  

Android的SDK \平台\ Android系统版本\工具

目录

从上面的链接引用:

 静态为const char * kNoCom pressExt [] = {
.JPG,.JPEG,巴纽,名为.gif
.WAV,.mp2,MP3,.OGG,.AAC
.MPG,文件.mpeg,。中旬,.midi,.smf,.jet,
.rtttl,.imy,.xmf,的MP4,.M4A,
的.m4v,名为.3gp,.3gpp,.3g2,.3gpp2,
.AMR,.awb,WMA,.WMV
};
 

My requirement is to open one app's Asset file from another app via content provider.(I am exposing that file with ContentProvider implementation)

I am able to open few files and read, but while opening some files I am getting exception. Please find the implementation for opening Asset File.

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

Some times, am.openFd(file_name); is throwing an exception saying that,

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)

But, the same file I can open in my PC or even in Android device in some other manner.

Can anyone point out me, in what scenarios, we will get this exception.

Thanks in advance.

解决方案

Android tries to compress all assets, unless it's useless, like for *.mp3 files. I don't know if there is a complete list of filetypes that don't get compressed, but you should be fine if you just rename your files to .mp3 (the decision to compress or not is only based on the extension)

A little google search revealed:

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

If you have sufficient control over the build process, you can use the "-0" flag with zip or aapt to add the assets.

-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 is located in your

android-sdk\platforms\android-VERSION\tools

directory

from the link cited above:

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

这篇关于的问题,同时打开资产文件与内容提供商的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆