我已经实现了以下功能,它在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;
}
问题是三星Galaxy S II(Android 2.3.5),HCT Wildfire(2.2.1)和HTC Sensation(Android 2.3.4)上的 openFile()方法不被称为 。但是在每个设备上调用onCreate()方法。
有人可以提供帮助吗?