2014-05-14 1 views
2

특정 CSV 파일을 데이터베이스로 가져 오려고합니다. 라이브러리 aFileChooser를 사용하여 파일을 선택하고 있지만 CSV 파일의 데이터는 가져 오지 않았습니다. 내가 어디서 잘못한거야? 감사합니다.Android 인 텐트에서 가져올 CSV 선택

@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    switch(requestCode) { 


    case ACTIVITY_CHOOSE_FILE1: { 
      if (resultCode == RESULT_OK){ 
       Uri uri = data.getData(); 
       File file1 = com.ipaulpro.afilechooser.utils.FileUtils.getFile(uri); 
       proImportCSV(file1); 
      } 
      } 
     } 
    } 



ricevi_csv= (Button) findViewById(R.id.but_ricevi_csv); 
    ricevi_csv.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 

     Intent chooseFile; 
      Intent intent; 
      chooseFile = new Intent(Intent.ACTION_GET_CONTENT); 
      chooseFile.setType("application/image"); 
      intent = Intent.createChooser(chooseFile, "Choose a CSV"); 
      startActivityForResult(intent, ACTIVITY_CHOOSE_FILE1); 
      } 
     }); 
    } 


private void proImportCSV(File from){ 

File root = Environment.getExternalStorageDirectory(); 
File exportDir = new File(root.getAbsolutePath()); 
File csvFile = new File(exportDir, getString(R.string.app_name)+".csv"); 

    try { 
ContentValues cv = new ContentValues(); 
// reading CSV and writing table 
CSVReader dataRead = new CSVReader(new FileReader(csvFile)); 
String[] vv = null; 
while((vv = dataRead.readNext())!=null) { 
    cv.clear(); 
    SimpleDateFormat currFormater = new SimpleDateFormat("dd-MM-yyyy"); 
    SimpleDateFormat postFormater = new SimpleDateFormat("yyyy-MM-dd"); 

     String eDDte; 
     try { 
      Date nDate = currFormater.parse(vv[0]); 
      eDDte = postFormater.format(nDate); 
      cv.put(Table.DATA,eDDte); 
     } 
     catch (Exception e) { 
     } 
cv.put(Table.C,vv[1]); 
cv.put(Table.E,vv[2]); 
cv.put(Table.U,vv[3]); 
cv.put(Table.C,vv[4]); 
SQLiteDatabase db = mHelper.getWritableDatabase(); 
    db.insert(Table.TABLE_NAME,null,cv); 

} dataRead.close();

} catch (예외 e) { Log.e ("TAG", e.toString());

}

+0

예외가 발생 했습니까? 내가 볼 수있는 유일한 이상한 점은 Intent.CATEGORY_OPENABLE 대신 "text/csv"및 Intent.ACTION_GET_CONTENT를 사용하는 것과는 대조적으로 "application/image"를 얻고 있다는 것입니다. "com.ipaulpro.afilechooser.utils.FileUtils.getFile (uri);"대신 "new File (data.getData(). getPath())"를 사용할 수도 있습니다. - 어제 CSV 중요한 물건을 만들었는데 제대로 작동하는 것 같았습니다. 귀하의 독서 내용은 괜찮아 보이지만 while 루프를 입력하기 전에 dataRead.readNext()를 호출하여 헤더 행을 건너 뛸 수 있습니다 (문자가있는 경우) – Guardanis

+0

코드를 보여줄 수 있습니까? 'Intent.CATEGORY_OPENABLE'이 (가) 나를 위해 일하지 않는다 – user2847219

+0

물론, 대답으로 게시하겠습니다. 그냥 github에서 그것을 끌어 당길 줘 – Guardanis

답변

6

열기 유형 "텍스트/CSV"와 CATEGORY_OPENABLE의 종류 사용 목적 : 당신의 onActivityResult를 지금의

private void selectCSVFile(){ 
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
    intent.addCategory(Intent.CATEGORY_OPENABLE); 
    intent.setType("text/csv"); 
    startActivityForResult(Intent.createChooser(intent, "Open CSV"), ACTIVITY_CHOOSE_FILE1); 
} 

:

case ACTIVITY_CHOOSE_FILE1: { 
     if (resultCode == RESULT_OK){ 
      proImportCSV(new File(data.getData().getPath()); 
     } 
     } 
    } 

그리고 지금 우리가 필요 proImportCSV 메소드를 변경하여 전달되는 실제 파일을 사용하도록 변경하십시오.

private void proImportCSV(File from){ 
    try { 
    // Delete everything above here since we're reading from the File we already have 
    ContentValues cv = new ContentValues(); 
    // reading CSV and writing table 
    CSVReader dataRead = new CSVReader(new FileReader(from)); // <--- This line is key, and why it was reading the wrong file 

    SQLiteDatabase db = mHelper.getWritableDatabase(); // LEt's just put this here since you'll probably be using it a lot more than once 
    String[] vv = null; 
    while((vv = dataRead.readNext())!=null) { 
     cv.clear(); 
     SimpleDateFormat currFormater = new SimpleDateFormat("dd-MM-yyyy"); 
     SimpleDateFormat postFormater = new SimpleDateFormat("yyyy-MM-dd"); 

     String eDDte; 
     try { 
      Date nDate = currFormater.parse(vv[0]); 
      eDDte = postFormater.format(nDate); 
      cv.put(Table.DATA,eDDte); 
     } 
     catch (Exception e) { 
     } 
     cv.put(Table.C,vv[1]); 
     cv.put(Table.E,vv[2]); 
     cv.put(Table.U,vv[3]); 
     cv.put(Table.C,vv[4]); 
      db.insert(Table.TABLE_NAME,null,cv); 
    } dataRead.close(); 

    } catch (Exception e) { Log.e("TAG",e.toString()); 

    } 
} 
+0

intent.setType ("text/csv"); 일반 파일 탐색기에서는 작동하지 않습니다. 파일에서 파일을 선택할 수 있습니다. 그러나 .csv 파일 만 보여주는 Dropbox 앱에는 영향을 미칩니다. 일반 파일 탐색기에서 동일한 작업을 수행하는 방법은 무엇입니까? –

관련 문제