'在com.示例.pdfreader.主活动.查找Pdf(主活动. java:63)在com.示例.pdfreader.主活动.查找Pdf(主活动. java:67)在com.示例.pdfreader.主活动.查找Pdf(主活动. java:67)在com.示例.pdfreader.主活动.显示Pdf(主活动. java:84)在com.示例. pdfreader.主活动$1.已授予权限(主活动. java:44)
我正在特灵构建PdfReader应用程序,并在ArrayList findpdf()中;方法,我得到了一些异常,它会导致应用程序中的bug,所以它的消失被crahshed '
package com.example.pdfreader;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.widget.Toast;
import com.karumi.dexter.Dexter;
import com.karumi.dexter.PermissionToken;
import com.karumi.dexter.listener.PermissionDeniedResponse;
import com.karumi.dexter.listener.PermissionGrantedResponse;
import com.karumi.dexter.listener.PermissionRequest;
import com.karumi.dexter.listener.single.PermissionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements OnPdfSelectListner{
private MainAdapter adapter;
private List<File> pdfList;
private RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
runtimePermission();
}
private void runtimePermission(){
Dexter.withContext(MainActivity.this).withPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
.withListener(new PermissionListener() {
@Override
public void onPermissionGranted(PermissionGrantedResponse permissionGrantedResponse) {
displayPdf();
}
@Override
public void onPermissionDenied(PermissionDeniedResponse permissionDeniedResponse) {
Toast.makeText(MainActivity.this, "Permission Requried", Toast.LENGTH_SHORT).show();
}
@Override
public void onPermissionRationaleShouldBeShown(PermissionRequest permissionRequest, PermissionToken permissionToken) {
permissionToken.continuePermissionRequest();
}
}).check();
}
public ArrayList<File> findPdf (File file)
{
ArrayList<File> arrayList = new ArrayList<> ();
File[] files = file.listFiles();
for (File singleFile :files)
{
if (singleFile.isDirectory () && !singleFile.isHidden())
{
arrayList.addAll(findPdf(singleFile));
}
else{
if(singleFile.getName().endsWith(".pdf")) {
arrayList.add(singleFile);
}
}
}
return arrayList;
}
public void displayPdf()
{
recyclerView = findViewById(R.id.rv);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
pdfList = new ArrayList<> ();
pdfList.addAll(findPdf (Environment.getExternalStorageDirectory()));
adapter = new MainAdapter(this, pdfList,this);
recyclerView.setAdapter(adapter);
}
@Override
public void onPdfSelected(File file) {
startActivity(new Intent(MainActivity.this,PdfActivity.class)
.putExtra("paht",file.getAbsolutePath()));
}
}
Here, is the all code of the MainActivity.java and although on calling the displayPdf(); method in runtimePermission(); is ok or shall we need to modify this functions.
3条答案
按热度按时间neskvpey1#
t40tm48m2#
下面是获取外部目录中所有pdf文件的方法。根据您的使用情况进行定制。
9njqaruj3#