首页 新闻 会员 周边

AS读取华为手机内置SD卡文件时,找不到文件问题

0
[待解决问题]
    private Button btndownloadFile,btnstopLoad;//按钮
    private TextView tvShow;//显示加载信息
    private TextView edtContent;//显示加载文件内容
    //private MyAsyncTask myAsyncTask;
    private downloadAsyncTaska download;//AsyncTask子类
    private ProgressBar proBar;//进度条
    private File file = new File(Environment.getExternalStorageDirectory(),"kgmusic//1.txt");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_async_task);
        btndownloadFile = findViewById(R.id.btn_loadFile);
        btnstopLoad = findViewById(R.id.btn_stopload);
        tvShow = findViewById(R.id.tv_show);
        edtContent = findViewById(R.id.edt_content);
        proBar = findViewById(R.id.progress_bar);
        btndownloadFile.setOnClickListener(AsyncTaskActivity.this);
        btnstopLoad.setOnClickListener(AsyncTaskActivity.this);
        download = new downloadAsyncTaska();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.btn_loadFile:
                //动态申请权限
                if(ContextCompat.checkSelfPermission(AsyncTaskActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED){
                    ActivityCompat.requestPermissions(AsyncTaskActivity.this,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},1);
                }else {
                    Toast.makeText(this, "无须动态申请", Toast.LENGTH_SHORT).show();
                    download.execute(file);
                }
                break;
            case R.id.btn_stopload:
                download.onCancelled();
                break;
            default:break;
        }
    }

    private class downloadAsyncTaska extends AsyncTask<File, Integer, String> {
        @Override
        protected String doInBackground(File... strings) {//运行在子线程 必须实现
            //读文件
            return read(strings[0]);
            //publishProgress(1);//传到主线程
        }

        @Override
        protected void onPreExecute() {//运行在主线程 需要自己写:异步运行
            tvShow.setText("加载中");
            super.onPreExecute();
        }

        @Override
        protected void onProgressUpdate(Integer... values) {//运行主线程 更新UI 可以自动调用
            int per = values[0]; // 取出传出的值
            proBar.setProgress(per);// 显示任务进度
            if (per <= 100)
                tvShow.setText("loading" + per + "%");
            tvShow.setText("loading..." + per + "%");// 显示任务进度
            super.onProgressUpdate(values);
        }

        @Override
        protected void onPostExecute(String s) {//运行主线程 执行完成结果 可以自动调用
            edtContent.setText(s);// 显示读取文件内容
            tvShow.setText("加载完成");
            super.onPostExecute(s);
        }

        @Override
        protected void onCancelled() {//异步执行:需要自己写
            tvShow.setText("取消加载!");
            proBar.setProgress(0);
            super.onCancelled();
        }

        //读取文件及计算进度值
        private String read(File file) {
            StringBuilder strb = new StringBuilder();//字符串处理类
            BufferedReader br = null;//字符缓冲流
            //获取文件长度
            long fileLength = file.length();//文件原长度
            long readLength = 0;
            try {
                br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));//读取文件:字节流->字符流->字符缓冲流
                String line = br.readLine();//读出一行数据
                while (line != null) {
                    strb.append(line + "\n");// 存入字符串处理类对象中
                    readLength += line.getBytes().length;//获取当前已经读取的文件长度
                    int per = (int) (readLength * 100.0 / fileLength);//计算百分比
                    publishProgress(per);//传出需要更新值,之后执行onProgressUpdate()
                    Thread.sleep(50);//模拟缓慢加载速度
                    line = br.readLine();//读缓冲流的下一行数据
                }
                return strb.toString();//返回读取的文件数据
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (br != null) {
                        br.close();//关闭缓冲流对象  顶层流关闭,低层流自然关闭
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return null; //如果读取文件不成功,返回null
        }
    }



华为手机上的路径没有错误W/System.err: java.io.FileNotFoundException: /storage/emulated/0/kgmusic/1.txt (No such file or directory)
找不到原因,求解!!!

這是記憶中的一本書!的主页 這是記憶中的一本書! | 菜鸟二级 | 园豆:222
提问于:2020-04-10 12:47

读写外部存储的权限给了吗?

。淑女范erり 4年前

@。淑女范erり: 给了,在app选里面storage中勾选了

《这是记忆中的一本书!》 4年前
< >
分享
所有回答(1)
0

你这是没找到文件,给个根路径试试

西红柿里没有番茄 | 园豆:645 (小虾三级) | 2020-04-10 19:27
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册