您的位置:首页 > 移动开发 > Android开发

Android 从Internet获取数据 实现获取一张图片

2011-01-20 16:59 691 查看
l

从Internet获取数据

利用HttpURLConnection对象,我们可以从网络中获取文件数据.



URL url = new URL("http://photocdn.sohu.com/20100125/Img269812337.jpg");



HttpURLConnection conn = (HttpURLConnection) url.openConnection();



conn.setConnectTimeout(5* 1000);



conn.setRequestMethod("GET");



if (conn.getResponseCode() != 200) throw new RuntimeException("请求url失败");



InputStream is = conn.getInputStream();



readAsFile(is, "Img269812337.jpg");



public static void readAsFile(InputStream inSream, File file) throws Exception{




FileOutputStream outStream = new FileOutputStream(file);




byte[] buffer = new byte[1024];




int len = -1;




while( (len = inSream.read(buffer)) != -1 ){




outStream.write(buffer, 0, len);




}




outStream.close();




inSream.close();



}



<?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"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/path"
android:text="http://himg2.huanqiu.com/attachment/080619/435d6bd22f.jpg"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查看这张图片"
android:id="@+id/button"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
/>
</LinearLayout>




<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">图片查看器</string>
<string name="hello">网络图片路径</string>
<string name="fail">获取图片失败!</string>
<string name="imagefail">网络链接失败!</string>

</resources>




package com.zyq.image;
import com.zyq.service.ImageService;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity
{
private EditText pathText;
private ImageView imageView;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

pathText=(EditText)this.findViewById(R.id.path);
imageView=(ImageView)this.findViewById(R.id.imageView);
Button button=(Button)this.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener()
{

@Override
public void onClick(View v)
{
String path=pathText.getText().toString().trim();
try
{
byte[] data=ImageService.getImage(path);
if(data!=null)
{
Bitmap bitmap=BitmapFactory.decodeByteArray(data, 0, data.length);//构造一张位图
imageView.setImageBitmap(bitmap);//完成图片的显示
}
else
{
Toast.makeText(MainActivity.this, R.string.fail, 1).show();
}
} catch (Exception e)
{
Toast.makeText(MainActivity.this, R.string.imagefail, 1).show();
e.printStackTrace();
}
}
});
}
}




package com.zyq.service;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class ImageService
{
/**
* 读取数据 获取网络图片
* @param path 网络图片路径
* @return
* @throws Exception
*/
public static byte[] getImage(String path) throws Exception
{

URL url=new URL(path);
HttpURLConnection urlConnection=(HttpURLConnection)url.openConnection();
urlConnection.setConnectTimeout(5*1000);
urlConnection.setRequestMethod("GET");
InputStream inputStream=urlConnection.getInputStream();

if(urlConnection.getResponseCode()==200)
{
ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
byte[] buffer=new byte[1024];
int len=0;
while((len=inputStream.read(buffer))!=-1)
{
outputStream.write(buffer,0,len);
}
inputStream.close();
outputStream.close();
return outputStream.toByteArray();
}
return null;
}
}




<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zyq.image"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity"
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>
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>






l
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: