How can I display all images from a specific folder on android gallery like, for example, whatapp does. I`m using MediaScannerConnectionClient
File folder =
You can fetch image from specific folder on SDcard using my ways also delete the files.
MainActivity
FileAdapter fileAdapter;
RecyclerView recyclerView;
ArrayList filePath = new ArrayList<>();
ArrayList filename = new ArrayList<>();
private File[] listFile;
File file;
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
tv_empty = findViewById(R.id.tv_empty);
loadFiles();
fileAdapter = new FileAdapter(this, filePath, filename);
recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
recyclerView.setAdapter(fileAdapter);
fileAdapter.notifyDataSetChanged();
private void loadFiles() {
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
Toast.makeText(getActivity(), "Error! No SDCARD Found!", Toast.LENGTH_LONG).show();
} else {
file = new File(Environment.getExternalStorageDirectory().getPath() + "/StorySaver");
if (!file.exists()) {
file.mkdirs();
}
}
filePath.clear();
filename.clear();
if (file.isDirectory()) {
listFile = file.listFiles();
for (File absolutePath : listFile) {
filePath.add(absolutePath.getAbsolutePath());
filename.add(absolutePath.getName());
}
}
if (filePath.size() == 0) {
tv_empty.setVisibility(View.VISIBLE);
} else {
tv_empty.setVisibility(View.GONE);
}
}
MyAdapter
public class FileAdapter extends RecyclerView.Adapter {
ArrayList filepath;
ArrayList filename;
public Context mContext;
File file;
public class CustomViewHolder extends RecyclerView.ViewHolder {
TextView content;
ImageView imageView;
public CustomViewHolder(final View view) {
super(view);
this.content = (TextView) view.findViewById(R.id.content);
this.imageView = (ImageView) view.findViewById(R.id.image);
}
}
public FileAdapter(Context context, ArrayList filepath, ArrayList filename) {
this.filepath = filepath;
this.filename = filename;
this.mContext = context;
}
public CustomViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
return new CustomViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_file, null));
}
public void onBindViewHolder(CustomViewHolder customViewHolder, final int i) {
file = new File(filepath.get(i));
customViewHolder.content.setText(filename.get(i));
Glide.with(this.mContext).load(filepath.get(i)).into(customViewHolder.imageView);
customViewHolder.imageView.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View view) {
Builder builder = new Builder(mContext);
builder.setMessage("Are you sure you want delete this?");
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int v) {
if (file.exists()) {
file.delete();
}
filepath.remove(i);
notifyDataSetChanged();
}
});
builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});
builder.show();
return false;
}
});
customViewHolder.imageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
Uri data = FileProvider.getUriForFile(mContext, BuildConfig.APPLICATION_ID + ".provider", new File(filepath.get(i)));
intent.setDataAndType(data, "*/*");
try {
String substring = filename.get(i).substring(filename.get(i).lastIndexOf("."));
if (substring.equals(".jpg")) {
intent.setDataAndType(data, "image/*");
} else if (substring.equals(".mp4")) {
intent.setDataAndType(data, "video/*");
}
} catch (Exception e) {
e.printStackTrace();
}
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
try {
mContext.startActivity(intent);
} catch (ActivityNotFoundException unused) {
Toast.makeText(mContext, "No application available", Toast.LENGTH_SHORT).show();
}
}
});
}
public int getItemCount() {
return filepath.size();
}
}