Android SDCard中写入文件

   日期:2020-11-10     浏览:88    评论:0    
核心提示:一、介绍 这是一个很简单的Demo,只是点击按钮后往SDCard中写入一个文本。二、案例实现1.搭建UI(代码如下)<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http:...

一、介绍

         这是一个很简单的Demo,只是点击按钮后往SDCard中写入一个文本。

 

二、案例实现

1.搭建UI(代码如下)

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="测试写入SDcord"
        android:onClick="OnClickButton"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

2.书写权限工具类(代码如下)

package com.hiscene.testsd;

import android.content.pm.PackageManager;
import android.os.Build;

public class PermisionUtils {

    //检查用户权限
    public static boolean CheckPermissions(MainActivity activity, int code, String[] permissons) {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

            boolean needPermission = false;

            for (String permisson : permissons) {
                if (activity.checkSelfPermission(permisson) != PackageManager.PERMISSION_GRANTED) {
                    needPermission = true;
                }
            }
            //用户没有权限
            if (needPermission) {
                activity.requestPermissions(permissons, code);
                return false;
            } else {
              return true;
            }
        }

        else { return true; }
    }
}

3.书写业务逻辑(代码如下)

package com.hiscene.testsd;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import static android.content.pm.PackageManager.PERMISSION_GRANTED;

public class MainActivity extends AppCompatActivity {

    //授权请求码
    private static final int PERMISSIONS_CODE = 656;
    private MainActivity activity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        activity=MainActivity.this;
    }

    public void OnClickButton(View view) {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            Toast.makeText(activity, "SDCard就绪", Toast.LENGTH_LONG).show();

            //动态申请权限
            String[] permissons={Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE};
            if (PermisionUtils.CheckPermissions(activity,PERMISSIONS_CODE,permissons))  Write();
        }
    }

    private void Write()
    {
        File ext = Environment.getExternalStorageDirectory();//SD卡目录
        File f2 = new File(ext, "user.info");
        try {
            OutputStream out = new FileOutputStream(f2);
            out.write("helloworld".getBytes());
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //授权回调
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == PERMISSIONS_CODE) {
            for (int i = 0; i < permissions.length; i++) {
                String s = permissions[i];
                if (grantResults[i] != PERMISSION_GRANTED) {
                    //用户拒绝授权
                    Toast.makeText(activity, "写入Sdcord失败,需要读取和写入权限!", Toast.LENGTH_LONG).show();
                } else {
                    Write();
                }
            }
        }
    }
    

}

 

 
打赏
 本文转载自:网络 
所有权利归属于原作者,如文章来源标示错误或侵犯了您的权利请联系微信13520258486
更多>最近资讯中心
更多>最新资讯中心
0相关评论

推荐图文
推荐资讯中心
点击排行
最新信息
新手指南
采购商服务
供应商服务
交易安全
关注我们
手机网站:
新浪微博:
微信关注:

13520258486

周一至周五 9:00-18:00
(其他时间联系在线客服)

24小时在线客服