Android LoaderManager AsyncTaskLoader加载全部图片RecyclerView BigImageView呈现,Java(1)
权限:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
引用:
implementation 'com.github.piasy:BigImageViewer:1.8.1'
implementation 'com.github.piasy:GlideImageLoader:1.8.1'
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.loader.app.LoaderManager;
import androidx.loader.content.AsyncTaskLoader;
import androidx.loader.content.Loader;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private static String TAG = "fly";
private LoaderManager loaderManager;
private PhotoAdapter photoAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = findViewById(R.id.recyclerview);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
photoAdapter= new PhotoAdapter(this);
recyclerView.setAdapter(photoAdapter);
setupLoader();
}
private static List<Photo> readImage(Context context) {
List<Photo> photos = new ArrayList<>();
//读取手机图片
Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
while (cursor.moveToNext()) {
//图片的路径
String path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
//图片名称
String name = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME));
//图片最后修改日期
//String date_modified = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATE_MODIFIED));
File file = new File(path);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String date = sdf.format(new Date(file.lastModified()));
//图片大小
long size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.SIZE));
Photo photo = new Photo(name, date, size, path);
photos.add(photo);
}
return photos;
}
private void setupLoader() {
loaderManager = LoaderManager.getInstance(this);
loaderManager.initLoader(2023, new Bundle(), loaderCallbacks);
}
private LoaderManager.LoaderCallbacks loaderCallbacks = new LoaderManager.LoaderCallbacks<List<Photo>>() {
@NonNull
@Override
public MyLoader onCreateLoader(int id, @Nullable Bundle args) {
Log.d(TAG, "onCreateLoader- id - " + id);
MyLoader loader = new MyLoader(getApplicationContext());
return loader;
}
@Override
public void onLoadFinished(@NonNull Loader<List<Photo>> loader, List<Photo> photos) {
Log.d(TAG, "onLoadFinished- " + loader.getId() + " - " + photos.size());
photoAdapter.dataChanged(photos);
}
@Override
public void onLoaderReset(@NonNull Loader loader) {
}
};
@Override
protected void onStart() {
super.onStart();
Log.d(TAG, "---");
Log.d(TAG, "onStart");
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume");
}
@Override
protected void onStop() {
super.onStop();
Log.d(TAG, "onStop");
}
private static class MyLoader extends AsyncTaskLoader<List<Photo>> {
private Context context;
public MyLoader(@NonNull Context context) {
super(context);
this.context = context;
}
@Override
protected void onStartLoading() {
super.onStartLoading();
Log.d(TAG, "onStartLoading- " + getId());
this.forceLoad();
}
//work thread
@Nullable
@Override
public List<Photo> loadInBackground() {
return readImage(context);
}
//UI main thread
@Override
public void deliverResult(@Nullable List<Photo> photos) {
super.deliverResult(photos);
Log.d(TAG, "deliverResult- " + getId() + " 图片总量:" + photos.size());
}
@Override
public void onContentChanged() {
super.onContentChanged();
Log.d(TAG, "onContentChanged- " + getId());
}
}
}
R.layout.activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
</androidx.recyclerview.widget.RecyclerView>
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import com.github.piasy.biv.BigImageViewer;
import com.github.piasy.biv.loader.glide.GlideImageLoader;
import com.github.piasy.biv.view.BigImageView;
import java.util.ArrayList;
import java.util.List;
public class PhotoAdapter extends RecyclerView.Adapter<PhotoAdapter.PhotoViewHolder> {
private Context context;
private List<Photo> photos = new ArrayList<>();
public PhotoAdapter(Context context) {
this.context = context;
BigImageViewer.initialize(GlideImageLoader.with(context));
}
public void dataChanged(List<Photo> photos) {
this.photos = photos;
notifyDataSetChanged();
}
@Override
public PhotoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.image_item, parent, false);
PhotoViewHolder holder = new PhotoViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(PhotoViewHolder holder, int pos) {
holder.textView.setText(photos.get(pos).name);
holder.imageView.showImage(getImageUri(context, photos.get(pos).path));
//Glide.with(context).load(new File(photos.get(pos).path)).into(holder.imageView);
}
@Override
public int getItemCount() {
return photos.size();
}
public class PhotoViewHolder extends RecyclerView.ViewHolder {
public BigImageView imageView;
public TextView textView;
public PhotoViewHolder(View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.image);
textView = itemView.findViewById(R.id.text);
}
}
public static Uri getImageUri(Context context, String filePath) {
Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
new String[]{MediaStore.Images.Media._ID}, MediaStore.Images.Media.DATA + "=? ",
new String[]{filePath}, null);
if (cursor != null && cursor.moveToFirst()) {
int id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns._ID));
Uri baseUri = Uri.parse("content://media/external/images/media");
return Uri.withAppendedPath(baseUri, "" + id);
} else {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DATA, filePath);
return context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
}
}
}
image_item.xml
<?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="wrap_content"
android:orientation="vertical">
<com.github.piasy.biv.view.BigImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="@android:color/darker_gray" />
</LinearLayout>
public class Photo {
public String name;//名称
public String date;//日期
public long size; //大小
public String path;//路径
public Photo(String name, String date, long size, String path) {
this.name = name;
this.date = date;
this.size = size;
this.path = path;
}
}
Android LoaderManager CursorLoader加载全部图片RecyclerView BigImageView呈现,Java(2)_zhangphil的博客-CSDN博客【Android设置头像,手机拍照或从本地相册选取图片作为头像】像微信、QQ、微博等社交类的APP,通常都有设置头像的功能,设置头像通常有两种方式:1,让用户通过选择本地相册之类的图片库中已有的图像,裁剪后作为头像。Android设置头像,手机拍照或从本地相册选取图片作为头像_android 头像拍照_zhangphil的博客-CSDN博客。Android图片添加文字水印并保存水印文字图片到指定文件_zhangphil的博客-CSDN博客。https://blog.csdn.net/zhangphil/article/details/129641492