[英]How can I anchor an image to the center within an Imageview in a scrollview, in Android?
[英]How can I download image for ImageView in Java, Android?
我需要從我的服務器下載圖像並將此圖像加載到 imageview 中。 所以我有一個問題 - 我可以將圖像下載到內存中並將其設置為 ImageView,而無需保存在 SD 卡/本地存儲上嗎? 或者我必須下載到某個文件存儲中? 如果可能的話,請給我例子。
同樣的問題在所以你可以搜索,
InputStream in = null;
try
{
URL url = new URL("URL FOR IMAGE");
URLConnection urlConn = url.openConnection();
HttpURLConnection httpConn = (HttpURLConnection) urlConn;
httpConn.connect();
in = httpConn.getInputStream();
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
bmpimg = BitmapFactory.decodeStream(in);
ImageView iv = "MY IMAGEVIEW";
iv.setImageBitmap(bmimg);
好的,首先將該庫實現到應用程序級別的 gradle 中,以便將圖像從 url 加載到圖像視圖中:
implementation("com.github.bumptech.glide:glide:4.6.1")
{
exclude group: "com.android.support"
}
現在在您的活動中編寫以下代碼以加載圖像:
Glide.with(this).load("url of your image").thumbnail(Glide.with(this).load(R.drawable.loadingsingleimage)).into(imageView);
現在通過實現以下代碼將其下載到您的設備存儲中..
download.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();
File filepath = Environment.getExternalStorageDirectory();
File dir = new File(filepath.getAbsolutePath() + "/MotivationalQuotes/");
dir.mkdir();
File file = new File(dir, System.currentTimeMillis() + ".png");
try {
outputStream = new FileOutputStream(file);
} catch (FileNotFoundException e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
try {
outputStream.flush();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
try {
outputStream.close();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
Toast.makeText(getApplicationContext(), "Downloaded into MotivationalQuotes.", Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
Toast.makeText(getApplicationContext(), "Image is not loaded yet...", Toast.LENGTH_SHORT).show();
}
}
});
這可以幫助你。
Bitmap bmImg;
void downloadFile(String fileUrl){
URL myFileUrl =null;
try {
myFileUrl= new URL(fileUrl);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bmImg = BitmapFactory.decodeStream(is);
imView.setImageBitmap(bmImg);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
來源: http : //en.androidwiki.com/wiki/Loading_images_from_a_remote_server
也看到這個
http://ballardhack.wordpress.com/2010/04/05/loading-remote-images-in-a-listview-on-android/
有關示例代碼,請參見此處。 您可以使用BitmapFactory來實現您的目標:
HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bmImg = BitmapFactory.decodeStream(is);
imView.setImageBitmap(bmImg);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.