我想从不同的网址下载多张图片。
我的问题是并非所有照片都已加载 此问题出现在模拟器和移动版本2.2
中如果我想下载6张照片,应用程序只有5张。运行。 如果我想下载25张照片,那么应用程序只有12,16或20张。运行。 每次运行模拟器时都会有不同的结果:S
这在2.3模拟器上正确运行..
的.java
public class DownloadPhotos extends Activity{
Context context;
// Progress dialog --> shows that something is loading
ProgressDialog dialog;
// the layout where we insert the loaded pictures
LinearLayout linlayout;
// where we put all bitmaps after download
ArrayList<Bitmap> photos = new ArrayList<Bitmap>();
// URLs of photos we want to download
String [] urls = {
"http://www.flowersegypt.net/upload/Flowers-Egypt-6.jpg","http://www.kabloom.com/images/product_images/KB_11100.jpg"
,"http://faisal-saud.com/wp/wp-conteant/uploads/2010/09/QuilledFlowers.jpg",
"http://i3.makcdn.com/wp-content/blogs.dir/144387/files//2009/11/wedding-flowers1.jpg",
"http://www.funonthenet.in/images/stories/forwards/flowers/Blue-Bell-Tunicate.jpg",
"http://flowersfast.com/f4322dl.jpg"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// Setting the layout of the page to "downloadphoto.xml" layout
setContentView(R.layout.downloadphoto);
// Bind the previously defined layout with the lyout in the xml
linlayout = (LinearLayout) findViewById(R.id.linearLayout1);
this.context=this;
//Checking if Internet is connected
if(CheckConnection())
{
// Starts in Threads
new THREAD1().execute("");
}
}
//========== Threads ============
//===============================
private class THREAD1 extends AsyncTask<String, Bitmap, Void>
{
// This function shows the progress dialog
// and it works on foreground
// while the needed data is loaded
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
dialog = ProgressDialog.show(context , "",
"Loading. Please wait...", true);
}
// This function is responsible for loading the data
// and it works in the background
@Override
protected Void doInBackground(String... params) {
// TODO Auto-generated method stub
//get all bitmaps of the predefined URLs
for(int i=0 ;i<urls.length;i++)
{
Bitmap tmp = Networking.getimage(urls[i]);
if(tmp != null)
{
photos.add(tmp);
// This line calls the function "onProgressUpdate" for each loaded bitmap
publishProgress(tmp);
}
}
return null;
}
//this function is called in ech time
@Override
protected void onProgressUpdate(Bitmap... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
ImageView image = new ImageView(context);
image.setImageBitmap(values[0]);//ba3red el bitmap
image.setScaleType(ScaleType.CENTER);
image.setClickable(true);
image.setPadding(10,10, 10,10);
linlayout.addView(image);
dialog.cancel();
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
}
}
// This function returns true if Internet connected otherwise returns false
public Boolean CheckConnection()
{
ConnectivityManager conMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
boolean connected=( conMgr.getActiveNetworkInfo() != null &&
conMgr.getActiveNetworkInfo().isAvailable() &&
conMgr.getActiveNetworkInfo().isConnected() );
return connected;
}
}
我用来下载照片的功能 1。
public static Bitmap getimage(String URL)
{
Bitmap bitmap=null;
InputStream in=null;
try {
// all spaces must be replaced by 20%
String tmp = URL.replaceAll(" ", "20%");
in = OpenHttpConnection(tmp);
BitmapFactory.Options options=new BitmapFactory.Options();
// deh mas2ola enha trag3 1/2 el image
options.inSampleSize = 2 ;
options.inScaled = true;
bitmap = BitmapFactory.decodeStream(in, null, options);
in.close();
} catch (Exception e) {
// TODO: handle exception
}
return bitmap;
}
答案 0 :(得分:0)
你完全误解了AsyncTask
的想法。 AsyncTask
中使用了三种参数类型:输入类型,进度类型和输出类型。在您的情况下,输入类型应为String
,因为您有一个URI数组。如果要向用户显示当前进度,则进度类型应为Integer
,否则为Void
。输出类型为Bitmap
,因为AsyncTask
的结果实际上是已下载Bitmap
的数组。您的主要问题是AsyncTas
k的设计。尝试纠正它并再次运行您的应用程序,从那时起它很有可能工作。希望这会有所帮助。