2012-07-26 37 views
1

这是我的尝试。我的文件托管在我自己的服务器上,主要是JPG文件。我试图将它们下载到我的应用程序中。我无法将这些图像生成到我的应用程序中。我跟着导游从这个博客http://getablogger.blogspot.gr/2008/01/android-download-image-from-server-and.html从文件服务器下载随机图片

我的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="Hello World, HTTPImage load test" 
/> 
<Button android:id="@+id/get_imagebt" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="Get an image" 
android:layout_gravity="center" 
/> 
<ImageView android:id="@+id/imview" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="center" 
/> 
</LinearLayout> 

编码

package com.example.downloadimages; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import java.io.IOException; 
import java.io.InputStream; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.util.HashMap; 
import java.util.Map; 
import java.util.Random; 

import android.app.Activity; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ImageView; 




public class MainActivity extends Activity { 

    ImageView imView; 
    String imageUrl="http://myfilehosting.com/"; 
    Random r; 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     r= new Random(); 

     Button bt3= (Button)findViewById(R.id.get_imagebt); 
     bt3.setOnClickListener(getImgListener); 
     imView = (ImageView)findViewById(R.id.imview); 
    } 

    View.OnClickListener getImgListener = new View.OnClickListener() 
    { 

     public void onClick(View view) { 
     // TODO Auto-generated method stub 

     //i tried to randomize the file download, in my server i put 4 files with name like 
     //jpg0.jpg, jpg1.jpg, jpg2.jpg so different file is downloaded in button press 
     int i =r.nextInt()%4; 
     downloadFile(imageUrl+i+".jpg"); 

     } 

     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(); 
     int length = conn.getContentLength(); 
     int[] bitmapData =new int[length]; 
     byte[] bitmapData2 =new byte[length]; 
     InputStream is = conn.getInputStream(); 

     bmImg = BitmapFactory.decodeStream(is); 
     imView.setImageBitmap(bmImg); 
     } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     } 





}; 
}; 
} 
+0

究竟失败了吗? – 2012-07-26 05:59:59

+0

你需要使用'AsyncTask'来做到这一点。阅读[Android的文档](http://developer.android.com/reference/android/os/AsyncTask.html),并看到这[许多SO解决方案之一](http://stackoverflow.com/问题/ 3090650/Android的装载-的图像,来自该网络与-的AsyncTask)。 – 2012-07-26 06:05:35

回答

1

有几个问题,你的代码:

  • 您尝试进行网络操作(从远程服务器下载文件)从UI线程。 网络操作必须从另一个线程执行。否则 - 异常被抛出。

的从另一个线程启动代码的方法之一是:

new Thread(new Runnable() { 

     @Override 
     public void run() { 
      downloadFile(imageUrl+i+".jpg"); 
     } 
    }).start(); 

更好的办法是使用的AsyncTask或IntentService(其也执行上单独的线程的代码)

  • 第二件事 - 你不能只用输入流的大小来创建int数组:如果图像太大,可能会失败。尝试打破下载与固定大小的段(比如2058个字节每个段)例如:

    private HttpURLConnection conn; 
    private InputStream stream; 
    private FileOutputStream out; 
    
    private double fileSize; 
    private double downloaded; 
    
    
    public void downloadFile(String fileURL, String fileName) { 
    try { 
    
        conn = (HttpURLConnection) new URL(fileURL).openConnection(); 
        fileSize = conn.getContentLength(); 
        File file = new File(fileName); 
    
        out = new FileOutputStream(file); 
        conn.connect(); 
    
        stream = conn.getInputStream(); 
    
        while (status == DOWNLOADING) { 
         byte buffer[]; 
    
         if (fileSize - downloaded > MAX_BUFFER_SIZE) { 
          buffer = new byte[MAX_BUFFER_SIZE]; 
         } else { 
          buffer = new byte[(int) (fileSize - downloaded)]; 
         } 
         int read = stream.read(buffer); 
    
         if (read == -1) { 
    
          out.close(); 
          if (conn != null) { 
           conn.disconnect(); 
          } 
    
          break; 
         } 
    
         out.write(buffer, 0, read); 
         downloaded += read; 
        } 
    
    } catch (Exception e) { 
        Log.e("downloadFile():", e.getMessage()); 
        if (conn != null) { 
         conn.disconnect(); 
        } 
    } 
    
    } 
    
+0

谢谢Tal Kanel。您提到的上述代码只会从使用字符串(URL)写入的特定网址获取图片。我想列出一个广泛的网址,定期更新/更改,而不必每次我有一个新的图像修改代码。我用String imageUrl =“http://myfilehosting.com/”;所以我可以列出在我的Web服务器中以.jpg结尾的所有图像。请请帮助我。 – 2012-07-26 06:25:10

+0

我想要做的是,我想从我的网络服务器(URL是这样的http://myfilehosting.com/100.jpg)加载100 ++图像,而无需在我的字符串中指定每个图像的URL。 – 2012-07-26 07:23:38

+0

有人吗?请回复 – 2012-07-26 09:17:00