是否可以在没有root权限的情况下读取单独复制到Android SD卡的数据?

发布于 2024-08-31 10:08:24 字数 959 浏览 3 评论 0原文

编辑:这是用户错误(抱歉)。我已经在下面回答了。

我正在开发一个需要访问SD卡上数据的应用程序。当我在我的开发设备(带有 Android 2.1 的 odroid)上运行时,我具有 root 访问权限,并且可以使用以下方式构建路径:

File sdcard = Environment.getExternalStorageDirectory();
String path = sdcard.getAbsolutePath() + File.separator + "mydata"
File data = new File(path);
File[] files = data.listFiles(new FilenameFilter() {
    @Override
    public boolean accept(File dir, String filename) {
        return filename.toLowerCase().endsWith(".xyz");
    }});

但是,当我将其安装在没有 root 访问权限的手机(2.1)上时,我得到 files == null 。我认为这是因为我没有正确的权限从 SD 卡读取数据。当我试图列出 /sdcard 上的文件时,我也会得到 files == null 。因此,如果没有我构建的路径,这同样适用。

此外,该应用程序并不打算通过应用程序商店分发,并且需要使用单独复制到 SD 卡的数据,因此这是一个真实的用例。放入 res/raw 的数据太多了(我已经尝试过,但没有成功)。

我也尝试添加:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

到清单中,即使我只想读取SD卡,但它没有帮助。我还没有找到读取存储的权限类型。

可能有一个正确的方法可以做到这一点,但我还没有找到它。任何提示都会有用。

Edit: This was user error (sorry). I have answered it below.

I am developing an application that needs to access data on the sd card. When I run on my development device (an odroid with Android 2.1) I have root access and can construct the path using:

File sdcard = Environment.getExternalStorageDirectory();
String path = sdcard.getAbsolutePath() + File.separator + "mydata"
File data = new File(path);
File[] files = data.listFiles(new FilenameFilter() {
    @Override
    public boolean accept(File dir, String filename) {
        return filename.toLowerCase().endsWith(".xyz");
    }});

However, when I install this on a phone (2.1) where I do not have root access I get files == null. I assume this is because I do not have the right permissions to read the data from the sd card. I also get files == null when just trying to list files on /sdcard. So the same applies without my constructed path.

Also, this app is not intended to be distributed through the app store and is needs to use data copied separately to the sd card so this is a real use-case. It is too much data to put in res/raw (I have tried, it did not work).

I have also tried adding:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

to the manifest, even though I only want to read the sd card, but it did not help. I have not found a permission type for reading the storage.

There is probably a correct way to do this, but I haven't been able to find it. Any hints would be useful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忆悲凉 2024-09-07 10:08:24

所以我只花了半个小时写了这个问题,然后我断开手机并连接它,而没有将其安装为驱动器。

这就是问题所在。现在可以了。

抱歉浪费了您的时间。 :/

So I just spent half an hour to write this question and then I disconnected the phone and connected it whithout mounting it as a drive.

This was the problem. Now it works.

Sorry for wasting your time. :/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文