新增glide加载图片,优化图片加载页面

yt_mpremote
liuguijing 2 months ago
parent 7e45f59973
commit bdb4726f55

@ -84,6 +84,7 @@ dependencies {
implementation "androidx.media3:media3-datasource-rtmp:1.1.1"
implementation "androidx.media3:media3-exoplayer-rtsp:1.1.1"
implementation 'com.github.bumptech.glide:glide:4.16.0'
// https://mvnrepository.com/artifact/dev.mobile/dadb
implementation 'dev.mobile:dadb:1.2.8'
implementation files('libs/common-release.aar')

@ -19,3 +19,5 @@
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
-keep class com.bumptech.glide.** { *; }
-dontwarn com.bumptech.glide.**

@ -143,6 +143,7 @@ public class DeviceActivity extends AppCompatActivity implements View.OnClickLis
localphotofilePath = getFilesDir() + "/Photos/";
localAppPath = localtmpfilePath + "App.json";
FileUtils.createDir(localtmpfilePath);
FileUtils.createDir(localphotofilePath);
}
@ -350,38 +351,39 @@ public class DeviceActivity extends AppCompatActivity implements View.OnClickLis
} catch (Exception ex) {
}
}
File localDevicePath = new File(getFilesDir(), "Photos" + File.separator + mSerialNo);
if (!localDevicePath.exists()) {
localDevicePath.mkdirs();
}
final File localFilePath = new File(localDevicePath, localFileName);
for (int idx = 0; idx < 10; idx++) {
boolean res = pullFile(adb, remoteFilePath, localFilePath);
if (res) {
mHandler.sendMessage(mHandler.obtainMessage(4));
mHandler.post(new Runnable() {
@Override
public void run() {
if (photoOrVideo) {
showPhoto(localFilePath.getAbsolutePath());
} else {
showVideo(localFilePath.getAbsolutePath());
//按照装置编号创建文件夹
String dirPath = localphotofilePath + mSerialNo;
boolean dir = FileUtils.createDir(dirPath);
if (dir) {
File file = FileUtils.getFile(dirPath + File.separator + localFileName);
for (int idx = 0; idx < 10; idx++) {
boolean res = pullFile(adb, remoteFilePath, file);
if (res) {
mHandler.sendMessage(mHandler.obtainMessage(4));
mHandler.post(new Runnable() {
@Override
public void run() {
if (photoOrVideo) {
showPhoto(file.getAbsolutePath(),dirPath);
} else {
showVideo(file.getAbsolutePath());
}
}
}
});
break;
});
break;
}
try {
Thread.sleep(1000);
} catch (Exception ex) {
ex.printStackTrace();
}
}
try {
Thread.sleep(1000);
// mAdb.shell("rm " + remoteFilePath);
} catch (Exception ex) {
ex.printStackTrace();
}
}
try {
// mAdb.shell("rm " + remoteFilePath);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
} catch (Exception ex) {
@ -501,10 +503,11 @@ public class DeviceActivity extends AppCompatActivity implements View.OnClickLis
*/
}
private void showPhoto(final String filePath) {
private void showPhoto(final String filePath,String path) {
Intent intent = new Intent(this, ImageActivity.class);
intent.putExtra("path", filePath);
intent.putExtra("cmdid", TextUtils.isEmpty(mAppConfig.cmdid) ? "" : mAppConfig.cmdid);
intent.putExtra("photodirpath", path);
// intent.putExtra("info", info);
startActivity(intent);
}

@ -41,6 +41,7 @@ import android.widget.TextView;
import com.xypower.mpremote.adapter.ImageItemAdapter;
import com.xypower.mpremote.adapter.ItemAdapter;
import com.xypower.mpremote.databinding.ActivityImageBinding;
import com.xypower.mpremote.utils.FileUtils;
import java.io.File;
import java.util.ArrayList;
@ -49,22 +50,12 @@ import java.util.List;
import java.util.Map;
public class ImageActivity extends AppCompatActivity implements View.OnClickListener {
private static final boolean AUTO_HIDE = true;
private static final int AUTO_HIDE_DELAY_MILLIS = 3000;
private static final int UI_ANIMATION_DELAY = 300;
private final Handler mHideHandler = new Handler(Looper.myLooper());
private String mSerialNo = "";
private List<File> mImageFiles = new ArrayList<>();
private ImageItemAdapter mAdapter;
private List<Map<String, Object>> mItems = new ArrayList<Map<String, Object>>();
private ActivityImageBinding binding;
private String path;//图片地址
private String cmdid;
private String photodirpath;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -80,16 +71,17 @@ public class ImageActivity extends AppCompatActivity implements View.OnClickList
private void initIntent() {
Intent intent = getIntent();
path = intent.getStringExtra("path");
photodirpath = intent.getStringExtra("photodirpath");
cmdid = intent.getStringExtra("cmdid");
}
private void initView() {
binding.imageView.setImageDrawable(null);
binding.imageView.setClickable(true);
binding.imageView.setOnClickListener(this);
if (path != null) {
loadImage(path);
}
// binding.imageView.setImageDrawable(null);
// binding.imageView.setClickable(true);
// binding.imageView.setOnClickListener(this);
// if (path != null) {
// loadImage(path);
// }
mAdapter = new ImageItemAdapter();
// mAdapter.setOnClickListener(this);
binding.recyclerView.setAdapter(mAdapter);
@ -99,13 +91,16 @@ public class ImageActivity extends AppCompatActivity implements View.OnClickList
private void initData() {
// 在Activity或Fragment中
getPicList();
// getPicList();
List<String> imageFiles = FileUtils.getImageFiles(photodirpath);
mAdapter.setItemList(imageFiles);
}
//获取文件夹下所有的照片
private void getPicList() {
// 获取应用专属目录的相对路径
String relativePath = Environment.DIRECTORY_PICTURES + "/" + Constants.PACKAGE_NAME_MPREMOTE + "/";
// String relativePath = Environment.DIRECTORY_PICTURES + "/" + Constants.PACKAGE_NAME_MPREMOTE + "/";
String relativePath = photodirpath;
String[] projection = {
MediaStore.Images.Media._ID,
@ -145,21 +140,21 @@ public class ImageActivity extends AppCompatActivity implements View.OnClickList
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
binding.imageView.requestLayout();
// binding.imageView.requestLayout();
}
private boolean loadImage(String path) {
binding.imageView.setImageDrawable(null);
File file = new File(path);
if (!file.exists()) {
return false;
}
Drawable drawable = loadDrawable(path);
if (drawable != null) {
binding.imageView.setImageDrawable(drawable);
}
return drawable != null;
}
// private boolean loadImage(String path) {
// binding.imageView.setImageDrawable(null);
// File file = new File(path);
// if (!file.exists()) {
// return false;
// }
// Drawable drawable = loadDrawable(path);
// if (drawable != null) {
// binding.imageView.setImageDrawable(drawable);
// }
// return drawable != null;
// }
private Drawable loadDrawable(String file) {
if (file == null || file.isEmpty()) {
@ -187,7 +182,7 @@ public class ImageActivity extends AppCompatActivity implements View.OnClickList
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.imageView:
case R.id.back:
finish();
break;
}

@ -9,6 +9,7 @@ import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.xypower.mpremote.R;
import com.xypower.mpremote.interfaces.OnImageItemClickListener;
import com.xypower.mpremote.utils.ImageUtils;
@ -54,7 +55,8 @@ public class ImageItemAdapter extends RecyclerView.Adapter<ImageItemAdapter.MyVi
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, @SuppressLint("RecyclerView") int position) {
String s = itemList.get(position);
holder.getImage().setImageDrawable(ImageUtils.loadDrawable(s));
Glide.with(holder.itemView.getContext()).load(s).into(holder.getImage());
// holder.getImage().setImageDrawable(ImageUtils.loadDrawable(s));
holder.itemView.setOnClickListener(v -> {
if (listener != null) {
listener.onItemClick(v, position);

@ -10,6 +10,8 @@ import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.List;
public class FileUtils {
@ -31,6 +33,44 @@ public class FileUtils {
// return file.getAbsolutePath();
// }
/**
*
* @param dirPath /sdcard/Pictures
* @return List<File>
*/
public static List<String> getImageFiles(String dirPath) {
List<String> imageFiles = new ArrayList<>();
File dir = new File(dirPath);
// 检查文件夹是否存在
if (!dir.exists() || !dir.isDirectory()) {
Log.e("Error", "文件夹不存在或不是目录: " + dirPath);
return imageFiles;
}
// 遍历文件夹中的文件
File[] files = dir.listFiles();
if (files == null) return imageFiles;
// 筛选图片文件(按扩展名)
for (File file : files) {
if (file.isFile() && isImageFile(file)) {
imageFiles.add(file.getAbsolutePath());
}
}
return imageFiles;
}
/**
*
*/
private static boolean isImageFile(File file) {
String name = file.getName().toLowerCase();
return name.endsWith(".jpg") || name.endsWith(".jpeg")
|| name.endsWith(".png") || name.endsWith(".webp")
|| name.endsWith(".gif") || name.endsWith(".bmp");
}
/**
*
*/

@ -7,22 +7,24 @@
android:layout_height="match_parent"
tools:context=".ImageActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitStart"
<include
android:id="@+id/toolbar"
layout="@layout/toolbar"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@android:drawable/screen_background_light_transparent" />
app:layout_constraintTop_toTopOf="parent" />
<!-- <ImageView-->
<!-- android:id="@+id/imageView"-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:scaleType="fitStart"-->
<!-- app:layout_constraintStart_toStartOf="parent"-->
<!-- app:layout_constraintTop_toTopOf="parent"-->
<!-- app:srcCompat="@android:drawable/screen_background_light_transparent" />-->
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView" />
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/toolbar" />
</androidx.constraintlayout.widget.ConstraintLayout>

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="8dp"
android:paddingTop="20dp"

Loading…
Cancel
Save