本文介绍了Android开发人员:直接在Samsung设备(具有extSdCard路径)上访问外部SD卡?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Android开发,因此相对于某些Android技术概念来说还比较陌生,但我确实有很强的开发人员背景.

I'm learning Android development so am relatively new to some of the technical Android concepts, but I do have a strong developer background.

我正在编写一个应用程序,我希望该应用程序可以直接访问用户SD卡上的文件.测试设备是Samsung Galaxy Tab4.

I am writing an app that I want to be able to directly access files on the user's SD card. The test device is a Samsung Galaxy Tab 4.

三星似乎使用一种自定义方法安装SD卡,将其放置在/storage/extSdCard而不是更传统的/sdcard上.

Samsung appears to use a custom method of mounting the SD card, placing it at /storage/extSdCard rather than the more traditional /sdcard.

在我的应用程序中,我使用方法Environment.getExternalStoragePublicDirectory方法请求存储路径,并获得了指向内部存储而不是SD卡的路径. (/storage/sdcard0/...)

In my app, I used the method Environment.getExternalStoragePublicDirectory method to request the path for storage, and was given a path which points to the internal storage rather than the SD card. (/storage/sdcard0/...)

当我尝试直接访问SD卡存储(/storage/extSdCard/...)时,出现错误open failed: EACCES (Permission denied).

When I try to directly access the SD card storage (/storage/extSdCard/...), I get the error open failed: EACCES (Permission denied).

此设备-以及可能是任何用户的设备-内部存储器上的可用存储空间非常有限. (此设备内部只有8GB的存储空间,其中大部分已由应用程序占用.)此外,我希望潜在的应用程序用户能够将文件直接放入其MicroSD卡中,而不必使用Android文件传输工具将内容加载到应用中...

This device - and potentially any user's device - has very limited storage available on the internal memory. (This device has only 8GB of storage internally, most of which is taken up by apps.) Additionally, I want a potential app user to be able to put files onto their MicroSD card directly and not have to use an Android file transfer tool to load content into the app...

我可以通过三星设备上运行的应用程序直接访问外部SD卡吗?

Any way I can access the external SD card directly from an app running on a Samsung device?

测试代码:

public void doTest()
{
    FileWriter fw;

    File f = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), "test.txt");
    Log.i("file","Trying to write a file at: "+f.getPath());
    try {
        fw = new FileWriter(f);
        fw.write("You got a file.");
        fw.flush();
        fw.close();
        Log.i("file", "Successfully wrote the file.");
    }
    catch (IOException e)
    {
        Log.i("file_error", "Could not write the file.");
        Log.i("file_error",e.getMessage());
    }

    f = new File("/storage/extSdCard/Documents", "test.txt");
    Log.i("file","Trying to write a file at: "+f.getPath());
    try {
        fw = new FileWriter(f);
        fw.write("You got a file.");
        fw.flush();
        fw.close();
        Log.i("file", "Successfully wrote the file.");
    }
    catch (IOException e)
    {
        Log.i("file_error", "Could not write the file.");
        Log.i("file_error",e.getMessage());
    }
}

推荐答案

在Android 4.4+上这是不可能的,因为大多数直接访问可移动存储已被缩减.例外情况包括预安装的应用程序以及在有根设备上运行并使用超级用户特权的应用程序.

That is not possible on Android 4.4+, as most of the direct access to removable storage has been curtailed. Exceptions include pre-installed apps and apps that run on rooted devices and use superuser privileges.

自从Android 2.x开始,

/sdcard尚未在大多数设备上用于可移动媒体.

/sdcard has not been used for removable media since Android 2.x, on most devices.

否,getExternalStoragePublicDirectory()指向外部存储.外部存储不是可移动存储. 内部存储完全是另外一回事.

No, getExternalStoragePublicDirectory() points to external storage. External storage is not removable storage. Internal storage is something else entirely.

欢迎您在Context上使用getExternalFilesDirs()getExternalCacheDirs()getExternalMediaDirs()方法.注意复数方法名称.如果这些方法返回2个以上的条目,则第二个及随后的条目是可移动存储上的可读取和写入的位置,无需权限.但是,您不能通过这种方式访问​​所有可移动存储,而只能访问特定于您的应用程序的位置.

You are welcome to use the getExternalFilesDirs(), getExternalCacheDirs(), and getExternalMediaDirs() methods on Context. Note the plural method names. If those methods return 2+ entries, the second and subsequent ones are locations on removable storage that you can read from and write to, no permissions required. However, you cannot access all of removable storage this way, just a location specific for your app.

也欢迎您使用存储访问框架 ,使用户可以选择内容的去向,包括将其放置在可移动存储中.但是,您不再使用文件和路径,而是使用内容和Uri值.

You are also welcome to use the Storage Access Framework, allowing the user to pick where the content goes, including placing it on removable storage. However, you are no longer working with files and paths, but with content and Uri values.

N Developer Preview为作用域目录访问"提供了另一个选项,该选项支持可移动存储.但是,这仍然是当前(2016年3月)开发人员预览版的一部分.

The N Developer Preview offers another option for "Scoped Directory Access", which supports removable storage. However, this is still part of a developer preview at the present time (March 2016).

这篇关于Android开发人员:直接在Samsung设备(具有extSdCard路径)上访问外部SD卡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 11:07