我的PDF文件中,我想开启使用外部应用程序,我的应用程序资产目录中,这样写道:我的内容提供商,我tryng,使其工作,但没有...
这里是code:
内容提供商:
包package.name;
进口的java.io.File;
进口java.io.FileNotFoundException;
进口java.net.URI中;
进口android.content.ContentProvider;
进口android.content.ContentValues;
进口android.database.Cursor;
进口android.net.Uri;
进口android.os.ParcelFileDescriptor;
公共类FileContentProvider扩展ContentProvider的{
私有静态最后弦乐URI_ preFIX =内容://package.name.filecontentprovider;
公共静态字符串constructUri(字符串URL){
开放的我们的uri = Uri.parse(URL);
返回uri.isAbsolute()?网址:URI_ preFIX +网址;
}
@覆盖
公共ParcelFileDescriptor中openFile(URI URI,字符串模式)抛出FileNotFoundException异常{
URI uri1 = URI.create(文件:///data/data/package.name/+ uri.getPath());
档案文件=新的文件(uri1.getPath());
ParcelFileDescriptor包裹= ParcelFileDescriptor.open(文件,ParcelFileDescriptor.MODE_READ_ONLY);
返回包裹;
}
@覆盖
公共布尔的onCreate(){
返回true;
}
@覆盖
公众诠释删除(URI URI,的String,字符串[]作为){
抛出新UnsupportedOperationException异常(不支持此提供程序);
}
@覆盖
公共字符串的getType(URI URI){
抛出新UnsupportedOperationException异常(不支持此提供程序);
}
@覆盖
公众开放的插入(URI URI,ContentValues contentvalues){
抛出新UnsupportedOperationException异常(不支持此提供程序);
}
@覆盖
公共光标查询(URI URI,字符串[]作为,的String,字符串[] AS1,串S1){
抛出新UnsupportedOperationException异常(不支持此提供程序);
}
@覆盖
公众诠释更新(开放的我们的URI,ContentValues contentvalues,的String,字符串[]作为){
抛出新UnsupportedOperationException异常(不支持此提供程序);
}
}
在这里我如何调用该文件的打开:
文件pdf =新的文件(资产/ prova.pdf);
意图I =新的意图(Intent.ACTION_VIEW,Uri.parse(内容://package.name/+ PDF));
i.setType(应用程序/ PDF格式);
startActivity(ⅰ);
我添加了Android清单下面一行,在标签内:
<提供机器人:FileContentProvider名称=机器人:当局=package.name/>
这是logcat的输出:
02-26 19:47:44.938:ERROR / AndroidRuntime(6494):未捕获的处理程序:螺纹主力退出,由于未捕获的异常
02-26 19:47:44.953:ERROR / AndroidRuntime(6494):java.lang.RuntimeException的:无法启动的活动ComponentInfo {com.tf.thinkdroid.samsung/com.tf.thinkdroid.pdf.app.PdfRenderScreen}:JAVA .lang.NullPointerException
02-26 19:47:44.953:ERROR / AndroidRuntime(6494):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
02-26 19:47:44.953:ERROR / AndroidRuntime(6494):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
02-26 19:47:44.953:ERROR / AndroidRuntime(6494):在android.app.ActivityThread.access $ 2200(ActivityThread.java:119)
02-26 19:47:44.953:ERROR / AndroidRuntime(6494):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1863)
02-26 19:47:44.953:ERROR / AndroidRuntime(6494):在android.os.Handler.dispatchMessage(Handler.java:99)
02-26 19:47:44.953:ERROR / AndroidRuntime(6494):在android.os.Looper.loop(Looper.java:123)
02-26 19:47:44.953:ERROR / AndroidRuntime(6494):在android.app.ActivityThread.main(ActivityThread.java:4363)
02-26 19:47:44.953:ERROR / AndroidRuntime(6494):在java.lang.reflect.Method.invokeNative(本机方法)
02-26 19:47:44.953:ERROR / AndroidRuntime(6494):在java.lang.reflect.Method.invoke(Method.java:521)
02-26 19:47:44.953:ERROR / AndroidRuntime(6494):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:862)
02-26 19:47:44.953:ERROR / AndroidRuntime(6494):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
02-26 19:47:44.953:ERROR / AndroidRuntime(6494):在dalvik.system.NativeStart.main(本机方法)
02-26 19:47:44.953:ERROR / AndroidRuntime(6494):由:显示java.lang.NullPointerException
02-26 19:47:44.953:ERROR / AndroidRuntime(6494):在com.tf.thinkdroid.pdf.app.RenderScreen.onNewIntent(来源不明)
02-26 19:47:44.953:ERROR / AndroidRuntime(6494):在com.tf.thinkdroid.pdf.app.RenderScreen.onCreate(来源不明)
02-26 19:47:44.953:ERROR / AndroidRuntime(6494):在com.tf.thinkdroid.pdf.app.PdfRenderScreen.onCreate(来源不明)
02-26 19:47:44.953:ERROR / AndroidRuntime(6494):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-26 19:47:44.953:ERROR / AndroidRuntime(6494):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
02-26 19:47:44.953:ERROR / AndroidRuntime(6494):11 ...更多
不明白问题出在哪里,似乎是外部应用程序无法获取文件。
感谢您的帮助!
解决方案我是新android开发,花了一整天找我的本地存储的PDF不能由外部应用程序打开的原因。我很高兴已经发现了这个线索。
在此期间马尔科得到它的工作,并在这里描述。 当心:意大利语言 - 谷歌翻译服务可以帮助; - )
http://www.marcofaion.it/?p=7 http://web.archive.org/网络/ 20111020204554 / HTTP://www.marcofaion.it/ P = 7
补充说明他的HOWTO初学者:
该行马可提到要在manifest.xml插入
<提供机器人:FileContentProvider名称=机器人:当局=package.name/>
应该插入的在的<应用...>< /用途>
标记
如果你打算定制的文件名,你应该换
的InputStream是= am.open(file.pdf);
与
的InputStream是= am.open(uri.getLastPathSegment());
PDF文件需要被放入已经存在的文件夹中的资产在你的项目中(尤其是在没有任何新添加的文件夹RES /资产或某事)! =)
i've a pdf file in my app assets directory that i want open using an external app, so wrote my content provider and i'm tryng to make it work but nothing...
here is the code:
Content Provider:
package package.name;
import java.io.File;
import java.io.FileNotFoundException;
import java.net.URI;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.ParcelFileDescriptor;
public class FileContentProvider extends ContentProvider {
private static final String URI_PREFIX = "content://package.name.filecontentprovider";
public static String constructUri(String url) {
Uri uri = Uri.parse(url);
return uri.isAbsolute() ? url : URI_PREFIX + url;
}
@Override
public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
URI uri1 = URI.create("file:///data/data/package.name/"+uri.getPath());
File file = new File(uri1.getPath());
ParcelFileDescriptor parcel = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
return parcel;
}
@Override
public boolean onCreate() {
return true;
}
@Override
public int delete(Uri uri, String s, String[] as) {
throw new UnsupportedOperationException("Not supported by this provider");
}
@Override
public String getType(Uri uri) {
throw new UnsupportedOperationException("Not supported by this provider");
}
@Override
public Uri insert(Uri uri, ContentValues contentvalues) {
throw new UnsupportedOperationException("Not supported by this provider");
}
@Override
public Cursor query(Uri uri, String[] as, String s, String[] as1, String s1) {
throw new UnsupportedOperationException("Not supported by this provider");
}
@Override
public int update(Uri uri, ContentValues contentvalues, String s, String[] as) {
throw new UnsupportedOperationException("Not supported by this provider");
}
}
and here how i call the opening of the file:
File pdf = new File("assets/prova.pdf");
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("content://package.name/" + pdf));
i.setType("application/pdf");
startActivity(i);
i've added the following line in the android manifest, inside the tag:
<provider android:name=".FileContentProvider" android:authorities="package.name" />
this is the logcat output:
02-26 19:47:44.938: ERROR/AndroidRuntime(6494): Uncaught handler: thread main exiting due to uncaught exception
02-26 19:47:44.953: ERROR/AndroidRuntime(6494): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tf.thinkdroid.samsung/com.tf.thinkdroid.pdf.app.PdfRenderScreen}: java.lang.NullPointerException
02-26 19:47:44.953: ERROR/AndroidRuntime(6494): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
02-26 19:47:44.953: ERROR/AndroidRuntime(6494): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
02-26 19:47:44.953: ERROR/AndroidRuntime(6494): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
02-26 19:47:44.953: ERROR/AndroidRuntime(6494): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
02-26 19:47:44.953: ERROR/AndroidRuntime(6494): at android.os.Handler.dispatchMessage(Handler.java:99)
02-26 19:47:44.953: ERROR/AndroidRuntime(6494): at android.os.Looper.loop(Looper.java:123)
02-26 19:47:44.953: ERROR/AndroidRuntime(6494): at android.app.ActivityThread.main(ActivityThread.java:4363)
02-26 19:47:44.953: ERROR/AndroidRuntime(6494): at java.lang.reflect.Method.invokeNative(Native Method)
02-26 19:47:44.953: ERROR/AndroidRuntime(6494): at java.lang.reflect.Method.invoke(Method.java:521)
02-26 19:47:44.953: ERROR/AndroidRuntime(6494): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:862)
02-26 19:47:44.953: ERROR/AndroidRuntime(6494): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
02-26 19:47:44.953: ERROR/AndroidRuntime(6494): at dalvik.system.NativeStart.main(Native Method)
02-26 19:47:44.953: ERROR/AndroidRuntime(6494): Caused by: java.lang.NullPointerException
02-26 19:47:44.953: ERROR/AndroidRuntime(6494): at com.tf.thinkdroid.pdf.app.RenderScreen.onNewIntent(Unknown Source)
02-26 19:47:44.953: ERROR/AndroidRuntime(6494): at com.tf.thinkdroid.pdf.app.RenderScreen.onCreate(Unknown Source)
02-26 19:47:44.953: ERROR/AndroidRuntime(6494): at com.tf.thinkdroid.pdf.app.PdfRenderScreen.onCreate(Unknown Source)
02-26 19:47:44.953: ERROR/AndroidRuntime(6494): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-26 19:47:44.953: ERROR/AndroidRuntime(6494): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
02-26 19:47:44.953: ERROR/AndroidRuntime(6494): ... 11 more
don't understand where the problem is, seem that the external application can't get the file.
thanks for any help!
解决方案I am new to android development and spent the whole day looking for the reason that my locally stored PDF could not be opened by external apps. I am glad having found this thread.
In the meantime Marco got it working and describes it here. Beware: Italian language- Google translation service might help ;-)
http://www.marcofaion.it/?p=7 http://web.archive.org/web/20111020204554/http://www.marcofaion.it/?p=7
Additional notes to his howto for beginners:
The line Marco mentions to be inserted in the Manifest.xml
<provider android:name=".FileContentProvider" android:authorities="package.name" />
should be inserted within the <application ...></application>
tag.
And if you plan to have custom filenames you should exchange
InputStream is = am.open("file.pdf");
with
InputStream is = am.open(uri.getLastPathSegment());
PDF files have to be put into already existing folder assets in your project (especially not in any newly added folder res/assets or sth.)! =)
上一篇:使用 jQuery 重置单选按钮单选、按钮、jQuery
下一篇:机器人:刷新的ListView使用ListAdapter和SimpleCursorAdapter机器人、ListView、SimpleCursorAdapter、ListAdapter