2012-12-11 40 views
2

我已經註冊我的應用程序以接收來自this post之後的其他應用程序的文件(任何類型,而不僅僅是圖像)。從其他應用程序檢索文件的內容名稱

我已經實施瞭解決方案,但我找不到一種方法來檢索數據流的「文件名」。 作爲從烏里像一個例子:

內容://下載/ all_downloads/5

我可以流出來,但我不知道有關原始文件的名稱,也生成它。

有沒有辦法檢索它?

回答

2

有沒有辦法檢索它?

一般來說,不,因爲可能沒有名稱,部分原因是可能沒有文件。您可以在內容上獲得InputStream,但這並不意味着InputStream後面有文件。

對於某些特定供應商(例如MediaStore),可能會嘗試確定與某些數據相關聯的文件名Uri,儘管這樣的黑客可能不可靠。

4

在大多數情況下,這將解決您的問題:

Uri intentData = intent.getData(); 
    if (intentData != null) { 
     String filePath; 
     if("content".equals(intent.getScheme())) 
     { 
      filePath = getFilePathFromContentUri(intentData); 
     } 
     else 
     { 
      filePath = intentData.getPath(); 
     } 
} 

private String getFilePathFromContentUri(Uri selectedUri) { 
    String filePath; 
    String[] filePathColumn = {MediaColumns.DATA}; 

    Cursor cursor = getContentResolver().query(selectedUri, filePathColumn, null, null, null); 
    cursor.moveToFirst(); 

    int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
    filePath = cursor.getString(columnIndex); 
    cursor.close(); 
    return filePath; 
} 
0

的onCreate()

Intent intent1 = getIntent(); 
     String action = intent1.getAction(); 
     String type = intent1.getType(); 

     if (Intent.ACTION_SEND.equals(action) && type != null) { 
      this.handleSend(intent1); 
     } 

void handleSend(Intent intent) { 
     try { 
      Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM); 
      imageShare.setImageURI(imageUri); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
相關問題