2013-07-04 1 views
0

저는 안드로이드에서 새로운 것입니다. 내 목록에서 선택했을 때 내 PDF 파일을 읽지 않는 코드가 무엇이 있는지 알고 싶습니다. 이 파일 을 읽을 수 없습니다하지만이 같은 하드 코드 된 경로를 사용할 때제 3 자 응용 프로그램에서 내 pdf 파일을 읽지 않습니다

public class ChoosefileActivity extends ListActivity { 

private List<String> item = null; 
private List<String> path = null; 
private String root; 
private TextView myPath; 
public final static String loc = "com.example.mypdfviewer.loc"; 
    private String dirPath; 

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

    myPath = (TextView)findViewById(R.id.path); 
     root = Environment.getExternalStorageDirectory().getPath(); 
     getDir(root); 
    } 

private void getDir(String dirPath) 
    { 
    myPath.setText(dirPath); 
    item = new ArrayList<String>(); 
    path = new ArrayList<String>(); 
    File f = new File(dirPath); 
    File[] files = f.listFiles(); 

    if(!dirPath.equals(root)) 
    { 
     item.add(root); 
     path.add(root); 
     item.add("../"); 
     path.add(f.getParent());  
    } 
    for(int i=0; i < files.length; i++) 
    { 
     File file = files[i]; 

     if(!file.isHidden() && file.canRead()){ 
     path.add(file.getPath()); 
     if(file.isDirectory()){ 
     item.add(file.getName() + "/"); 
    } 
    else 
    { 
     item.add(file.getName()); 
    } 
    } 
    } 
    ArrayAdapter<String> fileList = new ArrayAdapter<String>(this, R.layout.row, item); 
    setListAdapter(fileList); 
    } 

protected void onListItemClick(ListView l, View v, int position, long id) 
    { 
    File file = new File(path.get(position)); 

    if (file.isDirectory()) 
     { 
     if(file.canRead()){ 
      getDir(path.get(position)); 
     } 
     else 
     { 
      new AlertDialog.Builder(this) 
      .setIcon(R.drawable.ic_launcher) 
      .setTitle("[" + file.getName() + "] folder can't be read!") 
      .setPositiveButton("OK", null).show(); 
     } 
     } 
     else 
     { 
      new AlertDialog.Builder(this) 
      .setIcon(R.drawable.ic_launcher) 
      .setTitle("Open: " + "[" + file.getName() + "]") 
      .setPositiveButton("OK", new DialogInterface.OnClickListener() 
      { 
       public void onClick(DialogInterface dialog, int v) 
       { 

       File file = new File(**dirPath**); 
           Uri path = Uri.fromFile(file); 
       Intent intent = new Intent(Intent.ACTION_VIEW); 
       intent.setDataAndType(path, "application/pdf"); 
       intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
       startActivity(intent); 
       } 
       }) 
       .setNegativeButton("Cancel", new DialogInterface.OnClickListener() 
       { 
       public void onClick(DialogInterface dialog, int which) 
       { 
       dialog.cancel(); 
       } 
       }).show(); 
       } 
       } 
      } 

그것을 보여주고 오류 대화 상자 :

File file = new File("**/mnt/sdcard/pdf_File/another.pdf**"); 
       Uri path = Uri.fromFile(file); 
       Intent intent = new Intent(Intent.ACTION_VIEW); 
       intent.setDataAndType(path, "application/pdf"); 
       intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
       startActivity(intent); 

이 코드가 어떤에 표시 할 수 여기 내 코드입니다 pdf를 읽을 수있는 응용 프로그램입니다. 난 그냥 파일을 theres 문제가 있는지 확인하기 위해 그냥 사용하지만, 나는 그것이 내 코드에 있다고 생각 알아 냈어. 내가 필요로하는 것은 하드 코딩 된 경로가 아닙니다. 친절하게 도와주세요.

+0

왜 당신이 .pdf 파일에 액세스하기 바로 전에, 당신이 연결된 한 경로를 인쇄되지 않습니다. 기록하여 올바른지 확인하십시오. – g00dy

답변

0

에 을 어떻게 사용하고 계십니까?

하드 코드 된 경로가 작동하지만 다른 경로가 아닌 경우 경로가 잘못되었을 가능성이 있습니다. Log.d를 넣고 무엇이 켜져 있는지 확인하십시오. **dirPath**

getPath 대신 getAbsolutePath를 사용해 보셨습니까?

어쩌면 당신을 도울 수,이 스레드를 참조하십시오 - What's the difference between getPath(), getAbsolutePath(), and getCanonicalPath() in Java?

관련 문제