简体   繁体   English

写入Android 7上的外部存储

[英]Write to external storage on Android 7

I want to write to the external storage in my Android app, but I don't seem to get the permissions right. 我想写入我的Android应用程序中的外部存储,但似乎没有正确的权限。 The App runs on Android 7, so I request the permissions on runtime like this: 该应用程序在Android 7上运行,因此我要求在运行时提供如下权限:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
permission = ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);

if (permission != PackageManager.PERMISSION_GRANTED) {
    // We don't have permission so prompt the user
    int ACCESS_EXTERNAL_STORAGE_STATE = 1;
    ActivityCompat.requestPermissions(this,
            new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
            ACCESS_EXTERNAL_STORAGE_STATE);
}

permission = ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);

if (permission != PackageManager.PERMISSION_GRANTED) {
    // We don't have permission so prompt the user
    int ACCESS_EXTERNAL_STORAGE_STATE = 1;
    ActivityCompat.requestPermissions(this,
            new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
            ACCESS_EXTERNAL_STORAGE_STATE);
}
...
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode)
        {
            case 1: {
                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();
                }
            }
        }
    }

One observation I made is that onRequestPermissionsResult seems to return that the permission was not granted, but I was also never prompted when starting the app to permit writing to external storage. 我发现,onRequestPermissionsResult似乎返回未授予权限的信息,但是在启动应用程序以允许写入外部存储时也从未提示我。

I set these permissions in the Manifest: 我在清单中设置了以下权限:

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

The code to write to the storage is here: 写入存储的代码在这里:

public static void appendByteBuffer(byte[] data)
{
    String filename = Environment.getExternalStorageDirectory().getAbsolutePath()+"/test.raw";
    //filename value is "/storage/emulated/0/test.raw"
    File rawAudioFile = new File(filename);
    if (!rawAudioFile.exists())
    {
        try
        {
            rawAudioFile.createNewFile();
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    try (FileOutputStream output = new FileOutputStream(filename, true)) {
        output.write(data);
    } catch (IOException e) {}
}

03-16 14:50:49.988 19145-19306/? 03月16日14:50:49.988 19145-19306 /? W/System.err: java.io.IOException: Permission denied 03-16 14:50:49.988 19145-19306/? W / System.err:java.io.IOException:权限被拒绝03-16 14:50:49.988 19145-19306 /? W/System.err: W / System.err:
at java.io.UnixFileSystem.createFileExclusively0(Native Method) 03-16 14:50:49.988 19145-19306/? 在java.io.UnixFileSystem.createFileExclusively0(本机方法)03-16 14:50:49.988 19145-19306 /? W/System.err: at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:280) 03-16 14:50:49.989 19145-19306/? W / System.err:位于java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:280)03-16 14:50:49.989 19145-19306 /? W/System.err: at java.io.File.createNewFile(File.java:948) 03-16 14:50:49.989 19145-19306/? W / System.err:位于java.io.File.createNewFile(File.java:948)03-16 14:50:49.989 19145-19306 /? W/System.err: at com.meeter.study.AppUtils.appendByteBuffer(AppUtils.java:76) 03-16 14:50:49.989 19145-19306/? W / System.err:at com.meeter.study.AppUtils.appendByteBuffer(AppUtils.java:76)03-16 14:50:49.989 19145-19306 /? W/System.err: at W / System.err:位于

Use ContextCompat instead of ActivityCompat for checkSelfPermission : ContextCompat代替ActivityCompat用于checkSelfPermission

Replace: 更换:

permission = ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
permission = ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);

With: 带有:

permission = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
permission = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);

Then, Add onRequestPermissionsResult method to handle ALLOW, DENY selection. 然后,添加onRequestPermissionsResult方法以处理ALLOW,DENY选择。 Hope this helps. 希望这可以帮助。

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case ACCESS_EXTERNAL_STORAGE_STATE: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                   // Write to the storage (ex: call appendByteBuffer(byte[] data) here)

                } else {
                    Toast.makeText(getApplicationContext(), "Please grant permission.", Toast.LENGTH_LONG).show();
                }
                break;
            }
}

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

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