綁定帳號登入

Android 台灣中文網

打印 上一主題 下一主題

[求助] 如何實現Gameloft數據包下載功能

[複製連結] 查看: 2409|回覆: 9|好評: 0
跳轉到指定樓層
樓主
lkk47 | 收聽TA | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
發表於 2011-10-6 10:49

馬上加入Android 台灣中文網,立即免費下載應用遊戲。

您需要 登錄 才可以下載或查看,沒有帳號?註冊

x
第一次執行遊戲時會先下載數據包  請問如何實現??
例如:有一個ProgressDialog  然後後台有下載數據包??

還有一個問題  資料夾要上傳時要先壓縮成rar才可以上傳   那android下載下來還是解壓縮格式   如何讓android下載下來就是一個資料夾(資料夾裡面包含檔案  類似數據包)
「用Android 就來APK.TW」,快來加入粉絲吧!
Android 台灣中文網(APK.TW)
收藏收藏 分享分享 分享專題
用Android 就來Android 台灣中文網(https://apk.tw)
回覆

使用道具 舉報

沙發
ploglin | 收聽TA | 只看該作者
發表於 2011-10-6 11:59
下面是我之前實作下載一個zip檔案(裡面是sqlite的檔案),解壓縮的過程,所用的方式是使用 ProgressBar ,提供你參考

WelcomeActivity.java
  1. public class WelcomeActivity extends Activity implements Runnable {
  2.         private static final String TAG = WelcomeActivity.class.getSimpleName();
  3.         private final int HANDLER_OF_SUCCESS = 0;
  4.         private final int HANDLER_OF_DOWNLOAD = 1;
  5.         private final int HANDLER_OF_DOWNLOAD_END = 2;
  6.         private int file_size = 0;
  7.         private int down_size = 0;
  8.         private Thread thread;
  9.         private DownloadHelper db;
  10.         private ProgressBar progressBar1;
  11.         private TextView textView1;
  12.         private File download_path = null;

  13.         /** Called when the activity is first created. */
  14.         @Override
  15.         public void onCreate(Bundle savedInstanceState) {
  16.                 super.onCreate(savedInstanceState);
  17.                 setContentView(R.layout.welcome);

  18.                 if (Integer.valueOf(android.os.Build.VERSION.SDK) > 7) {
  19.                         download_path = Environment.getExternalStoragePublicDirectory("/mctw");
  20.                 } else {
  21.                         download_path = new File("/sdcard/mctw");
  22.                 }
  23.                 download_path.mkdirs();

  24.                 progressBar1 = (ProgressBar) findViewById(R.id.progressBar1);
  25.                 textView1 = (TextView) findViewById(R.id.textView1);

  26.                 thread = new Thread(this);
  27.                 thread.start();
  28.         }

  29.         @Override
  30.         public void run() {
  31.                 // TODO Auto-generated method stub
  32.                 String download_link = "http://xx.xx.xx.xx/app/data/xxx.zip";
  33.                 File download_file = new File(download_path, "xxx.zip");

  34.                 try {
  35.                         URL download_url = new URL(download_link);
  36.                         URLConnection conn = download_url.openConnection();
  37.                         conn.connect();
  38.                         InputStream is = conn.getInputStream();
  39.                         file_size = conn.getContentLength();
  40.                         FileOutputStream fos = new FileOutputStream(download_file);
  41.                         byte buf[] = new byte[1024];
  42.                         down_size = 0;
  43.                         sendMsg(HANDLER_OF_DOWNLOAD);
  44.                         do {
  45.                                 int numread = is.read(buf);
  46.                                 if (numread == -1) {
  47.                                         break;
  48.                                 }
  49.                                 fos.write(buf, 0, numread);
  50.                                 down_size += numread;
  51.                                 sendMsg(HANDLER_OF_DOWNLOAD);
  52.                         } while (true);
  53.                         sendMsg(HANDLER_OF_DOWNLOAD_END);
  54.                         is.close();
  55.                 } catch (MalformedURLException e) {
  56.                         // TODO Auto-generated catch block
  57.                         Log.e(TAG, e.getMessage());
  58.                 } catch (IOException e) {
  59.                         // TODO Auto-generated catch block
  60.                         Log.e(TAG, e.getMessage());
  61.                 }
  62.         }

  63.         private void sendMsg(int flag) {
  64.                 Message msg = new Message();
  65.                 msg.what = flag;
  66.                 handler.sendMessage(msg);
  67.         }

  68.         private Handler handler = new Handler() {
  69.                 @Override
  70.                 public void handleMessage(Message msg) {
  71.                         Bundle bundle = msg.getData();
  72.                         switch (msg.what) {
  73.                         case HANDLER_OF_SUCCESS:
  74.                                 break;
  75.                         case HANDLER_OF_DOWNLOAD:
  76.                                 double x = (double) down_size;
  77.                                 double y = (double) file_size;
  78.                                 double person = (x / y) * 100;
  79.                                 int personInt = (int) person;
  80.                                 if (progressBar1 != null) {
  81.                                         progressBar1.setProgress(personInt);
  82.                                         textView1.setText(getString(R.string.app_download) + Integer.toString(personInt) + "%");
  83.                                 }
  84.                                 break;
  85.                         case HANDLER_OF_DOWNLOAD_END:
  86.                                 textView1.setText(getString(R.string.app_compress));
  87.                                 File download_file = new File(download_path, "xxx.zip");
  88.                                 File sqlite_file = new File(download_path, "xxx.db");
  89.                                 try {
  90.                                         FileInputStream fin = new FileInputStream(download_file);
  91.                                         ZipInputStream zin = new ZipInputStream(new BufferedInputStream(fin));
  92.                                         ZipEntry entry;
  93.                                         while ((entry = zin.getNextEntry()) != null) {
  94.                                                 int size;
  95.                                                 byte[] buffer = new byte[2048];

  96.                                                 FileOutputStream fos = new FileOutputStream(sqlite_file);
  97.                                                 BufferedOutputStream bos = new BufferedOutputStream(fos, buffer.length);

  98.                                                 while ((size = zin.read(buffer, 0, buffer.length)) != -1) {
  99.                                                         bos.write(buffer, 0, size);
  100.                                                 }
  101.                                                 bos.flush();
  102.                                                 bos.close();
  103.                                         }
  104.                                         zin.close();
  105.                                         fin.close();
  106.                                 } catch (FileNotFoundException e) {
  107.                                         // TODO Auto-generated catch block
  108.                                         Log.e(TAG, e.getMessage());
  109.                                 } catch (IOException e) {
  110.                                         // TODO Auto-generated catch block
  111.                                         Log.e(TAG, e.getMessage());
  112.                                 }
  113.                                 download_file.delete();
  114.                                 textView1.setText(getString(R.string.app_welcome));
  115.                                 startActivity(new Intent().setClass(WelcomeActivity.this, MenuActivity.class));
  116.                                 finish();
  117.                                 break;
  118.                         }
  119.                 }
  120.         };
  121. }
複製代碼
welcome.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="vertical"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent"
  6.     >
  7.     <FrameLayout android:id="@+id/frameLayout1" android:layout_width="match_parent" android:layout_height="match_parent">
  8.         <LinearLayout android:id="@+id/linearLayout1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_gravity="center" android:orientation="vertical">
  9.             <ProgressBar android:layout_height="wrap_content" style="?android:attr/progressBarStyleHorizontal" android:id="@+id/progressBar1" android:layout_width="match_parent" android:max="100"></ProgressBar>
  10.             <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:id="@+id/textView1" android:layout_gravity="center" android:text="@string/app_download"></TextView>
  11.         </LinearLayout>
  12.     </FrameLayout>
  13. </LinearLayout>
複製代碼
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

板凳
 樓主| lkk47 | 收聽TA | 只看該作者
發表於 2011-10-6 16:30

謝謝你的程式碼  請問一下我使用AsyncTask方式在doInBackground  進行了後台下載檔案
那麼ZipInputStream也是放在doInBackground 下載檔案的後面嗎??
我貼我一小段程式碼你看一下  我把ZipInputStream放到檔案下載完成後執行
但是執行後只有下載檔案沒有解壓縮檔案
小弟幾個月前才碰android如果有亂寫地方請多多指教
  1. protected String doInBackground(String... params) {
  2.                     try {
  3.                         URL url = new URL( "http://10.0.2.2/Image666.zip" );
  4.                         HttpURLConnection connect = (HttpURLConnection) url.openConnection();
  5.                         connect.setDoInput( true );
  6.                         connect.setDoOutput( true );
  7.                         connect.setRequestMethod( "POST" );
  8.                         
  9.                         if ( connect.getResponseCode() == 200 ) {
  10.                             File outFile = new File( "mnt/sdcard/Image666.zip" );
  11.                             out = new FileOutputStream( outFile );

  12.                             InputStream in = connect.getInputStream();
  13.                             copy( in, out, 32 );
  14.                             in.close();
  15.                             out.close();
  16.                             
  17.                         }
  18.                         connect.disconnect();
  19.                         
  20.                         FileInputStream fin = new FileInputStream("mnt/sdcard/Image666.zip");
  21.                         ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fin));
  22.                         try {
  23.                             ZipEntry ze;
  24.                             while ((ze = zis.getNextEntry()) != null) {
  25.                                     byte[] buffer = new byte[1024];
  26.                                     BufferedOutputStream bos = new BufferedOutputStream(out, buffer.length);
  27.                                 
  28.                                 int count;
  29.                                 
  30.                                 while ((count = zis.read(buffer,0,buffer.length)) != -1) {
  31.                                     bos.write(buffer, 0, count);
  32.                                 }
  33.                                 bos.flush();
  34.                         bos.close();

  35.                             }
  36.                         } finally {
  37.                             zis.close();
  38.                         }
  39.                     } catch ( MalformedURLException e ) {
  40.                         e.printStackTrace();
  41.                     } catch ( IOException e ) {
  42.                         // TODO Auto-generated catch block
  43.                         e.printStackTrace();
  44.                     }
複製代碼
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

地板
ploglin | 收聽TA | 只看該作者
發表於 2011-10-6 16:57
你看你的 LogCat 有沒有什麼錯誤訊息,貼上來讓大家研究一下。
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

5
 樓主| lkk47 | 收聽TA | 只看該作者
發表於 2011-10-6 18:15
本帖最後由 lkk47 於 2011-10-6 18:17 編輯
ploglin 發表於 2011-10-6 16:57
你看你的 LogCat 有沒有什麼錯誤訊息,貼上來讓大家研究一下。


紅色不代表錯誤訊息  我把它標紅色看起來比較清楚
在logcat沒有顯示紅色錯誤訊息只有藍色跟綠色訊息


10-06 10:03:16.066: DEBUG/dalvikvm(335): GC_CONCURRENT freed 100K, 69% free 318K/1024K, external 0K/0K, paused 2ms+2ms
10-06 10:03:16.066: INFO/ActivityManager(61): Start proc com.TravelPro for activity com.TravelPro/.Travel: pid=346 uid=10035 gids={1015, 3003}
10-06 10:03:16.096: INFO/AndroidRuntime(335): NOTE: attach of thread 'Binder Thread #3' failed
10-06 10:03:16.146: DEBUG/jdwp(335): adbd disconnected
10-06 10:03:17.655: INFO/ActivityManager(61): Displayed com.TravelPro/.Travel: +1s649ms (total +31s597ms)
10-06 10:03:17.665: INFO/ActivityManager(61): Displayed com.android.launcher/com.android.launcher2.Launcher: +31s603ms
10-06 10:08:00.186: DEBUG/SntpClient(61): request time failed: java.net.SocketException: Address family not supported by protocol
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

6
ploglin | 收聽TA | 只看該作者
發表於 2011-10-7 12:30
對照了很久,不太知道是什麼問題。
不過下面這段你可以換看看
  1. FileInputStream fin = new FileInputStream("mnt/sdcard/Image666.zip");
複製代碼
換成
  1. FileInputStream fin = new FileInputStream(new File(download_path, "Image666.zip");
複製代碼
還有

你的 out 應該是要指向產出的檔案,這點我不確定你是否定義正確。

希望幫的到你
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

7
 樓主| lkk47 | 收聽TA | 只看該作者
發表於 2011-10-7 16:55
本帖最後由 lkk47 於 2011-10-8 17:21 編輯
ploglin 發表於 2011-10-7 12:30
對照了很久,不太知道是什麼問題。
不過下面這段你可以換看看換成還有


我現在改成執行緒方式寫  我現在先做一下動態ProgressDialog(就是下載檔案進度去改變進度條)  再來去解決解壓縮檔  
現在在進度條有個問題在run方法裡有個 file_size = conn.getContentLength();     請問是獲得下載進度的數據嗎??

如果是的話可以progressBar1.setProgress(file_size);獲取進度嗎??

真的謝謝你  應為昨天寫到後面很亂想整理一下

你程式碼裡面的進度條是不是檔案解壓縮的進度而不是檔案下載的進度對嗎??

我壓縮出來檔案是資料夾檔案File folder_file = new File(sdpath, "xx"); xx是資料夾檔名的話那資料夾附檔名是什麼xx.副檔名
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

8
ploglin | 收聽TA | 只看該作者
發表於 2011-10-7 22:41
我寫的進度條是針對檔案下載的進度唷,解壓縮那邊就沒有使用進度條了。

conn.getContentLength() 是取得 server 回傳檔案的大小。再利用換算同步更新 progressBar。
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

9
 樓主| lkk47 | 收聽TA | 只看該作者
發表於 2011-10-8 11:45
本帖最後由 lkk47 於 2011-10-8 20:23 編輯
ploglin 發表於 2011-10-7 22:41
我寫的進度條是針對檔案下載的進度唷,解壓縮那邊就沒有使用進度條了。

conn.getContentLength() 是取得 s ...


喔  謝謝你我了解  有問題再問你  感謝

你的程式碼完全府和我的需求  謝謝

private void sendMsg(int flag)這方法我不太懂可以大概說明一下嗎??

我壓縮出來檔案是資料夾檔案File folder_file = new File(sdpath, "xx"); xx是資料夾檔名的話那資料夾附檔名是什麼xx.副檔名
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

10
 樓主| lkk47 | 收聽TA | 只看該作者
發表於 2011-10-12 11:31
ploglin 發表於 2011-10-7 22:41
我寫的進度條是針對檔案下載的進度唷,解壓縮那邊就沒有使用進度條了。

conn.getContentLength() 是取得 s ...

請問一下我壓縮檔裡面有很多檔案  但是解壓縮出來只有一個檔案在sdcard  謝謝
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則