Recently Added

6/recent/ticker-posts

How to Delete and Share Multiples file from Recylerview | Select Multiples item Part 16

In this video player app series I am going to implement how to select multiples file from recylerview and delete from internal storage or share 
so let's start.

1.create a reference a variable of ArrayList in  VideoFolder.java  


ArrayList<Uri> uris = new ArrayList<>();


2.create a function for delete and share in VideoFolder.java
private boolean selectedFile(ArrayList<VideoModel> list, boolean canIDelete) {
for (int i = 0; i < list.size(); i++) {
String id = list.get(i).getId();
String path = list.get(i).getPath();
uris.add(Uri.parse(path));
if (canIDelete) {
Uri contentUris = ContentUris
.withAppendedId(MediaStore.Video.Media.EXTERNAL_CONTENT_URI
, Long.parseLong(id));
File file = new File(path);
boolean isDeleted = file.delete();
if (isDeleted) {
getApplicationContext().getContentResolver().delete(contentUris,
null, null);
}
}
}

if (!canIDelete) {
MediaScannerConnection.scanFile(getApplicationContext(), new String[]{String.valueOf(uris)},
null, new MediaScannerConnection.OnScanCompletedListener() {
@Override
public void onScanCompleted(String path, Uri uri) {
Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
intent.setType("video/*");
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
startActivity(Intent.createChooser(intent, "share"));
}
});
}

return true;
}
3.call the function when you click delete or share icon in OptionItemSelected

When you click delete add this code:

case R.id.delete_selected:
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
boolean isDeleted = selectedFile(selectionArrayList, true);
runOnUiThread(new Runnable() {
@Override
public void run() {
if (isDeleted) {
clearSelectingToolbar();
refresh();
Toast.makeText(VideoFolder.this, "Delete Success",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(VideoFolder.this, "Delete Fail",
Toast.LENGTH_SHORT).show();
}
}
});
}
});
thread.start();
break;

When you click share add this code:
case R.id.share_selected:
boolean isSuccess = selectedFile(selectionArrayList, false);
if (isSuccess) {
clearSelectingToolbar();
refresh();
Toast.makeText(this, "Loading",
Toast.LENGTH_SHORT).show();
}
break;
Watch full Tutorials

Post a Comment

0 Comments