对 android 文件系统的写入权限 [英] Writing permission on android filesystems

查看:32
本文介绍了对 android 文件系统的写入权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该在 android 数据文件夹中生成一个 .pdf 文件以使用下面的 Java 代码,并在 XML 清单文件中启用权限.但是当我运行代码时,我有以下异常.该应用程序在清单中具有不同的权限,应该全部正确配置,我声明我正在旧的 Android 4 上测试该应用程序.我该如何解决这个问题?这是因为什么?异常:错误:java.I. FileNotFoundException:/data/my.pdf:打开失败:EACCES(权限被拒绝)

I should generate a .pdf file inside the android data folder to use the Java code below, with the permissions enabled in the XML manifest file. But when I run the code I have the following exception. The application has different permissions within the manifest, It should all be configured correctly, I state that the application I'm testing on an old Android 4. How can I solve this? and what is it due to? Exception: error: java. I. FileNotFoundException: /data/my.pdf: open failed: EACCES (Permission denied)

代码:

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE " />

    public static Boolean GeneratePDF(String base64) {
        Boolean ret = true;

        try {
       String direttorio=""+Environment.getDataDirectory().getAbsolutePath();
        final File dwldsPath = new File(direttorio + "/" + "my.pdf");
        byte[] pdfAsBytes = Base64.decode(base64, 0);
        FileOutputStream os;
        os = new FileOutputStream(dwldsPath, false);
        os.write(pdfAsBytes);
        os.flush();
        os.close();
    } catch (Exception ex) {
        System.out.println("\n Errore Generazione File: "+ex);
        ret = false;
    }
    return ret;

    }

推荐答案

替换这一行

String direttorio=""+Environment.getDataDirectory().getAbsolutePath();

致:

String direttorio= Environment.getExternalStorageDirectory().getAbsolutePath();
final File dwldsPath = new File(direttorio + "/" + my.pdf");

这篇关于对 android 文件系统的写入权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆