查看網狀圖片

來源:互聯網
上載者:User

標籤: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>

 

查看網狀圖片

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.