繁体   English   中英

Android 6-以编程方式将应用程序文件复制到另一个文件夹

[英]Android 6 - Programmatically copy an app file to another folder

我有一个在Android 6中运行的应用程序,该应用程序从BLE设备流式传输数据。 当使用类似的东西时,这工作正常:

myFile =新文件(getExternalFilesDir(filepath),MyData);

生成的文件位于如下所示的位置:

文件路径:/ storage / emulated / 0 / Android / data / my_package / files / my_file

我想将此文件复制到Android / data目录下未埋藏的新文件夹中,以便用户可以轻松找到和检索通过USB电缆连接到手机/平板电脑的数据。 到目前为止,我还无法弄清楚该怎么做...或者甚至是不可能的。 我尝试执行的任何操作似乎都会导致权限异常。

我希望该文件夹与〜/ Android / data处于同一级别。 如果没有,还有其他选择吗? 例如将数据放在SD卡上。

我已经阅读了许多有关Android文件系统的文章和文章。 这一切都很混乱和模糊。 Android的新版本似乎改变了工作方式。 如果有人知道与Android 6(棉花糖)相关的清晰明了的解释(甚至有可行的示例!),请告诉我。

谢谢,马克斯

出于某些安全原因,Android 6.0限制获取运行时权限。 因此,下面的代码将帮助您获得许可。

注意不要从清单中删除权限给定的以下过程仅适用于6.0,其他Android OS渗透将从清单中授予

public static final int galleryPermissionRequestCode=4;


public void chkForPermissoins(){



if (Build.VERSION.SDK_INT >= 23) {
                    //do your check here

                    isStoragePermissionGranted(camPermissionRequestCode);

                } else {

                   //You already have the permission because the os you appp is running on is less tahn 23 (6.0)

                }


}


public  boolean isStoragePermissionGranted(int requsetCode) {
        if (Build.VERSION.SDK_INT >= 23) {
            if (ContextCompat.checkSelfPermission(getActivity(),android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
                    == PackageManager.PERMISSION_GRANTED) {

               //Now you have permsssion
                return true;
            } else {


                ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, requsetCode);
                return false;
            }
        }
        else { //permission is automatically granted on sdk<23 upon installation

           //Now you have permsssion
            return true;
        }


    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(grantResults[0]== PackageManager.PERMISSION_GRANTED){

        //Now you have permsssion
         //resume tasks needing this permission
        }
    }

我使用了Adeel Turk的许可建议。 因为我仅使用Android 6(API 23),所以不需要检查构建版本。

//
// Get storage write permission
//
public  boolean isStoragePermissionGranted(int requestCode) {
    if (ContextCompat.checkSelfPermission(MyActivity.this,android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
            == PackageManager.PERMISSION_GRANTED) {

        //Now you have permission
        return true;
    } else {


        ActivityCompat.requestPermissions(MyActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, requestCode);
        return false;
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {

        //Now you have permission

        // Check file copy generated the request
        // and resume file copy
        if(requestCode == WFileRequest)
            try {
                copyFile(OriginalDataFileName);
            } catch (IOException e) {
                e.printStackTrace();
            }
    }
}

尽管这解决了权限异常,但它没有回答如何在默认应用程序目录之外创建文件夹的问题。 以下代码获得许可,并在/ storage / emulated / 0处创建一个名为AppData的文件夹,该文件夹出现在设备存储的顶层。 就像Adeel Turk的示例一样,权限请求代码WFileRequest被设置为4,但是我知道您可以使用任何数字。 权限请求回调然后检查请求代码,并使用最初写入的文件名再次调用copyFile例程。

大部分代码使用了该论坛其他帖子中的示例,这些示例如何在android External Storage Directory中创建文件夹? 以及如何以编程方式将文件复制到另一个目录?

    public void copyFile(String SourceFileName) throws FileNotFoundException, IOException
{
    String filepath = "";
    //
    // Check permission has been granted
    //
    if (isStoragePermissionGranted(WFileRequest)) {
        //
        // Make the AppData folder if it's not already there
        //
        File Directory = new File(Environment.getExternalStorageDirectory() + "/AppData");
        Directory.mkdirs();

        Log.d(Constants.TAG, "Directory location: " + Directory.toString());
        //
        // Copy the file to the AppData folder
        // File name remains the same as the source file name
        //
        File sourceLocation = new File(getExternalFilesDir(filepath),SourceFileName);
        File targetLocation = new File(Environment.getExternalStorageDirectory() + "/AppData/" + SourceFileName);

        Log.d(Constants.TAG, "Target location: " + targetLocation.toString());

        InputStream in = new FileInputStream(sourceLocation);
        OutputStream out = new FileOutputStream(targetLocation);

        // Copy the bits from instream to outstream
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM