我试图在 Android 日志中打印或写入受保护的文件 /data/data/com.package/file。

huangapple 未分类评论44阅读模式
标题翻译

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(&quot;/data/data/mypackge/test&quot;);
            f.setReadable(true);

Log.d(&quot;TEST&quot;, 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.&lt;init&gt;(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), &quot;path-to-your-file&quot;);

This should safely create the link to the file.

Edit

If you want the data directory, use getInternalFilesDir()

huangapple
  • 本文由 发表于 2020年3月16日 13:04:53
  • 转载请务必保留本文链接:https://java.coder-hub.com/60700575.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定