簡體   English   中英

如何訪問從網絡下載的圖像

[英]How to access image downloaded from the web

我使用此代碼從網絡下載了圖像,並將其直接保存到應用程序特定的內部存儲中。

is = url.openConnection().getInputStream();
os = LoginActivity.this.openFileOutput(id + ".jpg", Context.MODE_PRIVATE);
int read;
byte[] data = new byte[1024];
while ((read = is.read(data)) != -1)
    os.write(data, 0, read);

如何使用Drawable訪問此存儲的圖像? 我嘗試使用以下代碼打開它,但該操作導致文件未找到異常。

File imgPath = new File(id + ".jpg");
profilePic.setImageDrawable(Drawable.createFromPath(imgPath.getPath()));

假設您在代碼的以下幾行中調用os.close()

實例化File對象時,需要指定路徑。 請執行下列操作 :

File imgPath = new File(yourContext.getFilesDir(), id + ".jpg");

如在getFilesDir()方法的文檔中所說:

返回到使用openFileOutput(String,int)創建的文件存儲在文件系統上目錄的絕對路徑。

暫無
暫無

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

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