没有权限.将文件保存到adnroid Studio上的SDCard [英] Permission denied. Save FIle to SDCard on adnroid Studio

查看:105
本文介绍了没有权限.将文件保存到adnroid Studio上的SDCard的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图简单地将文件保存到设备.我在清单中添加了权限,应用程序确实要求我提供访问存储的权限,但是当我单击保存文件"时,我的权限失败"吐司弹出.也许我没有正确地请求许可.我在三星S10和仿真器上以及两个设备上都尝试了它不起作用.如果我尝试第二次在我的真实设备上使用它,该应用程序将崩溃.但是,这不会在模拟器上发生希望可以有人帮帮我.已经谢谢你了.

I am trying to simply save a File to my Device.I added the Permission in the Manifest and the app does ask me for Permission to access the storage but when i click "Save File" my "permission failed" Toast pops up. Maybe i dont ask for the permission correctly. I tried it on my Samsung S10 and on the emulator and on both devices it does not work. If i try to use it on my real device a second time, the app just crashes. However that does not happen on the emulator Hope someone can help me. Thanks already.

/////代码

package com.example.writeexeternalstorage;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.Permission;

public class MainActivity extends AppCompatActivity {

    EditText fileName;
    EditText text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Here, thisActivity is the current activity

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

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
        }

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 1);
        }
        setContentView(R.layout.activity_main);



        setContentView(R.layout.activity_main);
    }

    private boolean isExternalStorageWriteable(){
        if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
            return true;
        }else{
            return false;
        }
    }

    public void writeFile(View v){
        if(isExternalStorageWriteable() && checkPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            File textFile = new File(Environment.getExternalStorageDirectory(), fileName.toString());

            try {
                FileOutputStream fos = new FileOutputStream(textFile);
                fos.write(text.getText().toString().getBytes());
                fos.close();

                Toast.makeText(this, "File saved", Toast.LENGTH_SHORT).show();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }else if(isExternalStorageWriteable() == false)
        {
            Toast.makeText(this, "External Storage is not writable",Toast.LENGTH_SHORT).show();
        }

        else if(checkPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == false) {
            Toast.makeText(this, "Permission failed", Toast.LENGTH_SHORT).show();
        }

    }

    public boolean checkPermission(String permission){
        int check = ContextCompat.checkSelfPermission(this,permission);
        return (check == PackageManager.PERMISSION_GRANTED);
    }
}

推荐答案

如果您在大于29的API级别上使用Environment.getExternalStorageDirectory(),则它将不起作用,因为从API级别29开始,android将该功能弃用了.希望会回答您的问题.

if you are using Environment.getExternalStorageDirectory() on an API level greater than 29 then it would not work, because starting from API level 29 android made this function deprecated. Hope this would answer your question.

这篇关于没有权限.将文件保存到adnroid Studio上的SDCard的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆