標籤:android http io os java ar 檔案 資料 sp
思路:通過httpConnection擷取檔案流,將檔案流轉為位元組數組,將數組裝換為位元影像付給imageView
資訊清單檔中加入<uses-permission android:name="android.permission.INTERNET"/>
imageServise.java
package com.example.seepicture.servise;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class imageServise {
/**
* 擷取網狀圖片的資料
* @return
* @throws IOException
*/
public static byte[] getImg(String path) throws IOException{
URL url=new URL(path);
InputStream inStreams = null;
HttpURLConnection connection=(HttpURLConnection) url.openConnection();//得到基於http協議的連結化物件
connection.setConnectTimeout(5000);
//設定get請求方式
connection.setRequestMethod("GET");
if(connection.getResponseCode()==200){
inStreams=connection.getInputStream();
return streamTools.streamToByte(inStreams);
}
return null;
}
}
sreamTools.java
package com.example.seepicture.servise;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class streamTools {
/**
* 將流中的資料讀取到位元組數組中
* @param inStream
* @return
* @throws IOException
*/
public static byte[] streamToByte(InputStream inStream) throws IOException{
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while( (len = inStream.read(buffer)) != -1){
outStream.write(buffer, 0, len);
}
inStream.close();
return outStream.toByteArray();
}
}
MainActivity.java
package com.example.seepicture;
import java.io.IOException;
import java.io.InputStream;
import java.security.PublicKey;
import com.example.seepicture.servise.imageServise;
import android.os.Bundle;
import android.os.Looper;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText txt_path;
private Button btn_see;
private ImageView imgView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt_path=(EditText) findViewById(R.id.txt_path);
btn_see=(Button) findViewById(R.id.btn_see);
imgView=(ImageView) findViewById(R.id.img);
btn_see.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
addImg();
}
});
}
/**
* 按鈕點擊事件
* @param view
*/
private void addImg(){
String path=txt_path.getText().toString();
byte[] data = null;
try {
if(path.equals("")){
data = imageServise.getImg("http://pica.nipic.com/2008-06-13/2008613145022997_2.jpg");
}else{
data = imageServise.getImg(path);
}
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
imgView.setImageBitmap(bitmap);//顯示圖片
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "資料請求錯誤,圖片路徑不正確", 1).show();
}
}
}
布局檔案
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="網狀圖片路徑"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/txt_path"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/btn_see"
android:onClick="addImg"
android:text="擷取圖片"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/img"
/>
</LinearLayout>
查看網狀圖片