标题翻译
I am trying to print or write protected file /data/data/com.package/file in android logs
问题
我尝试过
File f = new File("/data/data/mypackage/test");
f.setReadable(true);
Log.d("TEST", MainActivity.getFileContents(f));
} catch (Exception e) {
e.printStackTrace();
finish();
但是出现了错误
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
我在我的代码中尝试了很多更改,但无法解决这个问题。如果有人知道解决方案,请回复这个帖子。欢迎来自Android 5.1到9的任何Android版本的答案。我的应用程序需要在中央日志中写入应用程序目录/data/data/myapp/file内的文件,请帮忙,谢谢!
英文翻译
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.
答案1
得分: 0
有两件事情你需要做,首先,检查 f.exists() 是否存在
if(!f.exists()) return;
然后,你需要使用 new File(getExternalFilesDir(null), "path-to-your-file");
这样应该安全地创建文件的链接。
编辑
如果你想要数据目录,使用 getInternalFilesDir()
英文翻译
There are 2 things you need to do, first, check for f.exists()
if(!f.exists()) return;
Then, you need to make use of the 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()
专注分享java语言的经验与见解,让所有开发者获益!
评论