《Android摄像头相册使用+上传照片至服务器(上篇)》这篇文章介绍如何将照片存储至阿里云的OSS服务,已经上传成功,在这里继续介绍,如何下载已经存储的照片并将其显示在Android中。
阿里云对象存储OSS服务的后台,上传的图片,会有相应的URL,这是这个图片的网络地址,所以我们就使用OKhttp3来请求网络图片并将其显示在UI界面中。
一、Android前端
1、布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/take_photo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="#E91E63"
android:text="Take Photo" />
<Button
android:id="@+id/choose_from_album"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="#E91E63"
android:text="Choose From Album" />
<Button
android:id="@+id/upload"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="#E91E63"
android:onClick="upload"
android:text="上传" />
<Button
android:id="@+id/button5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="#E91E63"
android:onClick="download"
android:text="下载图片" />
<ImageView
android:id="@+id/picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
</LinearLayout>
新增加下载button。
2、Avatarupload.java
public class Avatarupload extends AppCompatActivity {
public static final int TAKE_PHOTO = 1;
public static final int CHOOSE_PHOTO = 2;
private ImageView picture;
private Button takePhoto;
private Button chooseFromAlbum;
private Uri imageUri;
private String imagePath = null;
private static final int SUCCESS = 1;
private static final int FALL = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_avatarupload);
takePhoto = (Button) findViewById(R.id.take_photo);
chooseFromAlbum = (Button) findViewById(R.id.choose_from_album);
picture = (ImageView) findViewById(R.id.picture);
//这里不改动
....
}
private void openAlbum() {
//这里不改动
....
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
//这里不改动
....
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//这里不改动
....
}
@TargetApi(19)
private void handleImageOnKitKat(Intent data) {
//这里不改动
....
}
private void handleImageBeforeKitKat(Intent data) {
//这里不改动
....
}
private String getImagePath(Uri uri, String selection) {
//这里不改动
....
}
private void displayImage(String imagePath) {
//这里不改动
....
}
//TODO
public void upload(View view) {
//这里不改动
....
}
//主要是下面这些
public void download(View view) {
//1.创建一个okhttpclient对象
OkHttpClient okHttpClient = new OkHttpClient();
//2.创建Request.Builder对象,设置参数,请求方式如果是Get,就不用设置,默认就是Get
String url = "使用自己的网络照片地址";
Request request = new Request.Builder()
.url(url)
.build();
//3.创建一个Call对象,参数是request对象,发送请求
Call call = okHttpClient.newCall(request);
//4.异步请求,请求加入调度
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
//得到从网上获取资源,转换成我们想要的类型
byte[] Picture_bt = response.body().bytes();
//通过handler更新UI
Message message = handler.obtainMessage();
message.obj = Picture_bt;
message.what = SUCCESS;
handler.sendMessage(message);
}
});
}
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what){
//加载网络成功进行UI的更新,处理得到的图片资源
case SUCCESS:
//通过message,拿到字节数组
byte[] Picture = (byte[]) msg.obj;
//使用BitmapFactory工厂,把字节数组转化为bitmap
Bitmap bitmap = BitmapFactory.decodeByteArray(Picture, 0, Picture.length);
//通过imageview,设置图片为空
picture.setImageBitmap(null);
//睡眠3秒钟,更有利于看效果
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//在加载图片
picture.setImageBitmap(bitmap);
break;
//当加载网络失败执行的逻辑代码
case FALL:
Toast.makeText(Avatarupload.this, "网络出现了问题", Toast.LENGTH_SHORT).show();
break;
}
}
};
}
这样效果就出来了,比如这里的URL地址可以是从后端传过来的。不是固定的。可以自己去写后端代码。