내 안드로이드 앱의 자산 폴더에 저장된 "NEFT.pdf"파일을 표시하려고합니다.
다음 코드는이 코드는 API 25 이상에서 작동하지 않는 API 25fileProvider를 사용하여 assets 폴더에서 PDF 파일을 열려고했지만 FileNotFoundException이 발생합니다. 해당 파일 또는 디렉토리가 없습니다.
private void CopyReadAssets(String filename) {
AssetManager assetManager = getAssets();
InputStream in = null;
OutputStream out = null;
File file = new File(getFilesDir(), filename);
try {
in = assetManager.open(filename);
out = openFileOutput(file.getName(), Context.MODE_WORLD_READABLE);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(
Uri.parse("file://" + getFilesDir() + "/"+filename), "application/pdf");
startActivity(intent);
} catch (Exception e)
{
Toast.makeText(PdfFilesList.this, "cra: "+e.toString(), Toast.LENGTH_SHORT).show();
}
}
까지 절대적으로 잘 작동합니다. MODE_WORLD_READABLE 오류가 더 이상 지원되지 않습니다.
나는 MODE_PRIVATE로 변경하지만 그건 나에게
android.os.fileuriexposedexception가 intent.getdata를 통해 응용 프로그램을 넘어 노출 다른 오류()을 제공합니다.
그래서 나는 Developer.Android.com에 설명 된 개념을 적용했다.
E/DisplayData : openFd : java.io.FileNotFoundException :
이 내가 오류 로그에 무엇을 얻을 아니오 같은 파일 또는 디렉터리
E/PdfLoader : 파일을로드 할 수 없습니다 (열리지 않습니다) 데이터 표시 [PDF : NEFT.pdf] + ContentOpenable, uri : content : //com.user.plansmart.provider/pdf_files/NEFT.pdf
uri가 올바르게 보입니다. 누구든지 여기서 오류를 찾는데 도와 줄 수 있습니까?
여기 공급자 요소가 매니페스트 파일에 있습니다. 당신이 PDF를 표시 싶어요 원인
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
이
이 PDF 파일private void displayFile(String filename) {
try {
File filePath = new File(getApplicationContext().getFilesDir(), "pdf");
File newFile = new File(filePath, filename);
//new version
Uri fileUri = FileProvider.getUriForFile(PdfFilesList.this,
BuildConfig.APPLICATION_ID + ".provider", newFile);
getApplicationContext().grantUriPermission(PACKAGE_NAME,
fileUri, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(fileUri, "application/pdf");
intent.setFlags(FLAG_GRANT_READ_URI_PERMISSION | FLAG_GRANT_WRITE_URI_PERMISSION);
startActivity(intent);
}catch (Exception e){
e.printStackTrace();
Toast.makeText(PdfFilesList.this, "df "+e.toString(), Toast.LENGTH_SHORT).show();
}
}
다음은 사용할 수 없습니다 자민련이 (가) 다음을 수행 선호 FileProvider 클래스를 제공합니다. 재고. Recode. – greenapps
'getFilesDir(), "pdf");'. 뭐라 했니? 자산? 아닙니다. 자산이 아닙니다. getFilesDir()은 비공개 내부 저장소입니다. 게시물을 수정하십시오. 제목으로 시작합니다. – greenapps
파일이 자산입니까? 그런 다음 getFilesDir()을 사용할 수 없습니다. 당신이 가지고있는 것을 말하십시오. – greenapps