package com.example.myfile;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.Manifest;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.chad.library.adapter.base.BaseQuickAdapter;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.EasyPermissions;
public class MainActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks {
private static final int REQUEST_CODE_QRCODE_PERMISSIONS = 1;
private FileAdapter adapter;
private List<FileInfo> list=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
adapter=new FileAdapter(R.layout.item,list);
recyclerView.setAdapter(adapter);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("wangwu", "onClick: ");
// Toast.makeText(MainActivity.this, "点击了", Toast.LENGTH_SHORT).show();
initData();
}
});
adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
@Override
public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
Toast.makeText(MainActivity.this, list.get(position).getFilePath(), Toast.LENGTH_SHORT).show();
}
});
}
private void initData() {
// 获取文件
// String path=Environment.getExternalStorageDirectory().getAbsolutePath()+"/Android/data/com.tencent.mobileqq/Tencent/QQfile_recv/";
File externalFilesDir = this.getExternalFilesDir(null);
String path1=externalFilesDir.getAbsolutePath();
String[] split = path1.split("/");
String path2=split[0]+"/"+split[1]+"/"+split[2]+"/0";
String path=path2+"/Android/data/com.tencent.mobileqq/Tencent/QQfile_recv/";
// Toast.makeText(this, ""+path, Toast.LENGTH_SHORT).show();
JSONArray allFiles = getAllFiles(path, ".pdf");
// Log.d("wangwu", "initData: "+allFiles);
}
/**
* 获取指定目录内所有文件路径
* @param dirPath 需要查询的文件目录
* @param _type 查询类型,比如mp3什么的
*/
public JSONArray getAllFiles(String dirPath, String _type) {
Log.d("wangwu", "getAllFiles: ");
File f = new File(dirPath+"/");
if (!f.exists()) {//判断路径是否存在
Log.d("wangwu", "判断路径是否存在 ");
return null;
}
File[] files = f.listFiles();
if(files==null){//判断权限
Log.d("wangwu", "判断权限 ");
return null;
}
JSONArray fileList = new JSONArray();
for (File _file : files) {//遍历目录
if(_file.isFile() && _file.getName().endsWith(_type)){
String _name=_file.getName();
FileInfo fileInfo = new FileInfo();
String filePath = _file.getAbsolutePath();//获取文件路径
String fileName = _file.getName().substring(0,_name.length()-4);//获取文件名
fileInfo.setFileName(fileName);
fileInfo.setFilePath(filePath);
list.add(fileInfo);
adapter.notifyDataSetChanged();
Log.d("wangwu","fileName:"+fileName);
Log.d("wangwu","filePath:"+filePath);
try {
JSONObject _fInfo = new JSONObject();
_fInfo.put("name", fileName);
_fInfo.put("path", filePath);
fileList.put(_fInfo);
}catch (Exception e){
}
} else if(_file.isDirectory()){//查询子目录
getAllFiles(_file.getAbsolutePath(), _type);
} else{
}
}
return fileList;
}
@Override
protected void onStart() {
super.onStart();
requestCodeQRCodePermissions();
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}
@Override
public void onPermissionsGranted(int requestCode, List<String> perms) {
}
@Override
public void onPermissionsDenied(int requestCode, List<String> perms) {
Toast.makeText(this, "权限不足,将影响部分功能的使用!", Toast.LENGTH_SHORT).show();
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@AfterPermissionGranted(REQUEST_CODE_QRCODE_PERMISSIONS)
// , Manifest.permission.READ_PHONE_STATE
private void requestCodeQRCodePermissions() {
String[] perms = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
if (!EasyPermissions.hasPermissions(this, perms)) {
EasyPermissions.requestPermissions(this, "请前往设置打开相关权限!", REQUEST_CODE_QRCODE_PERMISSIONS, perms);
}
}
}