2 つの Android アプリ A と B があります。アプリ A からアプリ B にファイルを渡していますが、アプリ B が URI を取得していることがわかります。FLAG_GRANT_READ_URI_PERMISSIONアプリ A でフラグを設定していて、それがアプリ B 内mFlagsの の値である 1 であることがわかります。これはすべて問題ありませんが、URI からFLAG_GRANT_READ_URI_PERMISSIONを作成しようとすると、例外が発生します。私は何を間違っていますか? FileInputStreamFileNotFoundException (Permission denied)
関連するコード スニペットを次に示します。
アプリ A で:
public void openTest Intent(String filePath) {
Intent testIntent = new Intent("com.example.appB.TEST");
testIntent.addCategory(Intent.CATEGORY_DEFAULT);
testIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
testIntent.setDataAndType(Uri.parse("file://"+filePath),"text/plain");
try {
startActivityForResult(testIntent, OPEN_NEW_TERM);
} catch (ActivityNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
アプリ B:
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (intent.getAction().equals("com.example.appB.TEST")) {
Uri fileUri = intent.getData();
File srcFile = new File(fileUri.getPath());
File destFolder = getFilesDir();
File destFile = new File(destFolder.getAbsolutePath()+srcFile.getName());
try {
copyFile(srcFile,destFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void copyFile(File src, File dst) throws IOException {
InputStream in = new FileInputStream(src); //**this is where it dies**
OutputStream out = new FileOutputStream(dst);
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
in例外が発生するのは、私が作成しているときです。理由について何か考えはありますか?