簡體   English   中英

如何從URI獲取文件?

[英]How to get a File from a URI?

我從我的應用中的圖片庫中選擇了一張圖片。 在onActivityResult中,它返回一個Intent對象,然后在其上執行getData()方法。 我得到了圖像URI。 我現在正在嘗試將其轉換為文件,以便將其發送到AWS S3。 這是我一直在嘗試但它只返回null:

  String[] filePathColumn = { MediaStore.Images.Media.DATA };
        Cursor cursor = context.getContentResolver().query(contentUri, filePathColumn, null, null, null);
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String filePath = cursor.getString(columnIndex);
        cursor.close();

我也試試

File file = new File(imageURI.getPath())

也是null。 這是不可能的還是有人可以幫忙?

假設您的onActivityResult檢入Intent對象為ReturnedIntent您可以這樣做

File f = new File(ReturnedIntent.getData()); 

或獲取輸入流並使用它

getContentResolver().openInputStream(ReturnedIntent.getData()) 
//returns inputstream

這個String對象String filePath的本質是什么?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM