小郭同学忒骚了 2022-10-11 19:05 采纳率: 0%
浏览 12

关于#android#的存储卡位置(divice file explorer)问题,如何解决?


package com.example.chapter06;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.chapter06.util.DateUtil;
import com.example.chapter06.util.FileUtil;
import com.example.chapter06.util.ToastUtil;

import java.io.File;

@SuppressLint("SetTextI18n")
public class FileWriteActivity extends AppCompatActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {
    private EditText et_name;
    private EditText et_age;
    private EditText et_height;
    private EditText et_weight;
    private boolean bMarried = false;
    private String[] typeArray = {"未婚", "已婚"};
    private String mPath; // 私有目录路径
    String file_path;
    private TextView tv_path;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_file_write);
        et_name = findViewById(R.id.et_name);
        et_age = findViewById(R.id.et_age);
        et_height = findViewById(R.id.et_height);
        et_weight = findViewById(R.id.et_weight);
        tv_path = findViewById(R.id.tv_path);
        CheckBox ck_married = findViewById(R.id.ck_married);
        ck_married.setOnCheckedChangeListener(this);
        findViewById(R.id.btn_save).setOnClickListener(this);
        findViewById(R.id.btn_read).setOnClickListener(this);
        // 获取当前App的私有下载目录
       // mPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString()+"/";
        mPath = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).toString()+"/";
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        bMarried = isChecked;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_save) {
            String name = et_name.getText().toString();
            String age = et_age.getText().toString();
            String height = et_height.getText().toString();
            String weight = et_weight.getText().toString();
            if (TextUtils.isEmpty(name)) {
                ToastUtil.show(this, "请先填写姓名");
                return;
            } else if (TextUtils.isEmpty(age)) {
                ToastUtil.show(this, "请先填写年龄");
                return;
            } else if (TextUtils.isEmpty(height)) {
                ToastUtil.show(this, "请先填写身高");
                return;
            } else if (TextUtils.isEmpty(weight)) {
                ToastUtil.show(this, "请先填写体重");
                return;
            }
            String content = String.format(" 姓名:%s\n 年龄:%s\n 身高:%scm\n 体重:%skg\n 婚否:%s\n 注册时间:%s\n",
                    name, age, height, weight, typeArray[bMarried?1:0], DateUtil.getNowDateTime("yyyy-MM-dd HH:mm:ss"));
            file_path = mPath + DateUtil.getNowDateTime("") + ".txt";
            FileUtil.saveText(file_path, content); // 把字符串内容保存为文本文件
            tv_path.setText("用户注册信息文件的保存路径为:\n" + file_path);
            ToastUtil.show(this, "数据已写入存储卡文件");
        }

        if(v.getId()==R.id.btn_read){
            tv_path.setText(FileUtil.openText(file_path));
        }
    }

}

img

img

文件正常都可以读取,但是存储卡所在位置上读不出来

img

  • 写回答

2条回答 默认 最新

  • fangcunlingtai-lily 2022-10-12 05:27
    关注

    设备没有root,所以是看不到的。可以用安卓模拟器,或者root过的手机

    评论

报告相同问题?

问题事件

  • 创建了问题 10月11日

悬赏问题

  • ¥45 工程软件单片机设计课题要求
  • ¥15 在centos7安装conda
  • ¥15 c#调用yolo3 dll文件获取的数据对不上
  • ¥20 WPF 如何实现多语言,label 和cs(live Charts)中是否都能翻译
  • ¥15 STM32F103上电短路问题
  • ¥15 关于#单片机#的问题:以ATMEGA128或相近型号单片机为控制器设计直流电机调速的闭环控制系统(相关搜索:设计报告|软件设计|流程图)
  • ¥15 打开软件提示错误:failed to get wglChoosePixelFormatARB
  • ¥15 (标签-python|关键词-char)
  • ¥15 python+selenium,在新增时弹出了一个输入框
  • ¥15 苹果验机结果的api接口哪里有??单次调用1毛钱及以下。