当前位置: 首页 > 知识库问答 >
问题:

如何在运行KitKat的设备上使用新的LollipopSD卡访问API?

富念
2023-03-14
public class MyActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
    startActivityForResult(intent, 42);
}

public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
    if (resultCode == RESULT_OK) {
        Uri treeUri = resultData.getData();
        DocumentFile pickedDir = DocumentFile.fromFile(new File("/mnt/extSdCard/Test"));

        // List all existing files inside picked directory
        for (DocumentFile file : pickedDir.listFiles()) {
            Log.d("Tag", "Found file " + file.getName() + " with size " + file.length());
        }

        // Create a new file and write into it
        DocumentFile newFile = pickedDir.createFile("text/plain", "My Novel");
        OutputStream out = null;
        try {
            out = getContentResolver().openOutputStream(newFile.getUri());
            out.write("A long time ago...".getBytes());
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.my, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

我尝试了,但说CreateFile失败:java.io.ioException:open失败:EACCES(权限被拒绝)

即使在添加权限后也使用-permissionandroid:name=“android.permission.write_external_storage”

Android版本4.4.2

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);

    DocumentFile pickedDir = DocumentFile.fromFile(new File("/mnt/extSdCard/Test"));

    // List all existing files inside picked directory
    for (DocumentFile file : pickedDir.listFiles()) {
        Log.d("Tag", "Found file " + file.getName() + " with size " + file.length());
    }

    // Create a new file and write into it
    DocumentFile newFile = pickedDir.createFile("text/plain", "My Novel");
    OutputStream out = null;
    try {

        //Says NullPointerException
        out = getContentResolver().openOutputStream(newFile.getUri());                             out.write("A long time ago...".getBytes());
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }        
}

共有1个答案

卜泓
2023-03-14

新的API只帮助Lollipop上的人写到二级sdcard,KitKat上的人仍然不走运。

DocumentFile.FromFile(...)

除了应用程序已有的文件之外,这并不给你任何额外的底层文件访问权限。

 类似资料:
  • 从Lollipop开始,应用程序可以访问真正的SD卡(在Kitkat上无法访问,之前的版本还没有正式支持),正如我在这里询问的那样。 因为现在已经很少看到支持SD卡的Lollipop设备了,因为模拟器没有这个功能(或者真的有吗?)为了模拟SD卡支持,我花了很长时间来测试它。 不管怎样,似乎不是使用普通的文件类来访问SD卡(一旦获得了权限),而是需要使用URI,使用DocumentFile。 这限制

  • 在Android4.4(KitKat)上,谷歌对SD卡的访问进行了严格的限制。 从Android Lollipop(5.0)开始,开发人员可以使用一个新的API,它要求用户确认是否允许访问特定的文件夹,正如Google-Groups的帖子所写的那样。 null null 关于新的API我有几个问题: 如何真正使用它? 根据这篇文章,操作系统会记住应用程序被授予了访问文件/文件夹的权限。如何检查是否

  • 使用Android4.4版本(KitKat),可以使用以下命令从命令行使用ADB记录Android设备的屏幕。 但这只在Android版(KitKat)和5.0版(Lolipop)有效 是否有任何其他命令或方法可以使用ADB在Android版本4.4(KitKat)下录制视频?

  • 类似基于 Chromium 的浏览器一样, Electron 也提供了通过 web API 访问设备硬件的方法。 大部分接口就像在浏览器调用的 API 一样,但有一些差异需要考虑到。 Electron和浏览器之间的主要区别是请求访问设备时发生的情况。 在浏览器中,用户可以在弹出窗口中允许访问单独的设备。 在 Electron API中,提供了可供开发者自动选择设备或提示用户通过开发者创建的接口选择

  • 问题内容: 我开始使用GDK开发Google Glass应用程序。 我将Eclipse与Android SDK Manager结合使用来安装GDK。 在那之后,我输入以下提到的步骤的谷歌眼镜项目在这里: 单击 文件 >新建项目> Android示例项目 选择 Glass Development Kit 作为构建目标,然后单击Next。 选择 计时器 示例,然后单击 完成 。 在Eclipse Pa

  • 我想连接一个android设备直接从我的环境android studio测试我的应用程序 android Studio-http://developer.android.com/sdk/index.html 我有一个三星galaxy s5设备,我已经把它连接到usb上,我已经安装了这个驱动程序: http://developer.samsung.com/technical-doc/view.do?