0

아래 코드는 API 레벨 19에서 잘 작동하며 경로는 /storage/downloads/w.xls 으로 표시됩니다. API 수준의 오류 26.파일이 발견되지 않음 Android 파일의 예외 Kitkat에서 제대로 작동하는 동안

제발, 어떻게 해결합니까?

java.io.FileNotFoundException: /document/primary:Download/W.xls (No such file or directory)

public void getdata(String filestring){ 
    try{ 
     File file = new File(filestring); 
     Workbook w; 
     w = Workbook.getWorkbook(file); 
     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); 

      String date = c1.getContents(); 
      String empid = c2.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.ms-excel"); 
    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 filepath = getPath(uri); 
      getdata(filepath); 
     } 
    } 
} 
public String getPath(Uri uri) { 
    String path = null; 
    String[] projection = { MediaStore.Files.FileColumns.DATA }; 
    Cursor cursor = getContentResolver().query(uri, projection, null, null, null); 
    if(cursor == null){ 
     path = uri.getPath(); 
    } 
    else{ 
     cursor.moveToFirst(); 
     int column_index = cursor.getColumnIndexOrThrow(projection[0]); 
     path = cursor.getString(column_index); 
     cursor.close(); 
    } 
    return ((path == null || path.isEmpty()) ? (uri.getPath()) : path); 
} 
+0

필요한 저장 권한이 있으며 런타임에 요청 했습니까? –

+0

기존 경로가 없습니다. 당신이 보지마.? 내용 체계를 파일 경로로 변환하려고 시도하지 마십시오. 콘텐츠 스키마 자체를 사용하십시오. – greenapps

+0

'w = Workbook.getWorkbook (file);'. File 개체를 나타내는 것으로 통합 문서를 만듭니다. 대신에 InputStream을 나타낼 가능성이 있습니까? – greenapps

답변

-1

GET 경로에 대한이 코드를 사용해보십시오는

/** 
* Get a file path from a Uri. This will get the the path for Storage Access 
* Framework Documents, as well as the _data field for the MediaStore and 
* other file-based ContentProviders. 
* 
* @param context The context. 
* @param uri The Uri to query. 
* @author paulburke 
*/ 
@TargetApi(Build.VERSION_CODES.KITKAT) 
public static String getPath(final Context context, final Uri uri) { 

    final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; 

    // DocumentProvider 
    if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) { 
     // ExternalStorageProvider 
     if (isExternalStorageDocument(uri)) { 
      final String docId = DocumentsContract.getDocumentId(uri); 
      final String[] split = docId.split(":"); 
      final String type = split[0]; 

      if ("primary".equalsIgnoreCase(type)) { 
       return Environment.getExternalStorageDirectory() + "/" + split[1]; 
      } 

      // TODO handle non-primary volumes 
     } 
     // DownloadsProvider 
     else if (isDownloadsDocument(uri)) { 

      final String id = DocumentsContract.getDocumentId(uri); 
      final Uri contentUri = ContentUris.withAppendedId(
        Uri.parse("content://downloads/public_downloads"), Long.valueOf(id)); 

      return getDataColumn(context, contentUri, null, null); 
     } 
     // MediaProvider 
     else if (isMediaDocument(uri)) { 
      final String docId = DocumentsContract.getDocumentId(uri); 
      final String[] split = docId.split(":"); 
      final String type = split[0]; 

      Uri contentUri = null; 
      if ("image".equals(type)) { 
       contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; 
      } else if ("video".equals(type)) { 
       contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; 
      } else if ("audio".equals(type)) { 
       contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; 
      } 

      final String selection = "_id=?"; 
      final String[] selectionArgs = new String[] { 
        split[1] 
      }; 

      return getDataColumn(context, contentUri, selection, selectionArgs); 
     } 
    } 
    // MediaStore (and general) 
    else if ("content".equalsIgnoreCase(uri.getScheme())) { 

     // Return the remote address 
     if (isGooglePhotosUri(uri)) 
      return uri.getLastPathSegment(); 

     return getDataColumn(context, uri, null, null); 
    } 
    // File 
    else if ("file".equalsIgnoreCase(uri.getScheme())) { 
     return uri.getPath(); 
    } 

    return null; 
} 

/** 
* Get the value of the data column for this Uri. This is useful for 
* MediaStore Uris, and other file-based ContentProviders. 
* 
* @param context The context. 
* @param uri The Uri to query. 
* @param selection (Optional) Filter used in the query. 
* @param selectionArgs (Optional) Selection arguments used in the query. 
* @return The value of the _data column, which is typically a file path. 
*/ 
public static String getDataColumn(Context context, Uri uri, String selection, 
            String[] selectionArgs) { 

    Cursor cursor = null; 
    final String column = "_data"; 
    final String[] projection = { 
      column 
    }; 

    try { 
     cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, 
       null); 
     if (cursor != null && cursor.moveToFirst()) { 
      final int index = cursor.getColumnIndexOrThrow(column); 
      return cursor.getString(index); 
     } 
    } finally { 
     if (cursor != null) 
      cursor.close(); 
    } 
    return null; 
} 


/** 
* @param uri The Uri to check. 
* @return Whether the Uri authority is ExternalStorageProvider. 
*/ 
public static boolean isExternalStorageDocument(Uri uri) { 
    return "com.android.externalstorage.documents".equals(uri.getAuthority()); 
} 

/** 
* @param uri The Uri to check. 
* @return Whether the Uri authority is DownloadsProvider. 
*/ 
public static boolean isDownloadsDocument(Uri uri) { 
    return "com.android.providers.downloads.documents".equals(uri.getAuthority()); 
} 

/** 
* @param uri The Uri to check. 
* @return Whether the Uri authority is MediaProvider. 
*/ 
public static boolean isMediaDocument(Uri uri) { 
    return "com.android.providers.media.documents".equals(uri.getAuthority()); 
} 

/** 
* @param uri The Uri to check. 
* @return Whether the Uri authority is Google Photos. 
*/ 
public static boolean isGooglePhotosUri(Uri uri) { 
    return "com.google.android.apps.photos.content".equals(uri.getAuthority()); 
} 
+0

알릴 것이다 그 오류 - jxl.read.biff.BiffException : OLE 스트림을 인식 할 수 없습니다 – user6698813

0

아래의 코드는 API 레벨에서 잘 작동 19

아니, 그렇지 않습니다. ACTION_OPEN_DOCUMENT에 의해 반환되는 Uri에 대한 요구 사항은 MediaStore이어야합니다. 또한, ACTION_OPEN_DOCUMENT에 의해 반환 된 Uri은 사용자가 액세스 권한이있는 것은 물론, 로컬 파일 시스템의 파일을 나타내지 않아도됩니다.

사용하십시오 (예 : Activity 같은 ContextgetContentResolver()를 호출하는 것으로 취득) ContentResolveropenInputStream()Uri에 의해 확인 된 내용에 InputStream을 얻을 수 있습니다.

+0

나는 그 시도도하지만 그 @ CommonsWare 작동하지 않습니다 – user6698813

+0

@ user6698813 : "일하지 않는다"는 것을 설명하지 않으면 아무도 도와 줄 수 없습니다. – CommonsWare

+0

@ user6698813 :'openInputStream()'을 사용하는 [여기 샘플 애플 리케이션] (https://github.com/commonsguy/cw-omnibus/tree/v8.7/Documents/Durable)입니다. [여기에 또 하나 있습니다] (https://github.com/commonsguy/cw-omnibus/tree/v8.7/Documents/TinyTextEditor). [다음은 1/3] (https://github.com/commonsguy/cw-omnibus/tree/v8.7/Documents/Diceware). 모두 'ACTION_OPEN_DOCUMENT'을 (를) 포함합니다. 모든 내용은 [이 책] (https://commonsware.com/Android/)의 한 장에서 다룹니다.[다음 장의 미리보기 버전입니다] (https://commonsware.com/Android/previews/consuming-documents). – CommonsWare