我在
的app的内部存储空间中有一个 abc.pdf 文件 data/data/com.example.p10documentapp/app_this_app_dir/abc.pdf
我首先将文件复制到catch目录
/data/data/com.example.p10documentapp/cache/data/data/com.example.p10documentapp/app_this_app_dir/ferguson_book.pdf
然后我尝试使用 Intent.ACTION_VIEW
打开abc.pdf我可以在ServiceProvider类的openFile()方法中打开文件。但该文件未通过pdf阅读器开放。请告诉我我做错了什么
MyActivity.class
void startInternt(){
copyFileToInternal("abc.pdf");
Intent intent = new Intent();
File file = new File(activity.getCacheDir(), directory + "/" + item.getName());
Uri uri = FileProvider.getUriForFile( activity, "com.example.p10documentapp", file);
intent.setAction(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(uri, "application/pdf");
try {
activity.startActivity(intent);
}
catch (ActivityNotFoundException e) {
e.printStackTrace();
}
}
private void copyFileToInternal(String name) {
try {
InputStream is = new FileInputStream(directory + "/" + name);
File cacheDir = activity.getCacheDir();
File outFile = new File(cacheDir.getAbsolutePath(), name);
OutputStream os = new FileOutputStream(outFile);
byte[] buff = new byte[1024];
int len;
while ((len = is.read(buff)) > 0) {
os.write(buff, 0, len);
}
os.flush();
os.close();
is.close();
} catch (IOException e) {
e.printStackTrace(); // TODO: should close streams properly here
}
}
ServiceProvider.java类
public class ServiceProvider extends ContentProvider {
static final String PROVIDER_NAME = "com.example.p10documentapp";
static final String URL = "content://" + PROVIDER_NAME + "/this_app_dir";
static final Uri CONTENT_URI = Uri.parse(URL);
private static final String PDFPATH = "public_pdfs/";
private final static String[] COLUMNS = {OpenableColumns.DISPLAY_NAME, OpenableColumns.SIZE};
static final String name = "name";
static final int uriCode = 1;
static final UriMatcher uriMatcher;
static {
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(PROVIDER_NAME, "this_app_dir", uriCode);
uriMatcher.addURI(PROVIDER_NAME, "this_app_dir/*", uriCode);
}
@Nullable
@Override
public ParcelFileDescriptor openFile(@NonNull Uri uri, @NonNull String mode) throws FileNotFoundException {
File cacheDir = getContext().getCacheDir();
File file = new File(cacheDir,uri.getPath());
return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
}
@Override
public boolean onCreate() {
return true;
}
@Nullable
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
return null;
}
@Nullable
@Override
public String getType(Uri uri) {
return "application/pdf";
}
@Nullable
@Override
public Uri insert(Uri uri, ContentValues values) {
return null;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
return 0;
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
return 1;
}
}
AndroidManifest.xlm
<provider
android:name=".ServiceProvider"
android:authorities="com.example.p10documentapp"
android:exported="false"
android:grantUriPermissions="true"
>
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
filepaths.xml文件
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<cache-path name="this_app_dir" path="." />
</paths>