2017-08-01 103 views
0

我正在開發一個應用程序,我打開文件資源管理器並選擇我選擇的任何文件並檢索其內容。打開的默認路徑是/ storage/sdcard0。我能夠讀取直接駐留在此路徑中的任何文件的內容。但是,對於包含在/ storage/sdcard0 /中的任何文件夾中的任何文件。無法訪問。該程序給出一個文件未找到錯誤。另外,我無法理解的路徑,這些文件有,像例如,如果一個文件駐留在路徑:如何在Android中讀取特定路徑中的文件?

/storage/sdcard0/DCIM/100ANDRO/DSC_0001.jpg

, 的logcat中顯示的路徑是:

內容:/media/external/images/media/84290/DSC_0001.jpg

如何將在我的Java代碼中訪問這個文件?

下面的代碼片段:

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    Log.d(TAG, "requestCode received: "+requestCode+" result code: "+resultCode); 

    if (requestCode == 1 && resultCode == RESULT_OK) { 

     Bitmap thumbnail = (Bitmap) data.getExtras().get("data"); 
     String folderPath = "Anant"; 
     String filePath = "image.jpg"; 

     String imagePath = saveToInternalStorage(thumbnail, folderPath, 
       sImageName); 

     Log.i(TAG, "DeviceAPIS:actual path :: " 
       + imagePath.trim().toString()); 
     sendCameraData(imagePath.toString().trim(), 
       ptrAppContainerForCamera); 
    } 

    else if (requestCode == REQUEST_PATH){ 
     if (resultCode == RESULT_OK) { 
       // Get the Uri of the selected file 
      Uri uri = data.getData(); 
      Log.d(TAG, "data.getData() result line 742: " + uri); 
      String uriString = uri.toString(); 
      File myFile = new File(uriString); 
      String path = myFile.getAbsolutePath(); 
      String base64 ="Error"; 
      byte[] bytesRead = base64.getBytes(); 
      String displayName = null; 
      String fileName = null; 

      if (uriString.startsWith("content://")) {    
       Cursor cursor = null; 
       try {       
        cursor = mContext.getContentResolver().query(uri, null, null, null, null);       
        if (cursor != null && cursor.moveToFirst()) {        
         displayName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)); 
         displayName = path + "/" + displayName; 
         Log.d(TAG, "BEFORE REPLACE: "+displayName); 
         int index = displayName.indexOf(':'); 
         fileName = displayName.substring(index + 1, displayName.length()); 
         Log.d(TAG,"displayName line 762 " + Part); 
        } 
       } finally { 
        cursor.close(); 
       } 
      } else if (uriString.startsWith("file://")) { 
       Log.d(TAG, "FILE BLOCK LINE 768");   
       displayName = myFile.getName(); 
       Log.d(TAG,"displayName 11" + displayName); 
      } 


      try{ 
       File sdcard = Environment.getExternalStorageDirectory(); 
       File readFile = new File(sdcard, fileName); 
     //  File readFile = new File(uri); 

       int length = (int)readFile.length(); 

       byte[] bytes = new byte[length]; 

       FileInputStream in = new FileInputStream(readFile); 
       try{ 
        in.read(bytes); 
       }finally { 
        in.close(); 
       } 

       String contents = new String(bytes); 
       Log.d(TAG,"contents read :: \\n" + contents); 

       //convert to Base64 
       bytesRead = contents.getBytes("UTF-8"); 
       base64 = Base64.encodeToString(bytesRead,Base64.DEFAULT); 

      }catch (Exception e){ 
       Log.d(TAG, "THROWING EXCEPTION"); 
       Log.e(TAG,e.getMessage(),e); 
      } 
     } 

引發的異常是java.io.FileNotFoundException。任何幫助是極大的讚賞。

回答

0

您可以嘗試如下:

String path = null; 
    Uri originalUri = data.getData(); 
    try { 
     String[] proj = { MediaStore.Images.Media.DATA }; 
     Cursor cursor = this.managedQuery(originalUri, proj, null, null, null); 
     int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
     cursor.moveToFirst(); 
     path = cursor.getString(column_index); 
    } catch (Exception e) { 

    } finally { 
     cursor.close(); 
    } 

如果路徑爲空,你可以先得到位圖,然後將它複製到你的本地路徑。

ContentResolver resolver = this.getContentResolver(); 
    Bitmap photo = MediaStore.Images.Media.getBitmap(resolver, originalUri); 
    .... // copy photo to your local path 
+0

哇!這真的有用!萬分感謝! :) :) – Naruto

0

你可以試試這個,

1. make sure you have added permission in you manifest file 
2. Settings -> Apps -> Your App -> Permissions -> Storage = true/enabled 

我曾面臨FileNotFound的同樣的問題,我能夠通過上述#2解決它。

+0

嗨@JTeam,權限不是我想這裏的問題。這是代碼本身。 – Naruto

相關問題