android获取网络图片
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
資源描述:
關(guān)于android獲取網(wǎng)絡(luò)圖片主要是把網(wǎng)絡(luò)圖片的數(shù)據(jù)流讀入到內(nèi)存中然后用
1.Bitmap bitMap = BitmapFactory.decodeByteArray(data, 0, length);?
方法來將圖片流傳化為bitmap類型 這樣才能用到
1.imageView.setImageBitmap(bitMap);?
來進(jìn)行轉(zhuǎn)化
在獲取bitmap時(shí)候出現(xiàn)null?
錯(cuò)誤代碼:
byte[] data = GetImageForNet.getImage(path);?
int length = data.length;?
Bitmap bitMap = BitmapFactory.decodeByteArray(data, 0, length);?
imageView.setImageBitmap(bitMap);?
下面是 GetImageForNet.getImage()方法的代碼清單
public static byte[] getImage(String path) throws Exception {?
URL url = new URL(path);?
HttpURLConnection httpURLconnection = (HttpURLConnection)url.openConnection();?
httpURLconnection.setRequestMethod("GET");?
httpURLconnection.setReadTimeout(6*1000);?
InputStream in = null;?
byte[] b = new byte[1024];?
int len = -1;?
if (httpURLconnection.getResponseCode() == 200) {?
in = httpURLconnection.getInputStream();?
in.read(b);?
in.close();?
return b;?
}?
return null;?
}?
看起來沒有問題 獲取網(wǎng)絡(luò)圖片輸入流,填充二進(jìn)制數(shù)組,返回二進(jìn)制數(shù)組,然后使用 Bitmap bitMap = BitmapFactory.decodeByteArray(data, 0, length); data就是返回的二進(jìn)制數(shù)組
獲取bitMap 看起來沒有問題,可是bitMap就是為null!
BitmapFactory.decodeByteArray方法中所需要的data不一定是傳統(tǒng)意義上的字節(jié)數(shù)組,查看android api,最后發(fā)現(xiàn)BitmapFactory.decodeByteArray所需要的data字節(jié)數(shù)組并不是想象中的數(shù)組!而是把輸入流傳化為字節(jié)內(nèi)存輸出流的字節(jié)數(shù)組格式
正確代碼:
try {?
byte[] data = GetImageForNet.getImage(path);?
String d = new String(data);?
// File file = new File("1.jpg");?
//OutputStream out = new FileOutputStream(file);?
//out.write(data);?
//out.close();?
int length = data.length;?
Bitmap bitMap = BitmapFactory.decodeByteArray(data, 0, length);?
imageView.setImageBitmap(bitMap);?
//imageView.seti?
} catch (Exception e) {?
Log.i(TAG, e.toString());?
Toast.makeText(DataActivity.this, "獲取圖片失敗", 1).show();?
}?
下面是改進(jìn)后的 GetImageForNet.getImage()方法的代碼清單
public static byte[] getImage(String path) throws Exception {?
URL url = new URL(path);?
HttpURLConnection httpURLconnection = (HttpURLConnection)url.openConnection();?
httpURLconnection.setRequestMethod("GET");?
httpURLconnection.setReadTimeout(6*1000);?
InputStream in = null;?
byte[] b = new byte[1024];?
int len = -1;?
if (httpURLconnection.getResponseCode() == 200) {?
in = httpURLconnection.getInputStream();?
byte[] result = readStream(in);?
in.close();?
return result;?
}?
return null;?
}?
public static byte[] readStream(InputStream in) throws Exception{?
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();?
byte[] buffer = new byte[1024];?
int len = -1;?
while((len = in.read(buffer)) != -1) {?
outputStream.write(buffer, 0, len);?
}?
outputStream.close();?
in.close();?
return outputStream.toByteArray();?
}
-------------------------------------------------------------------------------------------------------------------
android寫入數(shù)據(jù)庫、讀取sqlite中的圖片
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class AndroidTestActivity extends Activity {
??? /** Called when the activity is first created. */
?
?private Button btn;
?
?private SQLiteDatabase db = null;
?
?private ImageView imageView;
?
??? @Override
??? public void onCreate(Bundle savedInstanceState) {
??????? super.onCreate(savedInstanceState);
??????? setContentView(R.layout.main);
????????
??????? /**
???????? * getWritableDatabase()和getReadableDatabase()方法都可以獲取一個(gè)用于操作數(shù)據(jù)庫的SQLiteDatabase實(shí)例。
???????? * 但getWritableDatabase() 方法以讀寫方式打開數(shù)據(jù)庫,一旦數(shù)據(jù)庫的磁盤空間滿了,數(shù)據(jù)庫就只能讀而不能寫,
???????? * 倘若使用getWritableDatabase()打開數(shù)據(jù)庫就會出錯(cuò)。getReadableDatabase()方法先以讀寫方式打開數(shù)據(jù)庫,
???????? * 如果數(shù)據(jù)庫的磁盤空間滿了,就會打開失敗,當(dāng)打開失敗后會繼續(xù)嘗試以只讀方式打開數(shù)據(jù)庫。
???????? */
??????? DBHelper helper = new DBHelper(AndroidTestActivity.this, "mysql1.txt");
??db = helper.getWritableDatabase();
??
??????? imageView=(ImageView)findViewById(R.id.imgView);
??????? btn=(Button)findViewById(R.id.button1);
??????? btn.setOnClickListener(
??????? ??
??????? ??new Button.OnClickListener()
??????? ??{
??????? ???public void onClick(View v)
??????? ???{
??????? ????String fileName="mysql.db";
??????? ?????? AssetManager assets = getAssets();
??????? ?????? try {
??????? ?????? ?InputStream is=assets.open(fileName);
??????? ?????? ?
??????? ?????? ?Log.v("is.length", ?is.available()+"");
??????? ?????? ?
??????? ?????? ?FileOutputStream fos=new FileOutputStream(Environment.getDataDirectory()+ "/data/com.xujie.test/databases/" + "mysql1.txt");
??????? ?????? ?
??????? ?????? ?byte[]bytes=getInput(is);
??????? ?????? ?
//??????? ?????? ?int b=0;
??????? ?????? ?
//??????? ?????? ?while((b=is.read())!=-1)
//??????? ?????? ?{
//??????? ?????? ??fos.write(b);
//??????? ?????? ?}
??????? ?????? ?fos.write(bytes);
????????/**
???????? * 將數(shù)據(jù)流關(guān)閉
???????? */
??????? ?????? ?
??????? ?????fos.flush();
??? ??????fos.close();????????
??? ??????is.close();
??? ??????
??????? ?????? } catch (IOException e) {
???????// TODO Auto-generated catch block
???????e.printStackTrace();
??????}
??????? ???????
??????? ??? Cursor cursor= get_equipment_by_id("3");
??????? ????
??????? ??? while(cursor.moveToNext())
??????? ??? {
??????? ???? Bitmap bitmap=getIconFromCursor(cursor, cursor.getColumnIndex("icon"));
??????????? ??? Drawable drawable=new BitmapDrawable(bitmap);
??????????? ????
??????????? ??? imageView.setImageDrawable(drawable);
??????? ??? }
??????? ???}
??????? ??}
??????? );
??? }
????
????
??? public Bitmap getIconFromCursor(Cursor c, int iconIndex) {?
??? ?byte[] data = c.getBlob(iconIndex);?
??? ?try {
??? ??Log.v("BitmapFactory.decodeByteArray.length000:", "BitmapFactory.decodeByteArray.length");
??? ??Log.v("BitmapFactory.decodeByteArray.length:", BitmapFactory.decodeByteArray(data, 0, data.length).getWidth()+"");
??? ??Log.v("BitmapFactory.decodeByteArray.length111:", "BitmapFactory.decodeByteArray.length");
??? ??
??? ?return BitmapFactory.decodeByteArray(data, 0, data.length);?
??? ?} catch (Exception e) {?
??? ?return null;?
??? ?}
??? }
??? public byte[] getInput(InputStream is) throws IOException
?{
??ByteArrayOutputStream baos = new ByteArrayOutputStream();
??byte[] b = new byte[1024];
??int len = 0;
??while ((len = is.read(b, 0, 1024)) != -1)?
??{
???baos.write(b, 0, len);
???baos.flush();
??}
??return baos.toByteArray();
?}
????
?/**
? * @name ???get_equipment_by_id
? * @desc???? 設(shè)備列表
? * @author ??liwang
? * @date??? 2011-12-28
? * @param???String type 設(shè)備類型
? * @return?? Cursor
? */
?public Cursor get_equipment_by_id(String id)
?{
??return db.query("equipments", null, "id=?", new String[]{id} ,null, null, null);
?}
??? class DBHelper extends SQLiteOpenHelper
?{
??public DBHelper(Context context, String name, CursorFactory factory, int version)
??{
???super(context, name, factory, version);
???// TODO Auto-generated constructor stub
??}
??public DBHelper(Context context, String name)
??{
???super(context, name, null, 1);
??}
??@Override
??public void onCreate(SQLiteDatabase db)
??{
???// TODO Auto-generated method stub
??}
??@Override
??public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
??{
???// TODO Auto-generated method stub
??}
?}
}
轉(zhuǎn)載于:https://my.oschina.net/u/1423612/blog/291707
總結(jié)
以上是生活随笔為你收集整理的android获取网络图片的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [Project Euler]加入欧拉
- 下一篇: c php乱码,Cknife的PHP功能