简体   繁体   English

我正在尝试在 android 日志中打印或写入受保护的文件 /data/data/com.package/file

[英]I am trying to print or write protected file /data/data/com.package/file in android logs

I tried我试过

File f = new File("/data/data/mypackge/test");
            f.setReadable(true);

Log.d("TEST", MainActivity.getFileContents(f));
        } catch (Exception e) {
            e.printStackTrace();
            finish();

but its gives error但它给出了错误

java.io.FileNotFoundException: /data/data/mypackage/test: open failed: EACCES (Permission denied)
03-16 00:53:19.897  4231  4231 W System.err:    at libcore.io.IoBridge.open(IoBridge.java:452)
03-16 00:53:19.897  4231  4231 W System.err:    at java.io.FileInputStream.<init>(FileInputStream.java:76)
03-16 00:53:19.897  4231  4231 W System.err:    at com.mypackage.MainActivity.getFileContents(MainActivity.java:58)
03-16 00:53:19.897  4231  4231 W System.err:    at com.mypackage.MainActivity.onCreate(MainActivity.java:50)
03-16 00:53:19.897  4231  4231 W System.err:    at android.app.Activity.performCreate(Activity.java:6237)
03-16 00:53:19.897  4231  4231 W System.err:    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
03-16 00:53:19.897  4231  4231 W System.err:    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
03-16 00:53:19.897  4231  4231 W System.err:    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
03-16 00:53:19.897  4231  4231 W System.err:    at android.app.ActivityThread.-wrap11(ActivityThread.java)
03-16 00:53:19.897  4231  4231 W System.err:    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
03-16 00:53:19.897  4231  4231 W System.err:    at android.os.Handler.dispatchMessage(Handler.java:102)
03-16 00:53:19.897  4231  4231 W System.err:    at android.os.Looper.loop(Looper.java:148)
03-16 00:53:19.897  4231  4231 W System.err:    at android.app.ActivityThread.main(ActivityThread.java:5417)
03-16 00:53:19.897  4231  4231 W System.err:    at java.lang.reflect.Method.invoke(Native Method)
03-16 00:53:19.897  4231  4231 W System.err:    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
03-16 00:53:19.897  4231  4231 W System.err:    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
03-16 00:53:19.897  4231  4231 W System.err: Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
03-16 00:53:19.897  4231  4231 W System.err:    at libcore.io.Posix.open(Native Method)
03-16 00:53:19.897  4231  4231 W System.err:    at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
03-16 00:53:19.897  4231  4231 W System.err:    at libcore.io.IoBridge.open(IoBridge.java:438)
03-16 00:53:19.897  4231  4231 W System.err:    ... 15 more

I tried most of changes in my code but can't get that please any one have solution answer to this thread any android version answer are welcome from android 5.1 to 9 my app has to write a file inside my app directory /data/data/myapp/file in central logs please help any one know this.我在我的代码中尝试了大部分更改,但无法得到任何人对此线程的解决方案答案欢迎任何 android 版本的答案从 android 5.1 到 9 我的应用程序必须在我的应用程序目录 /data/data/ 中写入一个文件中央日志中的 myapp/file 请帮助任何人知道这一点。

There are 2 things you need to do, first, check for f.exists() if(!f.exists()) return;你需要做两件事,首先,检查 f.exists() if(!f.exists()) return;

Then, you need to make use of the new File(getExternalFilesDir(null), "path-to-your-file");然后,您需要使用new File(getExternalFilesDir(null), "path-to-your-file");

This should safely create the link to the file.这应该安全地创建文件的链接。

Edit编辑

If you want the data directory, use getInternalFilesDir()如果需要数据目录,请使用getInternalFilesDir()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在尝试创建一个 android 应用程序,它使用符号链接复制另一个应用程序中的文件并在中央 Android 日志中打印 - I am trying to create a android app which copy a file in another app using symlink and print in central Android logs 我无法将所有数据写入文件 - I am not able to write all data to a file 尝试将数据写入.txt文件。 我究竟做错了什么? - Trying to write the data to a .txt file. What am I doing wrong? 我正在尝试使用文件中的数据创建数组 - I am trying to create an array using data from a file 我可以在没有root的情况下将txt文件写入data \\ data \\ com.example.app(其他应用)\\文件吗? - Can i write txt file into data\data\com.example.app(other app)\files without root? 我正在尝试使用Java将MySql数据导出到文件中,但是我无法获取表头 - I am trying to export MySql data to a file using Java, but I am not able to get the table headers 如何写入android中数据目录之外的文件? - How do I write to a file outside of the data directory in android? 我正在尝试写入已经包含信息的文件 - I am trying to write into a file which already contains information 我正在尝试从数据库中提取数据并将其存储到 xml 文件中。 我正在使用 Sql 查询“Select * from”来提取数据 - I am trying to pull data from a database and store it into a xml file. I am using a Sql query "Select * from "for pulling data 我正在尝试将数据输入parse.com中的custmom列,但出现错误 - I am trying to enter data to a custmom column in parse.com ,but I am getting an error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM