3

I'm listing PDF files in folder. But I want them to be named as filename, instead of sdcard/mypath/files Also, I want to open them whenever I click them via PDF viewer. My code:

public class activity1 extends ListActivity {

private List<String> fileList = new ArrayList<String>();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    File root = new File("sdcard/mypath");
    ListDir(root);
}

void ListDir(File f) {
    File[] files = f.listFiles();
    fileList.clear();
    for (File file : files) {
        fileList.add(file.getPath());
    }
    ArrayAdapter<String> directoryList = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, fileList);
    setListAdapter(directoryList);
}}

2 Answers2

2
   public class activity1 extends ListActivity {
   ListView lv;
   private List<String> fileList = new ArrayList<String>();

  @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    File root = new File("sdcard/mypath");
      lv = getListView();
        ListDir(root);
       }

     void ListDir(File f) {
      File[] files = f.listFiles();
   fileList.clear();
for (File file : files) {
    fileList.add(file.getName());
}
ArrayAdapter<String> directoryList = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, fileList);
setListAdapter(directoryList);
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            viewPdf(fileList.get(i));
        }
    });
}

 }}

and open the file in pdf default pdf viewer

     private void viewPdf(String file) {

        File pdfFile = new File(Environment.getExternalStorageDirectory() +    "/" + "mypath"+ "/" + file);
        Uri path = Uri.fromFile(pdfFile);

       // Setting the intent for pdf reader
    Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
    pdfIntent.setDataAndType(path, "application/pdf");
    pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);


        startActivity(pdfIntent);
   }

Use the above code.. Hope it helps!

Mr.Popular
  • 845
  • 1
  • 8
  • 15
0

Below code will help to open the file with as these type. check this answer:

 public static Intent getOpenFileIntent(Context ctx, String path) {

    Intent intent = new Intent(Intent.ACTION_VIEW);
    try {
        File file = new File(path);
        Uri uri = Uri.fromFile(file);
        String extension = MimeTypeMap.getFileExtensionFromUrl(path);
        if (extension != null) {
            String type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
            boolean status = canDisplay(ctx, type);
            if (type == null || !status) {
                type = "*/*";
            }
            if (!status) {
                Toast.makeText(ctx, "Cannot find appropriate Application", Toast.LENGTH_LONG).show();
            }
            intent.setDataAndType(uri, type);
        }
    } catch (Exception e) {
        AppLog.exception(e);
    }
    return intent;
}

and below code will check that the particular actionview app is present or not

   static boolean canDisplay(Context context, String mimeType) {
    try {
        PackageManager packageManager = context.getPackageManager();
        Intent testIntent = new Intent(Intent.ACTION_VIEW);
        testIntent.setType(mimeType);
        return packageManager.queryIntentActivities(testIntent, PackageManager.MATCH_DEFAULT_ONLY).size() > 0;
    } catch (Exception e) {
        AppLog.exception(e);
    }
    return false;
}

if the file type can openable, it will show the file with appropriate application or list the application can open the file.

UPDATE

Before send the file to this function make sure , that the particular file is present and readable.

Noorul
  • 3,386
  • 3
  • 32
  • 54