0

I have pdf reader with list view and cant open all pdf because diffrent password.

source code: https://deepshikhapuri.wordpress.com/2017/04/24/open-pdf-file-from-sdcard-in-android-programmatically/

I want pdf that can not open when on click pop up error messages. can you help me?

Main Activity.java

ListView lv_pdf;
public static ArrayList<File> fileList = new ArrayList<File>();
PDFAdapter obj_adapter;
public static int REQUEST_PERMISSIONS = 1;
boolean boolean_permission;
File dir;

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


}

private void init() {
    lv_pdf = (ListView) findViewById(R.id.lv_pdf);
    dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),"Pdf");
    fn_permission();
    lv_pdf.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int i, long l) {

            Intent intent = new Intent(getApplicationContext(), PdfActivity.class);
            intent.putExtra("position", i);
            startActivity(intent);
            Log.e("Position", i + "Pdf");
        }
    });
}

public static ArrayList<File> getfile(File dir) {
    File listFile[] = dir.listFiles();
    if (listFile != null && listFile.length > 0) {
        for (int i = 0; i < listFile.length; i++) {
            if (listFile[i].isDirectory()) {
                getfile(listFile[i]);
            } else {
                boolean booleanpdf = false;
                if (listFile[i].getName().endsWith(".pdf")) {
                    for (int j = 0; j < fileList.size(); j++) {
                        if (fileList.get(j).getName().equals(listFile[i].getName())) {
                            booleanpdf = true;
                        } else {

                        }
                    }
                    if (booleanpdf) {
                        booleanpdf = false;
                    } else {
                        fileList.add(listFile[i]);
                    }
                }

            }
        }

    }
    return fileList;
}

private void fn_permission() {
    if ((ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)) {

        if ((ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, android.Manifest.permission.READ_EXTERNAL_STORAGE))) {


        } else {
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE},
                    REQUEST_PERMISSIONS);

        }
    } else {
        boolean_permission = true;

        getfile(dir);

        obj_adapter = new PDFAdapter(getApplicationContext(), fileList);
        lv_pdf.setAdapter(obj_adapter);

    }
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == REQUEST_PERMISSIONS) {

        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

            boolean_permission = true;
            getfile(dir);

            obj_adapter = new PDFAdapter(getApplicationContext(), fileList);
            lv_pdf.setAdapter(obj_adapter);

        } else {

            Toast.makeText(getApplicationContext(), "Please allow the permission", Toast.LENGTH_LONG).show();

        }
    }
}

PdfActivity

 public static final String SAMPLE_FILE = "android_tutorial.pdf";
PDFView pdfView;
Integer pageNumber = 0;
String pdfFileName;
String TAG = "PdfActivity";
int position = -1;

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

private void init() {
    pdfView = (PDFView) findViewById(R.id.pdfView);

    //position = Environment.getExternalStorageDirectory()+"/sdcard/Pdf"
    position = getIntent().getIntExtra("position", -1);
    displayFromSdcard();
    //String position = Environment.getExternalStorageDirectory().getAbsolutePath() +"/Pdf/";
}

private void displayFromSdcard() {
    pdfFileName = MainActivity.fileList.get(position).getName();

    pdfView.fromFile(MainActivity.fileList.get(position))
            .defaultPage(pageNumber)
            .enableSwipe(true)
            .password("123456")
            .swipeHorizontal(false)
            .onPageChange(this)
            .enableAnnotationRendering(true)
            .onLoad(this)
            .scrollHandle(new DefaultScrollHandle(this))
            .load();
}


@Override
public void onPageChanged(int page, int pageCount) {
    pageNumber = page;
    setTitle(String.format("%s %s / %s", pdfFileName, page + 1, pageCount));
}


@Override
public void loadComplete(int nbPages) {
    PdfDocument.Meta meta = pdfView.getDocumentMeta();
    printBookmarksTree(pdfView.getTableOfContents(), "-");

}

public void printBookmarksTree(List<PdfDocument.Bookmark> tree, String sep) {
    for (PdfDocument.Bookmark b : tree) {

        Log.e(TAG, String.format("%s %s, p %d", sep, b.getTitle(), b.getPageIdx()));

        if (b.hasChildren()) {
            printBookmarksTree(b.getChildren(), sep + "-");
        }
    }
}

PdfAdapter

 Context context;
ViewHolder viewHolder;
ArrayList<File> al_pdf;

public PDFAdapter(Context context, ArrayList<File> al_pdf) {
    super(context, R.layout.adapter_pdf, al_pdf);
    this.context = context;
    this.al_pdf = al_pdf;

}


@Override
public int getItemViewType(int position) {
    return position;
}

@Override
public int getViewTypeCount() {
    if (al_pdf.size() > 0) {
        return al_pdf.size();
    } else {
        return 1;
    }
}

@Override
public View getView(final int position, View view, ViewGroup parent) {


    if (view == null) {
        view = LayoutInflater.from(getContext()).inflate(R.layout.adapter_pdf, parent, false);
        viewHolder = new ViewHolder();
        viewHolder.tv_filename = (TextView) view.findViewById(R.id.tv_name);

        view.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) view.getTag();

    }

    viewHolder.tv_filename.setText(al_pdf.get(position).getName());
    return view;

}

public class ViewHolder {

    TextView tv_filename;


}
Lord_Curdin
  • 957
  • 1
  • 14
  • 27
Mind
  • 1
  • 5

1 Answers1

0
lv_pdf.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int i, long l) {

      if(isPaswordProcted()){
       Toast.makeText(this, password procted, Toast.LENGTH_SHORT).show();
       }else{
        Intent intent = new Intent(getApplicationContext(), PdfActivity.class);
        intent.putExtra("position", i);
        startActivity(intent);

      }
    }
});
Nikhil Vadoliya
  • 1,542
  • 10
  • 17
  • Which type of error come? Comments your log of error. – Nikhil Vadoliya Apr 28 '18 at 06:31
  • sorry i just learned basic android. what do you mean is this? i try red in PasswordProtected – Mind Apr 28 '18 at 08:40
  • if(isPasswordProcted()){ Toast.makeText(this, PasswordProtection, Toast.LENGTH_SHORT).show(); }else{ Intent intent = new Intent(getApplicationContext(), PdfActivity.class); intent.putExtra("position", i); startActivity(intent); – Mind Apr 28 '18 at 08:41
  • and (isPasswordProcted()) is also red – Mind Apr 28 '18 at 08:51
  • Do you get any method who check that pdf is password protected or not in your used library? – Nikhil Vadoliya Apr 28 '18 at 09:36
  • If it's password protected than open Dialog and user enter password of pdf. – Nikhil Vadoliya Apr 28 '18 at 09:40
  • This password pass in second activity which contain pdfview . This password set in . password (user_entered_password) method of library. – Nikhil Vadoliya Apr 28 '18 at 09:42
  • sorry password pdf already exist in PdfActivity.java in pdf library and i mean when i click pdf which different password appear message "this file can not be opened" – Mind Apr 28 '18 at 10:00
  • i mean like this [link](http://www.apnatutorials.com/android/android-toast.php?categoryId=2&subCategoryId=78&myPath=android/android-toast.php) when i clicked pdf with diffrent password pop up warning toast. "this file can not be opened" – Mind Apr 28 '18 at 10:01
  • help me broooo :D – Mind Apr 28 '18 at 10:03
  • It's means you want not come toast message but they not open pdf . – Nikhil Vadoliya May 02 '18 at 04:46