【问题标题】:Android M and above storage permission issueAndroid M及以上存储权限问题
【发布时间】:2018-03-15 22:26:51
【问题描述】:

我的应用程序用于从用户获取文本(字符串)并将其保存在内部和/或外部存储中,从中读取,问题开始于大约两个日期,当时我尝试在使用的虚拟设备上运行应用程序OS android 7.0.1“Nougat”和应用程序跳过/忽略写入外部存储权限,即使我看到权限对话框在我点击外部缓存按钮时出现

我的问题是:这是否意味着android OS android marshmallow 及以上版本不需要创建权限方法?


看这个 GIF 来联系

整个代码

    public class MainFragment extends android.app.Fragment {

    private View view;
    private EditText userName2;
    private Button internalButton, externalButton, showData;
    FileOutputStream fileOutputStream;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_main, container, false);
        userName2 = view.findViewById(R.id.editTextUserName);
        internalButton = view.findViewById(R.id.internalButton);
        externalButton = view.findViewById(R.id.externalButton);
        showData = view.findViewById(R.id.showData);

        internalButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                saveToInternalCache();
            }
        });

        externalButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                enableRunTimePermisstion();


            }
        });

        showData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), DataActivity.class);
                startActivity(intent);
            }
        });

        return view;

    }


    @TargetApi(23)
    public void enableRunTimePermisstion() {
        if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) &&
                (getContext().checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) !=
                        PackageManager.PERMISSION_GRANTED))
            if (getActivity().shouldShowRequestPermissionRationale
                (Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            Toast.makeText(getActivity(), "Write storage permission is need for app"
                    , Toast.LENGTH_LONG).show();
        } else {
            requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
            Toast.makeText(getActivity(), "request permission"
                    , Toast.LENGTH_LONG).show();
        }
        saveToExternalCache();

    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 1) {
            if (permissions[0].equals(Manifest.permission.WRITE_EXTERNAL_STORAGE) &&
                    grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(getActivity(), "Write external storage granted", Toast.LENGTH_LONG).show();
                saveToExternalCache();
            }
        } else {
            Toast.makeText(getActivity(), "Write external permission denied", Toast.LENGTH_SHORT).show();
            enableRunTimePermisstion();
        }
    }


    public void saveToInternalCache() {
        String userName = userName2.getText().toString();
        File path = getActivity().getCacheDir();
        File file = new File(path, "dataInternal.txt");
        try {
            fileOutputStream = new FileOutputStream(file);
            fileOutputStream.write(userName.getBytes());
            Toast.makeText(getActivity(), "Data stored successfully" + file.getAbsolutePath(), Toast.LENGTH_LONG).show();
        } catch (IOException e) {
            Log.e("IOException", e.toString());
        } finally {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                Log.e("IOException", e.toString());
            }
        }


    }

    public void saveToExternalCache() {
        String userName = userName2.getText().toString();
        File path = getActivity().getExternalCacheDir();
        File file = new File(path, "dataExternal.txt");
        try {
            fileOutputStream = new FileOutputStream(file);
            fileOutputStream.write(userName.getBytes());
            Toast.makeText(getActivity(), "Data stored successfully"
                    + file.getAbsolutePath(), Toast.LENGTH_LONG).show();
        } catch (IOException e) {
            Log.e("IOException", e.toString());
        } finally {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                Log.e("IOException", e.toString());
            }
        }

    }

}

【问题讨论】:

    标签: android android-fragments android-permissions android-6.0-marshmallow android-external-storage


    【解决方案1】:

    简短的回答是,它取决于您正在编写的外部存储的何处以及您如何获取路径。每个应用程序都可以访问自己的“私有”外部存储和外部缓存区域,无需特殊权限。您分别通过Context.getExternalFilesDir() 和Context.getExternalCacheDir() 访问路径。

    另一方面,如果您想在外部存储中的任何其他位置写入,例如通过调用Environment.getExternalStorageDirectory() 获得的路径,那么您需要持有WRITE_EXTERNAL_STORAGE 权限。即便如此,当您开始使用辅助外部存储时,也会有一些时髦的限制。

    【讨论】:

    • Context.getExternalFilesDir(); 的区别在哪里和 Environment.getExternalStorageDirectory(); ?
    • 这正是这个答案刚刚解释的内容。
    猜你喜欢
    • 2016-06-30
    • 1970-01-01
    • 1970-01-01
    • 2017-07-26
    • 2017-03-31
    • 1970-01-01
    • 1970-01-01
    • 2023-02-19
    • 2023-03-08
    相关资源
    最近更新 更多