从共享文件夹读取PDF文件

By simon at 2018-02-28 • 0人收藏 • 30人看过

我正在使用此代码从共享文件夹读取pdf文件,但我不能 读取文件

package com.example.kuldeep.myapplication;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.playlog.internal.LogEvent;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfTextExtractor;
import org.apache.commons.io.comparator.LastModifiedFileComparator;
import org.apache.commons.io.filefilter.WildcardFileFilter;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.net.MalformedURLException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbException;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileFilter;
import jcifs.smb.SmbFileInputStream;
import jcifs.smb.SmbFileOutputStream;
import static com.example.kuldeep.myapplication.R.id.textView1;
import static com.example.kuldeep.myapplication.R.layout.activity_main14;
import static org.apache.commons.io.comparator.LastModifiedFileComparator.*;
public class Main14Activity extends AppCompatActivity {

 private static final String TAG = "Main14Activity";
 TextView textView, textiew1;
 String CustomerNameRegex = "Customer Name: [a-zA-Z\\s]*",
  MobileNumberRegex = "PH:\\s[0-9]*",
  JobCardNoRegex = "Jobcard No :\\sJC-[a-zA-Z0-9\\S]*\n[a-zA-Z0-9\\S]*",
  ServiceTypeRegex = "Service Type:([a-zA-Z0-9\\s\\S])*GSTIN",
  RegistrationNumberRegex = "Reg. No.:\\s[a-zA-Z0-9\\S]*";
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(activity_main14);
  /*listView=(ListView)findViewById(R.id.list);*/
  textView = (TextView) findViewById(R.id.text);
  textView = (TextView) findViewById(R.id.text1);
  jcifs.Config.setProperty("jcifs.netbios.wins", "192.168.1.12");
  Thread th = new Thread(new Runnable() {
   @Override
   public void run() {
    SmbFile choice = null;
    try {
     String yourPeerName = "kuldeep";
     String yourPeerPassword = "123";
     String yourPeerIP = "192.168.1.12";
     final String path = "smb://" + yourPeerIP + "/Scan/";
     NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null, yourPeerName, yourPeerPassword);
     Log.e("Connected", "Yes");
     SmbFile file = new SmbFile(path, auth);
     SmbFile[] mFile = file.listFiles();
     if (mFile == null || mFile.length == 0) {
      return;
     }
     SmbFile lastmode = mFile[0];
     for (int i = 1; i < mFile.length; i++) {
      if (lastmode.lastModified() < mFile[i].lastModified()) {
       lastmode = mFile[i];
      }
     }
     Log.v(TAG, "File" + lastmode);
     String s = String.valueOf(lastmode);
     Log.v(TAG, "File" + s);
     Log.v(TAG, "File" + lastmode.getInputStream().read());
     Log.v(TAG, "File" + lastmode.getName());
     final String dat = lastmode.getName().toString();
     runOnUiThread(new Runnable() {
      @Override
      public void run() {
       textView.setText(dat);
       try {
        String parsedText = "";
        PdfReader reader = new PdfReader(path + dat);
        Log.v(TAG, "File" + reader);
        int n = reader.getNumberOfPages();
        for (int i = 0; i < n; i++) {
         parsedText = parsedText + PdfTextExtractor.getTextFromPage(reader, i + 1).trim() + "\n"; //Extracting the content from the different pages
        }
        Log.v(TAG, "File" + parsedText);
        reader.close();
       } catch (Exception e) {
        e.printStackTrace();
       }
      }
     });
    } catch (MalformedURLException e) {
     e.printStackTrace();
    } catch (SmbException e) {
     e.printStackTrace();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
  });
  th.start();
 }

1 个回复 | 最后更新于 2018-02-28
2018-02-28   #1

你声明相同的textview

       textView=(TextView)findViewById(R.id.text);
       textiew1=(TextView)findViewById(R.id.text1);

登录后方可回帖

Loading...