无法访问创建的内部目录

时间:2017-10-26 04:04:02

标签: java android

当我尝试在storage / emulator / 0 /目录中创建一个目录时,它变得不可见且无法访问。我使用mkdir()创建,当我检查它的存在时,dir.exists()返回true,即使我无法访问它。

我的设备在API 23中运行,因此我在运行时获得了权限,如下所示:

 boolean hasPermission = (ContextCompat.checkSelfPermission(this,
            Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED);
    if (!hasPermission) {
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                REQUEST_READ_STORAGE);
    }

我还创建了onRequestPermissionsResult函数:

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode)
    {
        case REQUEST_READ_STORAGE: {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
            {
                //reload my activity with permission granted or use the features what required the permission
            } else
            {
                Toast.makeText(this, "The app was not allowed to write to your storage. Hence, it cannot function properly. Please consider granting it this permission", Toast.LENGTH_LONG).show();
            }
        }
    }
}

在此功能中,我尝试访问我的目录

 public void makeOutSide(String song, InputStream ins){

    // Create the directory

    File dir = new File(Environment.getExternalStorageDirectory(),"appMemes");
    // If it does not exists, make it.
    if (!dir.exists()) {
        dir.mkdirs(); // Generating the directory
    }
    try {
        // Open the resource
        byte[] buffer = new byte[ins.available()];
        ins.read(buffer);
        ins.close();
        // Burn
        String filename = Environment.getExternalStorageDirectory()
                + "/appMemes/"+song+".mp3";
        FileOutputStream fos = new FileOutputStream(filename);
        fos.write(buffer);
        fos.close();
    } catch (Exception e) {
        System.out.println(e);

    }
}

我的AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.renan.appmemes">

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


<application


...
</application>

1 个答案:

答案 0 :(得分:1)

首先在Oncreate中放入EnableRuntimePermission函数

export RACK_ENV=development

然后使用此行

创建目录
private void EnableRuntimePermission() {
        if (ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.READ_EXTERNAL_STORAGE)
               != PackageManager.PERMISSION_GRANTED) {

            if (ActivityCompat.shouldShowRequestPermissionRationale
                    (MainActivity.this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)) {

                Toast.makeText(MainActivity.this, "Allow permissions", Toast.LENGTH_LONG).show();
            } else {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    requestPermissions(
                            new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, RequestPermissionCode);
                }
            }
        }
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {

            case RequestPermissionCode:

                if (grantResults.length > 0) {

                    boolean writeExternalFile = grantResults[0] == PackageManager.PERMISSION_GRANTED;

                    if (writeExternalFile) {
                    } else {
                        Toast.makeText(MainActivity.this, "Allow permissions to Edit the Image", Toast.LENGTH_LONG).show();
                    }

                }
                break;
        }
    }