2011-10-26 36 views
1

嘿,我是一個新的Android世界,並正在考慮製作一個小應用程序,它可以將多個圖像(可能來自指定的網址)下載到SD卡或下載文件夾中,然後這些圖像可以被視爲幻燈片(接下來有按鈕然後回來)。另外,當下載進行時,應該有一個進度條來顯示下載進度。我能夠爲一個圖像做但不是多個。 這裏是我的代碼:如何下載多個圖像並將其顯示爲幻燈片?

import java.io.BufferedInputStream; 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.net.URL; 
import java.net.URLConnection; 

import android.app.Activity; 
import android.app.Dialog; 

import android.app.ProgressDialog; 

import android.content.Intent; 
import android.net.Uri; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 


public class lab1_dhruv extends Activity{ 
    public static final int DIALOG_DOWNLOAD_PROGRESS = 0; 
     private Button startBtn; 
     private ProgressDialog mProgressDialog; 


     /** Called when the activity is first created. */ 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 
      startBtn = (Button)findViewById(R.id.startBtn); 
      startBtn.setOnClickListener(new OnClickListener(){ 
       public void onClick(View v) { 
        startDownload(); 
       } 
      }); 
     } 

     private void startDownload() { 

      //String url = "http://farm1.static.flickr.com/114/298125983_0e4bf66782_b.jpg"; 
      //new DownloadFileAsync().execute(url); 
      String[] imageList = {"http://farm1.static.flickr.com/114/298125983_0e4bf66782_b.jpg","http://fc03.deviantart.net/fs14/i/2007/086/9/1/Steve_Jobs_portrait_by_tumb.jpg","http://www.buzzreactor.com/sites/default/files/Bill-Gates1.jpg"}; 

      //String url1 = "http://fc03.deviantart.net/fs14/i/2007/086/9/1/Steve_Jobs_portrait_by_tumb.jpg"; 
      new DownloadFileAsync().execute(imageList); 


     } 
     @Override 
     protected Dialog onCreateDialog(int id) { 
      switch (id) { 
      case DIALOG_DOWNLOAD_PROGRESS: 
       mProgressDialog = new ProgressDialog(this); 
       mProgressDialog.setMessage("Downloading file.."); 
       mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
       mProgressDialog.setCancelable(false); 
       mProgressDialog.show(); 
       return mProgressDialog; 
      default: 
       return null; 
      } 
     } 

    class DownloadFileAsync extends AsyncTask<String, String, String> { 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      showDialog(DIALOG_DOWNLOAD_PROGRESS); 
     } 

     @Override 
     protected String doInBackground(String... aurl) { 
      int count; 

     try { 

     URL url = new URL(aurl[0]); 
     URLConnection conexion = url.openConnection(); 
     conexion.connect(); 

     int lenghtOfFile = conexion.getContentLength(); 
     Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile); 

     InputStream input = new BufferedInputStream(url.openStream()); 
     OutputStream output = new FileOutputStream("/sdcard/some_photo_from_gdansk_poland.jpg"); 

     byte data[] = new byte[1024]; 

     long total = 0; 

      while ((count = input.read(data)) != -1) { 
       total += count; 
       publishProgress(""+(int)((total*100)/lenghtOfFile)); 
       output.write(data, 0, count); 
      } 

      output.flush(); 
      output.close(); 
      input.close(); 
     } catch (Exception e) {} 
     return null; 

     } 
     protected void onProgressUpdate(String... progress) { 
      Log.d("ANDRO_ASYNC",progress[0]); 
      mProgressDialog.setProgress(Integer.parseInt(progress[0])); 
     } 

     @Override 
     protected void onPostExecute(String unused) { 
      dismissDialog(DIALOG_DOWNLOAD_PROGRESS); 
     } 
    } 

    public void showDownload(View view) { 
     Intent intent = new Intent(); 
     intent.setAction(android.content.Intent.ACTION_VIEW); 
     File file = new File("/sdcard/some_photo_from_gdansk_poland.jpg"); 
     intent.setDataAndType(Uri.fromFile(file), "image/*"); 
     startActivity(intent); 
     } 
    } 

這裏是main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/hello" /> 
    <Button 
     android:text="Start the download here" 
     android:id="@+id/startBtn" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 
    </Button> 

    <Button 
     android:text="Show Download Image" 
     android:id="@+id/startBtn1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:onClick="showDownload"> 
    </Button> 

這裏是AndroidManifest.xml中: -

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
     package="cmpe235.lab1.dhruv.assignment" 
     android:versionCode="1" 
     android:versionName="1.0"> 
    <uses-sdk android:minSdkVersion="9" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

    <application android:icon="@drawable/icon" android:label="@string/app_name"> 
     <activity android:name=".lab1_dhruv" 
        android:label="@string/app_name"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

    </application> 
</manifest> 

有什麼樣例子,我可以看到或能有人告訴我,我應該在我的代碼中做什麼。我是Android新手,所以不太瞭解。謝謝

回答

1

使用圖庫小部件。您需要製作一個適配器,用於爲您下載的圖像提供Gallery-Widget。我敢肯定,你可以找到關於如何實現這一頁或谷歌圖庫代碼;)

您已經實現了一個庫之後也有你的圖像之間自動翻轉幾個方面,這裏有一個例子:

Using the Android Gallery as an automated slideshow

+0

使用圖庫稍後完成。我被困在如何做多個文件下載並相應地閱讀它們。目前我可以下載許多文件,但我不知道,因爲我不知道如何檢查SD卡內容。 – Dhruv

相關問題