我正在尝试显示存储在我的Android应用程序资产文件夹中的“ NEFT.pdf”文件。
以下代码在API 25之前绝对可以正常工作
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();
}
}
此代码不适用于API 25及更高版本。它给出了不再支持的错误 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在我看来是正确的。任何人都可以帮助我在这里找到错误。
这是清单文件中的 provider 元素。
<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>
这是 provider_paths.xml 文件
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
<files-path name="pdf_files" path="pdf/"/>
这是显示pdf文件的Android代码
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();
}
}
因为您想在单独的应用程序(例如Adobe Reader)中显示PDF文件,所以我希望执行以下操作:
private void CopyReadAssets()
{
AssetManager assetManager = getActivity().getAssets();
InputStream in = null;
OutputStream out = null;
String state = Environment.getExternalStorageState();
if (!Environment.MEDIA_MOUNTED.equals(state)) {
Toast.makeText(getActivity(), "External Storage is not Available", Toast.LENGTH_SHORT).show();
}
File pdfDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/PDFs");
if (!pdfDir.exists()) {
pdfDir.mkdir();
}
File file = new File(pdfDir + "/abc.pdf");
try
{
in = assetManager.open("abc.pdf");
out = new BufferedOutputStream(new FileOutputStream(file));
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch (Exception e)
{
Log.e("tag", e.getMessage());
}
if (file.exists()) //Checking for the file is exist or not
{
Uri path = Uri.fromFile(file);
Intent objIntent = new Intent(Intent.ACTION_VIEW);
objIntent.setDataAndType(path, "application/pdf");
objIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent intent1 = Intent.createChooser(objIntent, "Open PDF with..");
try {
startActivity(intent1);
} catch (ActivityNotFoundException e) {
Toast.makeText(getActivity(), "Activity Not Found Exception ", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getActivity(), "The file not exists! ", Toast.LENGTH_SHORT).show();
}
}
要将文件复制到设备html" target="_blank">内存中:
private void copyFile(InputStream in, OutputStream out) throws IOException
{
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1)
{
out.write(buffer, 0, read);
}
}
使用以下权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
null 我在用这个代码 但我一直得到这个错误
该文件位于我的资产文件夹下。因此,我正在设置WebViewClient并将我的页面加载到其中。但是我得到一个“网页不可用”的错误。 以下是我的活动代码: 和布局文件:
似乎现在Android N需要FileProvider,所以我正在尝试实现FileProvider以将文件从网络保存到本地临时位置,然后我需要读取此临时文件。 我这样做是为了设置FileProvider: 显示xml: 然后我有一个提供者路径。我的res/xml文件夹中的xml文件,包括: 最后,这是我必须创建临时文件的java代码: 的行抛出异常 对我做错了什么有什么建议吗?非常感谢。 更新/编
我需要从我的图库中选择一张图片,然后放到我的baasbox服务器上。为此,我需要一个图像的输入流。 BaasBox Docs报告如下: 所以,我在网上搜索了怎么能做到这一点。 所以,当它开始时,我从gallery中选择一张图片并上传到b()start。。但它在运行时会返回以下错误代码(应用程序不会崩溃): D/log: Uri is:content://com.android.providers.
我是Python的新手,所以请原谅下面的基本代码和问题,但是我一直在努力找出是什么导致了我得到的错误。 以下是我正在努力做的: 循环浏览特定文件夹 迭代该特定文件夹中的每个csv文件 计算 这是我的代码: 以下是我得到的错误: 我已确认文件夹和文件确实存在。IOError的原因是什么?如何解决?另外,我的代码是否还有其他问题会阻止我执行整个任务? 提前谢谢