[英]Android Studio - Download images online
在我的應用程序中,我希望能夠從站點(ddragon)加載很多圖像。
問題在於,每次我要加載圖像時,都需要花費大量時間和精力才能從設備上保存這些照片,因此這也是一個問題。
我想知道處理大尺寸(高度和寬度為200〜300 dp)的大量圖像(500幅以上)的最佳方法是什么,謝謝!
(現在我正在使用Universal Image Loader)
imageLoader = ImageLoader.getInstance();
imageLoader.displayImage(q.getImageURL(), questionImage, null, new ImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
loadingView.setVisibility(View.VISIBLE);
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
finish();
Toast.makeText(getApplicationContext(), "Failed loading the image...\nID: " + q.getID(), Toast.LENGTH_LONG).show();
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
loadingView.setVisibility(View.INVISIBLE);
myCountDownTimer.start();
}
@Override
public void onLoadingCancelled(String imageUri, View view) {
}
});
MainActivity.java
import java.io.InputStream;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
// Set your Image URL into a string
String URL = "Image URL for example http://www.google.com";
ImageView image;
Button button;
ProgressDialog mProgressDialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the layout from image.xml
setContentView(R.layout.activity_main);
// Locate the ImageView in activity_main.xml
image = (ImageView) findViewById(R.id.image);
// Locate the Button in activity_main.xml
button = (Button) findViewById(R.id.button);
// Capture button click
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// Execute DownloadImage AsyncTask
new DownloadImage().execute(URL);
}
});
}
// DownloadImage AsyncTask
private class DownloadImage extends AsyncTask<String, Void, Bitmap> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(MainActivity.this);
// Set progressdialog title
mProgressDialog.setTitle("Download Image Tutorial");
// Set progressdialog message
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
// Show progressdialog
mProgressDialog.show();
}
@Override
protected Bitmap doInBackground(String... URL) {
String imageURL = URL[0];
Bitmap bitmap = null;
try {
// Download Image from URL
InputStream input = new java.net.URL(imageURL).openStream();
// Decode Bitmap
bitmap = BitmapFactory.decodeStream(input);
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap result) {
// Set the bitmap into ImageView
image.setImageBitmap(result);
// Close progressdialog
mProgressDialog.dismiss();
}
}
}
Activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" >
</ImageView>
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/image"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/button" />
</RelativeLayout>
和清單中的互聯網許可
<uses-permission android:name="android.permission.INTERNET" >
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.