内容提供程序无法在所有设备上完全运行
我已经实现了以下内容,并且它可以在 HTC Desire (Android 2.2) 上轻松运行。
Uri uri = Uri.parse("content://com.package.app.AssetProvider/"+"document.pdf");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(uri, "application/pdf");
try
{
startActivity(intent);
}
catch (ActivityNotFoundException e)
{
Toast.makeText(this,
"You have PDF reader installed!",
Toast.LENGTH_SHORT).show();
}
在我的内容提供程序中,我实现了 openFile 方法:
@Override
public ParcelFileDescriptor openFile(Uri uri, String mode)
throws FileNotFoundException {
Log.d(TAG, " openFile ");
try {
AssetManager am = context.getAssets();
InputStream is = am.open(uri.getLastPathSegment());
File dir = new File("data/data/"+ PACKAGE_NAME +"/files/");
dir.mkdirs();
File file = new File("data/data/"+ PACKAGE_NAME +"/files/outFile.pdf");
file.createNewFile();
OutputStream out=new FileOutputStream(file);
byte buf[]=new byte[1024];
int len;
while((len=is.read(buf))>0)
out.write(buf,0,len);
out.close();
is.close();
parcel = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE);
}
catch (Exception e) {
Log.e("Exception",e.toString());
}
return parcel;
}
问题是在 Samsung Galaxy S II (Android 2.3.5)、HCT Wildfire (2.2.1) 上未调用方法 openFile() )和 HTC Sensation(Android 2.3.4)。但 onCreate() 方法会在每个设备上调用。
有谁可以帮忙吗?
I've implemented the following and it works painless on HTC Desire (Android 2.2).
Uri uri = Uri.parse("content://com.package.app.AssetProvider/"+"document.pdf");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(uri, "application/pdf");
try
{
startActivity(intent);
}
catch (ActivityNotFoundException e)
{
Toast.makeText(this,
"You have PDF reader installed!",
Toast.LENGTH_SHORT).show();
}
And in my content provider I've implemented the openFile method:
@Override
public ParcelFileDescriptor openFile(Uri uri, String mode)
throws FileNotFoundException {
Log.d(TAG, " openFile ");
try {
AssetManager am = context.getAssets();
InputStream is = am.open(uri.getLastPathSegment());
File dir = new File("data/data/"+ PACKAGE_NAME +"/files/");
dir.mkdirs();
File file = new File("data/data/"+ PACKAGE_NAME +"/files/outFile.pdf");
file.createNewFile();
OutputStream out=new FileOutputStream(file);
byte buf[]=new byte[1024];
int len;
while((len=is.read(buf))>0)
out.write(buf,0,len);
out.close();
is.close();
parcel = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE);
}
catch (Exception e) {
Log.e("Exception",e.toString());
}
return parcel;
}
The problem is that the method openFile() is not called on Samsung Galaxy S II (Android 2.3.5), HCT Wildfire (2.2.1) and HTC Sensation (Android 2.3.4). But the method onCreate() is called on every device.
Anyone that can help, please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论