网络图片查看器

功能比较简单,代码也有解释,直接上代码:

Strings.xml代码

<?xml version="1.0" encoding="utf-8"?>

<resources>

    <string name="app_name">netimage</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">网络图片查看器</string>
    <string name="button">查看</string>
    <string name="error">获取图片失败</string>

</resources>


main.xml布局代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >


    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
<EditText
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:id="@+id/imagepaht"
   android:text="http://hao.360.cn"/>
<Button
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:id="@+id/button"
   android:text="@string/button"/>
<ImageView
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:id="@+id/imageview"/>

</LinearLayout>


MainActivity.java代码

public class MainActivity extends Activity {
   
private EditText pathtext;
private ImageView imageview;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        pathtext = (EditText)findViewById(R.id.imagepaht);
        imageview = (ImageView)findViewById(R.id.imageview);
        Button button = (Button) this.findViewById(R.id.button);
        button.setOnClickListener(new ButtonClickListener());
    }
    private final class ButtonClickListener implements View.OnClickListener{


@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//得到图片路径
String path =pathtext.getText().toString();
try {
//得到图片数据
byte[] data= ImageService.getImage(path);
data = ImageService.getImage(path);
//加载图片
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
imageview.setImageBitmap(bitmap);//显示图片
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(getBaseContext(), R.string.error, Toast.LENGTH_SHORT).show();
}

}
   
    }
    
}


StreamTool.java代码

public class StreamTool {
/*
* 读取流中的数据
*/


public static byte[] read(InputStream inStream) throws Exception {
// TODO Auto-generated method stub
ByteArrayOutputStream outStream = new ByteArrayOutputStream();

//new一个1024缓冲区
byte[] buffer = new byte[1024];
int len = 0;

//读取数据直到读完
while((len = inStream.read(buffer))!=-1){
outStream.write(buffer,0,len);
}
//关闭输出流
inStream.close();

return outStream.toByteArray();
}

}


ImageService.java代码

public class ImageService {
/*
 * 获取网络图片的数据
 * @param path 网络图片路径
 * @return
 * 
 * */
public static byte[] getImage(String path) throws Exception {
// TODO Auto-generated method stub
URL url = new URL(path);
HttpURLConnection conn =(HttpURLConnection) url.openConnection();//基于http的链接对象
conn.setConnectTimeout(5000);//链接时间
//请求方式
conn.setRequestMethod("GET");
if(conn.getResponseCode()==200)//响应码
{
//获取返回数据
InputStream inStream = conn.getInputStream();
return StreamTool.read(inStream);
}
return null;
}


}


你可能感兴趣的:(android)