[英]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.