Thursday, 27 October 2016

Load PDF files



Using this code

activity_main.xml

<RelativeLayout 
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="#9EBA89" >

<ListView
 android:id="@+id/listView1"
 android:layout_width="match_parent"
 android:layout_height="wrap_content" >
</ListView>

</RelativeLayout>



MainActivity.java
package com.example.readpdfnames;

import java.io.File;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {

File path;
ListView list;
static ArrayList<String> pdf_paths=new ArrayList<String>();
static ArrayList<String> pdf_names=new ArrayList<String>();

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

list=(ListView)findViewById(R.id.listView1);
pdf_paths.clear();
pdf_names.clear();

//Access External storage 
path = new File(Environment.getExternalStorageDirectory() + "");
searchFolderRecursive1(path);

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1pdf_names);
Log.e("aaaaaaaaaa"""+pdf_names);

list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
String path = pdf_paths.get(arg2);
File file = new File(path);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
}
});
}
private static void searchFolderRecursive1(File folder)
{
 if (folder != null)
 {
  if (folder.listFiles() != null)
  {
   for (File file : folder.listFiles())
   {
    if (file.isFile())
    {
     //.pdf files 
     if(file.getName().contains(".pdf"))
      {
       Log.e("ooooooooooooo""path__="+file.getName());
       file.getPath();
       pdf_names.add(file.getName());
       pdf_paths.add(file.getPath());
       Log.e("pdf_paths"""+pdf_names);
      }
     } 
     else 
     {
      searchFolderRecursive1(file);
     }
    }
   }
  }
 }
}


No comments:

Post a Comment