2017-10-24 1 views
0

을 가져올 실제 파일 경로로하여 onActivityResult에서 URI를 변환합니까하지만 File file = new File(path)을 쓸 때 그것은 FileNotFoundException가 발생합니다. 도와주세요.어떻게 <code>/document/primary:Download/West Delhi Mis June.xlsx</code></p> <p>으로 내가 <code>String path = uri.getPath()</code>에서 URI를 얻고 해당 파일

public void getdata(File fil){ 

    try{ 
     Workbook w; 
     w = Workbook.getWorkbook(fil); 
     Sheet sheet = w.getSheet(0); 
     for (int j = 1; j<sheet.getRows(); j++){ 

      Cell c1 = sheet.getCell(0,j); 
      Cell c2 = sheet.getCell(1,j); 
      Cell c3 = sheet.getCell(2,j); 
      Cell c4 = sheet.getCell(3,j); 
      Cell c5 = sheet.getCell(4,j); 
      Cell c6 = sheet.getCell(5,j); 
      Cell c7 = sheet.getCell(6,j); 
      Cell c8 = sheet.getCell(7,j); 
      Cell c9 = sheet.getCell(8,j); 
      Cell c10 = sheet.getCell(9,j); 
      Cell c11 = sheet.getCell(10,j); 
      Cell c12 = sheet.getCell(11,j); 
      Cell c13 = sheet.getCell(12,j); 

      String date = c1.getContents(); 
      String empid = c2.getContents(); 
      String project = c3.getContents(); 
      String name = c4.getContents(); 
      String route = c5.getContents(); 
      String cabno = c6.getContents(); 
      String location = c7.getContents(); 
      String contact = c8.getContents(); 
      String gender = c9.getContents(); 
      String duty = c10.getContents(); 
      String shift = c11.getContents(); 
      String cabtype = c12.getContents(); 
      String zone = c13.getContents(); 

      adb.insertRoastData(date,empid,project,name,route,cabno,location,contact,gender,duty,shift,cabtype,zone); 
     } 
    }catch (Exception e){ 
     e.printStackTrace(); 
    } 

} 

public void performFileSearch() { 

    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); 

    intent.addCategory(Intent.CATEGORY_OPENABLE); 

    intent.setType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); 

    startActivityForResult(intent, READ_REQUEST_CODE); 
} 

@Override 
public void onActivityResult(int requestCode, int resultCode, 
          Intent resultData) { 
    if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK) { 
     Uri uri = null; 
     if (resultData != null) { 
      uri = resultData.getData(); 
      String path = uri.getPath(); 
      InputStream dataStream = this.getContentResolver().openInputStream(path); 

     } 
    } 
} 
+0

uri를 어떻게 받고 있습니까? – codeFreak

+0

":"을 "/"로 바꾸고 path.replace (":", "/"); – Gautam

+0

안드로이드 파일 선택기를 통해 @codeFreak – user6698813

답변

4

당신은 직접 파일에 열린 우리당을 변환 할 수 없습니다 :

내 코드입니다.
Uri는 실제 절대 경로를 포함하지 않을 수도 있습니다.

Nougat의 새로운 파일 보안 모델의 일부인 실제 파일을 제공하기 위해 다른 앱의 ContentProvider를 거쳐야 할 수도 있기 때문입니다.

그럼 어떻게 할 수 있습니까? 운영체제를 InputStream으로 열게하십시오 :

Uri uri = ... 
InputStream dataStream = context.getContentResolver().openInputStream(uri); 
// now buffer and read stream 
+0

오류로 - - openInputStream (android.net.Uri) ContentResolver에 ~ (java.lang.String) @RobCo – user6698813

+0

매개 변수로 경로를 수락하지 않습니다. @RobCo – user6698813

+0

@ user6698813 :'Uri'을'openInputStream()'에 건네줍니다. – CommonsWare

관련 문제