我正在使用Android Studio構建我的應用程序,此應用程序可以將圖像從樹莓上傳到我的模擬器。它工作正常。我現在想做的就是上傳這張圖片,直接顯示給用戶,而不用在圖庫中搜索。我想創建另一個類,並在我的xml文件中將此圖像設置爲背景圖像,但這太像我每次想從樹莓上傳圖像時都必須創建另一個類。 有人可以幫助我。謝謝允許在Android Studio中顯示圖像的按鈕
1
A
回答
0
如果我正確理解你的問題,你想從Android文件系統加載一個圖像到你的應用程序並顯示給用戶。
Drawable
,Android的廣義圖像類,允許您通過Drawable#createFromPath
從文件加載。
This SO question表明Drawable#createFromPath
不與file://
路徑開始工作,所以根據您的使用情況,你可能想先與Uri#parse
/Uri#getPath
。
一旦你有一個Drawable
,您可以通過以下兩種方式之一顯示出來:把一個ImageView
在您的應用程序並調用其setImageDrawable
方法,或者設置Drawable
爲背景圖像通過View#setBackground
(注意:setBackground
在只增加API 16 - 在以前的版本中,您應該致電View#setBackgroundDrawable
)。
把所有的這一起,我們最終以下(未經測試):
private void loadImage(String imagePath) {
Uri imageUri;
String fullImagePath;
Drawable image;
ImageView imageDisplay;
imageUri = Uri.parse(imagePath);
fullImagePath = imageUri.getPath();
image = Drawable.createFromPath(fullImagePath);
imageDisplay = (ImageView) findViewById(R.id.imageDisplay);
/*if image is null after Drawable.createFromPath, this will simply
clear the ImageView's background */
imageDisplay.setImageDrawable(image);
/*if you want the image in the background instead of the foreground,
comment the line above and uncomment this bit instead */
//imageDisplay.setBackground(image);
}
你應該能夠修改此與任何View
工作只是通過用適當的替代imageDisplay
的聲明的類型View
鍵入並更改findViewById
。只要確保你打電話setBackground
,而不是setImageDrawable
,對於非ImageView
View
。
相關問題
- 1. Android:圖像按鈕或按鈕突出顯示效果時按
- 2. Android應用程序 - 允許用戶選擇按鈕圖像
- 3. Android Studio按鈕矩陣不會顯示
- 4. 在Android中顯示上方圖像按鈕
- 5. 在android中按鈕單擊後顯示圖像
- 6. 如何在Android中顯示和隱藏圖像按鈕?
- 7. 圖像不顯示在按鈕上
- 8. css在圖像上顯示按鈕
- 9. 在按鈕上單擊顯示圖像
- 10. 顯示圖像時,按下按鈕
- 11. Qt圖像頂部的顯示按鈕
- 12. 如何在按下按鈕時在黑莓中顯示圖像?
- 13. Android:在圖像中創建許多按鈕
- 14. Android圖像按鈕
- 15. Android圖像按鈕
- 16. android圖像按鈕
- 17. 如果圖像存在,在按鈕上顯示圖像
- 18. 在android中的按鈕圖像大小
- 19. 在android中變化的圖像按鈕
- 20. 在Qt GUI(Python)中按下按鈕時顯示圖像
- 21. 顯示字段,允許按鈕編輯和保存html
- 22. 如何在android studio中單擊按鈕時繪製圖像?
- 23. 可可按鈕不會顯示圖像
- 24. jQuery UI按鈕圖像不顯示
- 25. Xcode按鈕圖像不顯示
- 26. 像按鈕不顯示縮略圖
- 27. 在button_count中顯示零像按鈕
- 28. Android按鈕==>設置突出顯示的圖像?
- 29. 單選按鈕顯示圖像檢查
- 30. 點擊按鈕時顯示圖像?
謝謝你的回答,它似乎是正確的,但它說不能解決imageDisplay在這一個:imageDisplay =(ImageView)findViewById(R.id.imageDisplay); –
'R.id.imageDisplay'是'ImageView'的ID。你需要用你給自己的'ImageView'來替換它。 – computerfreaker
thnx男人,它完美的作品。 –