android中怎么跳转到相册获取照片并得到url
1个回答
2016-08-23 · 知道合伙人软件行家
关注
展开全部
方法/步骤
1
如下图所示,需要根据URL地址获取图片加载到图中Anroid机器人所在的位置,这是运行前的效果:
2
首先需根据URL地址获取图片,如下所示,urladdr即为图片地址,返回Drawable对象:
//download image from network using @urladdress
private Drawable loadImageFromNetwork(String urladdr) {
// TODO Auto-generated method stub
Drawable drawable = null;
try{
//judge if has picture locate or not according to filename
drawable = Drawable.createFromStream(new URL(urladdr).openStream(), "image.jpg");
}catch(IOException e){
Log.d("test",e.getMessage());
}
if(drawable == null){
Log.d("test","null drawable");
}else{
Log.d("test","not null drawable");
}
return drawable;
}
3
获取到图片后,需要更新主线程UI资源,考虑到时间以及界面反应延迟等,所以采用线程加以处理,如下图所示:
// image
new Thread(new Runnable(){
Drawable drawable = loadImageFromNetwork(urladdress);
@Override
public void run(){
//post() is quite important,update pictures in UI main thread
image.post(new Runnable(){
@Override
public void run(){
//TODO Auto-generated method stub
image.setImageDrawable(drawable);
}
});
}
//download image from network using @urladdress
private Drawable loadImageFromNetwork(String urladdr) {
//... 略(如 1 中所示)
}
}).start(); //线程启动
4
说明:在上述示例代码中,image是ImageView类的一个对象,也就是APP中的一个显示图像组件,利用获取到的图片drawable去更新image,运行效果如下所示:
1
如下图所示,需要根据URL地址获取图片加载到图中Anroid机器人所在的位置,这是运行前的效果:
2
首先需根据URL地址获取图片,如下所示,urladdr即为图片地址,返回Drawable对象:
//download image from network using @urladdress
private Drawable loadImageFromNetwork(String urladdr) {
// TODO Auto-generated method stub
Drawable drawable = null;
try{
//judge if has picture locate or not according to filename
drawable = Drawable.createFromStream(new URL(urladdr).openStream(), "image.jpg");
}catch(IOException e){
Log.d("test",e.getMessage());
}
if(drawable == null){
Log.d("test","null drawable");
}else{
Log.d("test","not null drawable");
}
return drawable;
}
3
获取到图片后,需要更新主线程UI资源,考虑到时间以及界面反应延迟等,所以采用线程加以处理,如下图所示:
// image
new Thread(new Runnable(){
Drawable drawable = loadImageFromNetwork(urladdress);
@Override
public void run(){
//post() is quite important,update pictures in UI main thread
image.post(new Runnable(){
@Override
public void run(){
//TODO Auto-generated method stub
image.setImageDrawable(drawable);
}
});
}
//download image from network using @urladdress
private Drawable loadImageFromNetwork(String urladdr) {
//... 略(如 1 中所示)
}
}).start(); //线程启动
4
说明:在上述示例代码中,image是ImageView类的一个对象,也就是APP中的一个显示图像组件,利用获取到的图片drawable去更新image,运行效果如下所示:
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询