将从LeanCloud下载的图片文件用ImageView显示
程序员文章站
2022-06-21 20:10:33
1.相关方法1.1 选择图片 private void startChooser() { // 启动图片选择器 imagePicker.startChooser(this, new ImagePicker.Callback() { // 选择图片回调 @Override public void onPickImage(Uri imageUri) { } // 裁剪图片回调...
1.相关方法
1.1 选择图片
private void startChooser() {
// 启动图片选择器
imagePicker.startChooser(this, new ImagePicker.Callback() {
// 选择图片回调
@Override public void onPickImage(Uri imageUri) {
}
// 裁剪图片回调
@Override public void onCropImage(Uri imageUri) {
String uri = imageUri.toString();
Log.d("Uri", imageUri.toString());
Uri u2 = Uri.parse(uri);
// draweeView.setImageURI(imageUri);
draweeView.setImageURI(u2);
draweeView.getHierarchy().setRoundingParams(RoundingParams.asCircle());
// 图片上传
upload(imageUri);
}
// 自定义裁剪配置
@Override public void cropConfig(CropImage.ActivityBuilder builder) {
builder
// 是否启动多点触摸
.setMultiTouchEnabled(false)
// 设置网格显示模式
.setGuidelines(CropImageView.Guidelines.OFF)
// 圆形/矩形
.setCropShape(CropImageView.CropShape.RECTANGLE)
// 调整裁剪后的图片最终大小
.setRequestedSize(960, 540)
// 宽高比
.setAspectRatio(16, 9);
}
// 用户拒绝授权回调
@Override public void onPermissionDenied(int requestCode, String[] permissions,
int[] grantResults) {
}
});
}
1.1 上传图片
/**
* 图片上传
*/
private void upload(@NotNull Uri uri){
String path = URI.create(uri.toString()).getPath();
try {
AVFile file = AVFile.withAbsoluteLocalPath("avatar.jpg", path);
file.saveInBackground().subscribe(new Observer<AVFile>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(AVFile avFile) {
Log.d("avFile and objectId:", avFile.getObjectId());
AVUser user = AVUser.getCurrentUser();
user.put("headImage", avFile);
user.save();
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
1.2 将字节数组转换成ImageView可调用的Bitmap对象
/**
* 将字节数组转换成ImageView可调用的Bitmap对象
* @param bytes 字节数组
* @param opts 设置你换成bitmap的属性(大小 宽 高 编码格式 预览 等),为了防止图片过大可以只获取预览
* @return
*/
public Bitmap getPicFromBytes(byte[] bytes, BitmapFactory.Options opts) {
if (bytes != null)
if (opts != null)
return BitmapFactory.decodeByteArray(bytes, 0, bytes.length,
opts);
else
return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
return null;
}
1.3 图片缩放
public Bitmap zoomBitmap(Bitmap bitmap, int w, int h){
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Matrix matrix = new Matrix();
float scaleWidth = ((float) w / width);
float scaleHeight = ((float) h / height);
matrix.postScale(scaleWidth, scaleHeight);
Bitmap newBmp = Bitmap.createBitmap(bitmap, 0, 0, width, height,
matrix, true);
return newBmp;
}
2.onCreate()方法中活动中各组件初始化和该活动实现的方法
private ImagePicker imagePicker = new ImagePicker();
private SimpleDraweeView draweeView;
private Button button;
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_file);
// 设置标题
imagePicker.setTitle("设置头像");
// 设置是否裁剪图片
imagePicker.setCropImage(true);
draweeView = findViewById(R.id.draweeView);
imageView = findViewById(R.id.image);
// 测试Uri
String uri = "file:///data/data/com.example.imagepickerapply/cache/cropped994058138.jpg";
File file = new File(URI.create(uri).getPath());
if (file.exists()){
// 判断图片是否还存在, 存在的话将头像进行设置
Log.d("urifile", "存在");
Uri u2 = Uri.parse(uri);
draweeView.setImageURI(u2);
draweeView.getHierarchy().setRoundingParams(RoundingParams.asCircle());
}else{
Log.d("urifile", "不存在");
}
draweeView.setOnClickListener(this);
button = findViewById(R.id.button_leancloud);
button.setOnClickListener(this);
}
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
imagePicker.onActivityResult(this, requestCode, resultCode, data);
}
@Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
imagePicker.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
}
3.点击事件(从LeanCloud下载图片文件并显示)
@Override public void onClick(View v) {
if (v.getId() == R.id.draweeView) {
startChooser();
}else if (v.getId() == R.id.button_leancloud){
// 下载图片文件 将字节数组转换成Bitmap对象
AVUser user = AVUser.getCurrentUser();
AVFile file = user.getAVFile("headImage");
file.getDataInBackground().subscribe(new Observer<byte[]>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(byte[] bytes) {
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = 2;
// 将opts传入, 获取的bitmap 就是原图的1/4大小
Bitmap bitmap = getPicFromBytes(bytes, opts);
Bitmap bitmap = getPicFromBytes(bytes,null);
imageView.setImageBitmap(bitmap);
draweeView.setImageBitmap(bitmap);
draweeView.getHierarchy().setRoundingParams(RoundingParams.asCircle());
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
}
}
4. 布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:fresco="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".FileActivity"
>
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/draweeView"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_centerInParent="true"
fresco:placeholderImage="@mipmap/click" />
<Button
android:id="@+id/button_leancloud"
android:layout_marginTop="20dp"
android:layout_below="@+id/draweeView"
android:layout_alignLeft="@+id/draweeView"
android:text="button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/image"
android:layout_width="400px"
android:layout_height="400px"
android:layout_below="@+id/button_leancloud"
android:background="@drawable/ic_launcher_background"/>
</RelativeLayout>
5. 运行结果
事件点击前:
事件点击后:
本文地址:https://blog.csdn.net/weixin_44086832/article/details/107434509
推荐阅读
-
以Spring Boot的方式显示图片或下载文件到浏览器
-
将从LeanCloud下载的图片文件用ImageView显示
-
例子:用PHP实现上载任何类型的文件并可以直接显示或下载下来_PHP
-
例子:用PHP实现上载任何类型的文件并可以直接显示或下载下来_PHP
-
用php实现的下载css文件中的图片的代码
-
用php实现的下载css文件中的图片的代码
-
下载photoshop软件 用php实现的下载css文件中的图片的代码
-
下载photoshop软件 用php实现的下载css文件中的图片的代码
-
用php实现的下载css文件中的图片的代码_PHP教程
-
用php实现的下载css文件中的图片的代码_PHP教程