簡體   English   中英

如何在Android中打開裝有Uri的文件?

[英]How do I open a file I have the Uri for in Android?

我正在嘗試打開我存儲在外部存儲器中的圖像。 這是我的代碼:

File imagePath = new File(imageURI);
InputStream inputStream=null;
try {
    inputStream = getContentResolver().openInputStream(Uri.parse(imageURI));
}catch(FileNotFoundException e){
    e.printStackTrace();
}
Bitmap bm = BitmapFactory.decodeStream(inputStream);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] originalImage = baos.toByteArray();

但是它似乎無法找到該文件。 Uri的格式為content://com.android.providers.media.documents/document/image%3A21。 謝謝你的幫助。

現在在一個項目中,我在SD卡上的目錄中有外部圖像。 我在用

 String thePath = Environment.getExternalStorageDirectory() + "/myAppFiles”; File imgFile = new File(thePath + " / " + " externalImage.jpg "); if (imgFile.exists()) { try { BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 6; Bitmap bm = BitmapFactory.decodeFile(imgFile.getAbsolutePath(), options); } } 

嘗試這個:

File imagePath = new File(imageURI.getPath());

url.getPath()返回以下格式的字符串:“ / mnt / sdcard / xxx.jpg”,而方案類型未預先固定

暫無
暫無

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

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