ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

如何在运行KITKAT的设备上使用新的Lollipop SD卡访问API?

2019-08-30 09:34:53  阅读:197  来源: 互联网

标签:android java android-sdcard


Kit-Kat问题无法写入外部SD卡,

Goolge Document中所述为了简化运行KITKAT或更早版本的设备上的代码,您可以使用fromFile(File)来模拟DocumentsProvider的行为
下面的代码(新API)适用于Lollipop,但如何使用kitkat的新API?

另见Kit-Kat issue (New API)

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);
}
}

如何使用fromFile(File)?

我试过但说无法创建文件:java.io.IOException:打开失败:EACCES(权限被拒绝)

即使在添加权限之后
     uses-permission android: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();
    }        
}

解决方法:

新的API只能帮助Lollipop上的人写入二级sdcard,KitKat上的人仍然是out of luck.

07001

This doesn’t give you any additional
access to the underlying files beyond what your app already has.

利用Intent.ACTION_OPEN_DOCUMENT_TREE创建的新权限
你必须使用DocumentFile.fromTreeUri返回的Uri

标签:android,java,android-sdcard
来源: https://codeday.me/bug/20190830/1767248.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有