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

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

概述Kit-Kat问题无法写入外部SD卡,如GoolgeDocument中所述为了简化运行KITKAT或更早版本的设备上的代码,您可以使用fromFile(File)来模拟DocumentsProvider的行为下面的代码(新API)适用于Lollipop,但如何使用kitkat的新API?另见Kit-Katissue(NewAPI)publicclassMyActivityex

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

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

另见Kit-Kat issue (New API)

public class MyActivity extends Activity {@OverrIDeprotected 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();        }    }}@OverrIDepublic 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;}@OverrIDepublic 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

总结

以上是内存溢出为你收集整理的如何在运行KITKAT的设备上使用新的Lollipop SD卡访问API?全部内容,希望文章能够帮你解决如何在运行KITKAT的设备上使用新的Lollipop SD卡访问API?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1107594.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存