2017-10-09 12 views
0

내 애플리케이션에서 서버로 파일을 업로드하려고합니다. 사용자는 내부 저장소에서 파일을 첨부 할 수 있습니다. 그러나 Nougat 전화에서 얻은 경로가 표준 /storage/emulated/0/...과 다른 점도 있습니다. 몇몇 전화기에서 나는 external_files/...을 얻었고 다른 것들은 root_files/storage/999/....이되었습니다. 이 경우 파일의 올바른 경로가 표시되지 않습니다. 이 시나리오를 어떻게 처리합니까?파일 첨부 및 서버에 업로드 - Android Nougat

내 코드 :

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode == RESULT_OK) { 
     Uri selectedImageUri = data.getData(); 
     String filePath = data.getData().getPath(); 
     File sourceFile = new File(fileFields.get(i).getFileName()); 
     FileInputStream fileInputStream = new FileInputStream(sourceFile); 
    } 
} 

답변

0

Uri이 파일이 아닙니다. Uri의 스키마가 file이 아닌 한 getPath()은 의미가 없습니다. 더 새로운 안드로이드 장치에는 그다지 일반적이지 않습니다. 대신 체계는 content이고 getPath()은 쓸모가 없습니다.

대신, 모두 filecontentUri 값를 들어, ContentResolveropenInputStream()Uri에 의해 확인 된 내용에 InputStream 얻기 위해 사용합니다. 이 접근법은 안드로이드 1.0으로 다시 돌아갑니다.

+0

예를 들어 주시겠습니까? 나는'ContentResolver'를 사용하려했지만'cursor.moveToFirst()'에'NPE'를 얻었습니다. –

+0

@SahanaPrabhakar : 제 대답에는'커서 '가 없습니다. 나는'openInputStream()'을 호출한다고 말했다. 'openInputStream()'은'Cursor'를 반환하지 않습니다. 'openInputStream()'은'InputStream'을 리턴합니다. 그래서,'getContentResolver(). openInputStream (selectedImageUri)'는 당신에게'Uri'에 의해 식별 된 내용에 대한'InputStream'을줍니다. – CommonsWare