2011-12-28 2 views
0

의 "sdcard 파일을 여는 방법"을 클릭하고 파일을 클릭하면 에뮬레이터에서 열어야합니까?"** sdcard 파일을 android **"에서 여는 방법과 파일을 클릭하면 에뮬레이터에서 열어야합니까?

+0

첫 번째 Google에 귀하의 검색어. 거기에서 아무 것도 없으면, 그 후에 질문으로 여기에서 물으십시오. – Praveenkumar

+1

여기 소스 코드를 묻지 마라 !!! – Rajasekar

+0

여기에 질문을 게시하기 전에이 사이트를 방문하십시오. http://stackoverflow.com/faq –

답변

5
public class Testopen extends Activity { 
    private List<String> list; 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     File file = new File(Environment.getExternalStorageDirectory() 
      + File.separator 
      + "mymusic" //folder name 
     ); 
     if (!file.exists()) { 
      file.mkdirs(); 
     } 
     ListView lv = (ListView) findViewById(R.id.listview1); 
     list = getSD(); 
     lv.setAdapter((ListAdapter) new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list)); 
     Log.d("LOG", "FIRE !!!! "); 

     lv.setOnItemClickListener(new OnItemClickListener() { 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       // When clicked, show a toast with the TextView text 
       Toast.makeText(Testopen.this,"clicked an item", Toast.LENGTH_LONG).show(); 

       Intent intent = new Intent(Testopen.this, OpenFileActivity.class); 
       startActivity(intent); 
      } 
     }); 
    } 

    //read from sdcard 
    private List<String> getSD() { 
     List<String> item = new ArrayList<String>(); 
     File f = new File("/sdcard/download"); 
     File[] files = f.listFiles(); 

     for(int i = 0; i < files.length; i++) { 
      File file = files[i]; 
      //take the file name only 
      long size = file.length()/1024; 

      String myfile = file.getPath().substring(file.getPath().lastIndexOf("/")+1,file.getPath().length()).toLowerCase(); 
      //item.add(myfile); 
      item.add(myfile+"    "+"Size:"+size+" KB"); 

      // Log.d("LOG", "fs "+file.length()); 
      //item.add(file.length()); 
     } 
     return item; 
    } 
} 
0

체크 아웃 코드 -

Extends ListActivity.

ArrayAdapter<String> adapter; 
    int clickCounter=0; 
    ArrayList<String> listItems=new ArrayList<String>(); 
    private File[] imagelist; 
    String[] pdflist; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 
      File images = Environment.getExternalStorageDirectory(); 
      imagelist = images.listFiles(new FilenameFilter(){ 
        public boolean accept(File dir, String name) 
        { 
          return ((name.endsWith(".pdf"))); 
        } 
      }); 
      pdflist = new String[imagelist.length]; 
      for(int i = 0;i<imagelist.length;i++) 
      { 
        pdflist[i] = imagelist[i].getName(); 
      } 
      this.setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, pdflist)); 
    } 
    @SuppressWarnings("rawtypes") 
    @Override 
    protected void onListItemClick(ListView l, View v, int position, long id) { 
      super.onListItemClick(l, v, position, id); 
      PackageManager packageManager = getPackageManager(); 
      Intent testIntent = new Intent(Intent.ACTION_VIEW); 
      testIntent.setType("application/pdf"); 
      List list = packageManager.queryIntentActivities(testIntent, PackageManager.MATCH_DEFAULT_ONLY); 
      if (list.size() > 0 && imagelist[(int) id].isFile()) { 
       Intent intent = new Intent(); 
       intent.setAction(Intent.ACTION_VIEW); 
       Uri uri = Uri.fromFile(imagelist[(int) id].getAbsoluteFile()); 
       intent.setDataAndType(uri, "application/pdf"); 
       startActivity(intent); 
      } 
    } 

는 main.xml에 당신이 찾고있는 무엇

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <ListView android:id="@+android:id/list" android:layout_width="fill_parent" 
      android:layout_height="fill_parent" android:drawSelectorOnTop="false" /> 
</LinearLayout> 
+0

다음 오류가 발생했습니다. 12-28 11 : 42 : 43.345 : E/AndroidRuntime (344) : java.lang.RuntimeException : 활동을 시작할 수 없습니다. ComponentInfo {com.example/com.example.Pdf} : java.lang.RuntimeException : 귀하의 컨텐츠에는 id 속성이 'android.R.id.list'인 ListView가 있어야합니다. –

+0

업데이트 된 답변을 확인하십시오. – Praveenkumar

+0

작동 여부 – Praveenkumar

관련 문제