因为项目的需要,需要做一个拍照、相册获取图片,并且上传图片到网络上面的做法。其中,用到了
一、把本地图片路径解析显示到ImageView上;
1、
Bitmap bitmap = null;
bitmap = getLoacalBitmap(url);
imageView.setImageBitmap(bitmap);
/**
* 加载本地图片
* from Internet
* @param url
* @return
*/
public static Bitmap getLoacalBitmap(String url) {
try {
FileInputStream fis = new FileInputStream(url);
return BitmapFactory.decodeStream(fis);
} catch (FileNotFoundException e) {
e.printStackTrace();
returnnull;
}
}
此方法可以用下面的方法替换:
Bitmap bitmap = null;
bitmap = getLoacalBitmap(url);
imageView.setImageBitmap(bitmap);
/**
* 加载本地图片
* from Internet
* @param url
* @return
*/
public static Bitmap getLoacalBitmap(String url) {
try {
FileInputStream fis = new FileInputStream(url);
return BitmapFactory.decodeStream(fis);
} catch (FileNotFoundException e) {
e.printStackTrace();
returnnull;
}
}
2、
Bitmap bitmap = null;
bitmap = BitmapFactory.decodeFile(url);
imageView.setImageBitmap(bitmap);
二、把图片网络路径解析显示到ImageView上;
Bitmap bitmap = null;
bitmap = BitmapFactory.decodeFile(url);
imageView.setImageBitmap(bitmap);
方法1、
Bitmap bitmap = null;
new Thread(getHttpBitmapRunnable).start();
Runnable getHttpBitmapRunnable =new Runnable(){
@Override
public void run() {
byte[] data =null;
try {
data = getImage(logo);
} catch (Exception e) {
e.printStackTrace();
}
Message msg =new Message();
msg.what = 0;
msg.obj = bitmap;
getLogoHandler.sendMessage(msg);
}
};
Handler getLogoHandler = new Handler(){
public void handleMessage(android.os.Message msg) {
if(msg.what == 0){
iv_userphoto.setImageBitmap((Bitmap)(msg.obj));
}
}
};
public staticbyte[] getImage(String urlPath) throws Exception{
<span style="white-space:pre"> </span>URL url = new URL(urlPath);
<span style="white-space:pre"> </span>HttpURLConnection conn = (HttpURLConnection)url.openConnection();
<span style="white-space:pre"> </span>conn.setRequestMethod("GET");
<span style="white-space:pre"> </span>conn.setConnectTimeout(5*1000);
<span style="white-space:pre"> </span>InputStream inStream = conn.getInputStream();
<span style="white-space:pre"> </span>return readFromInput(inStream);
}
public staticbyte[] readFromInput(InputStream inStream)throws Exception{
<span style="white-space:pre"> </span>ByteArrayOutputStream outStream = new ByteArrayOutputStream();
<span style="white-space:pre"> </span>byte[] buffer = new byte[1024];
<span style="white-space:pre"> </span>int len = 0;
<span style="white-space:pre"> </span>while((len = inStream.read(buffer))!= -1){
<span style="white-space:pre"> </span>outStream.write(buffer,0,len);
<span style="white-space:pre"> </span>}
inStream.close();
return outStream.toByteArray();
}
方法2、
new Thread(getHttpBitmapRunnable).start();
public Bitmap getHttpBitmap(String data) {
Bitmap bitmap = null;
try
{
//初始化一个URL对象
URL url = new URL(data);
//获得HTTPConnection网络连接对象
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(5*1000); //设置连接主机超时(单位:毫秒)
connection.setReadTimeout(5*1000);//设置从主机读取数据超时(单位:毫秒)
connection.setDoInput(true);
connection.connect();
//得到输入流
InputStream is = connection.getInputStream();
Log.i("TAG", "*********inputstream**"+is);
bitmap = BitmapFactory.decodeStream(is);
Log.i("TAG", "*********bitmap****"+bitmap);
//关闭输入流
is.close();
//关闭连接
connection.disconnect();
} catch (Exception e)
{
e.printStackTrace();
}
return bitmap;
}
Runnable getHttpBitmapRunnable = new Runnable(){
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>public void run() {
<span style="white-space:pre"> </span>try {
<span style="white-space:pre"> </span>Bitmap bitmap = getHttpBitmap();
<span style="white-space:pre"> </span>}catch (Exception e) {
<span style="white-space:pre"> </span>e.printStackTrace();
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>Message msg = new Message();
<span style="white-space:pre"> </span>msg.what = 0;
<span style="white-space:pre"> </span>msg.obj =bitmap;
<span style="white-space:pre"> </span>getLogoHandler.sendMessage(msg);
<span style="white-space:pre"> </span>}
};
Handler getLogoHandler = new Handler(){
<span style="white-space:pre"> </span>public void handleMessage(android.os.Message msg) {
<span style="white-space:pre"> </span>if(msg.what == 0){
<span style="white-space:pre"> </span>imageView.setImageBitmap((Bitmap)(msg.obj));
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
};
new Thread(getHttpBitmapRunnable).start();
public Bitmap getHttpBitmap(String data) {
Bitmap bitmap = null;
try
{
//初始化一个URL对象
URL url = new URL(data);
//获得HTTPConnection网络连接对象
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(5*1000); //设置连接主机超时(单位:毫秒)
connection.setReadTimeout(5*1000);//设置从主机读取数据超时(单位:毫秒)
connection.setDoInput(true);
connection.connect();
//得到输入流
InputStream is = connection.getInputStream();
Log.i("TAG", "*********inputstream**"+is);
bitmap = BitmapFactory.decodeStream(is);
Log.i("TAG", "*********bitmap****"+bitmap);
//关闭输入流
is.close();
//关闭连接
connection.disconnect();
} catch (Exception e)
{
e.printStackTrace();
}
return bitmap;
}
Runnable getHttpBitmapRunnable = new Runnable(){
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>public void run() {
<span style="white-space:pre"> </span>try {
<span style="white-space:pre"> </span>Bitmap bitmap = getHttpBitmap();
<span style="white-space:pre"> </span>}catch (Exception e) {
<span style="white-space:pre"> </span>e.printStackTrace();
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>Message msg = new Message();
<span style="white-space:pre"> </span>msg.what = 0;
<span style="white-space:pre"> </span>msg.obj =bitmap;
<span style="white-space:pre"> </span>getLogoHandler.sendMessage(msg);
<span style="white-space:pre"> </span>}
};
Handler getLogoHandler = new Handler(){
<span style="white-space:pre"> </span>public void handleMessage(android.os.Message msg) {
<span style="white-space:pre"> </span>if(msg.what == 0){
<span style="white-space:pre"> </span>imageView.setImageBitmap((Bitmap)(msg.obj));
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
};
String base64 = (String)objMap.get("base64");
imageView.setImageBitmap(stringtoBitmap(base64));
public Bitmap stringtoBitmap(String string) {
// 将字符串转换成Bitmap类型
Bitmap bitmap = null;
try {
byte[] bitmapArray;
bitmapArray = Base64.decode(string, Base64.DEFAULT);
bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0,
bitmapArray.length);
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
String base64 = (String)objMap.get("base64");
imageView.setImageBitmap(stringtoBitmap(base64));
public Bitmap stringtoBitmap(String string) {
// 将字符串转换成Bitmap类型
Bitmap bitmap = null;
try {
byte[] bitmapArray;
bitmapArray = Base64.decode(string, Base64.DEFAULT);
bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0,
bitmapArray.length);
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}