implement deleting files from viewpager

This commit is contained in:
tibbi 2016-02-29 21:25:08 +01:00
parent fdc2e17d39
commit 6f629cc5f5
3 changed files with 58 additions and 7 deletions

View file

@ -2,6 +2,7 @@ package com.simplemobiletools.gallery.activities;
import android.content.Intent;
import android.database.Cursor;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
@ -29,6 +30,7 @@ public class ViewPagerActivity extends AppCompatActivity implements ViewPager.On
private ActionBar actionbar;
private List<String> photos;
private MyViewPager pager;
private String path;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -40,10 +42,10 @@ public class ViewPagerActivity extends AppCompatActivity implements ViewPager.On
actionbar = getSupportActionBar();
hideSystemUI();
final String path = getIntent().getStringExtra(Constants.PHOTO);
path = getIntent().getStringExtra(Constants.PHOTO);
final MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager());
pager = (MyViewPager) findViewById(R.id.view_pager);
photos = getPhotos(path);
photos = getPhotos();
adapter.setPaths(photos);
pager.setAdapter(adapter);
pager.setCurrentItem(pos);
@ -63,7 +65,7 @@ public class ViewPagerActivity extends AppCompatActivity implements ViewPager.On
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
getMenuInflater().inflate(R.menu.viewpager_menu, menu);
return true;
}
@ -73,6 +75,9 @@ public class ViewPagerActivity extends AppCompatActivity implements ViewPager.On
case R.id.menu_share:
shareImage();
return true;
case R.id.menu_remove:
deleteImage();
return true;
default:
return super.onOptionsItemSelected(item);
}
@ -89,7 +94,43 @@ public class ViewPagerActivity extends AppCompatActivity implements ViewPager.On
startActivity(Intent.createChooser(sendIntent, shareTitle));
}
private List<String> getPhotos(final String path) {
private void deleteImage() {
final String path = photos.get(pager.getCurrentItem());
final File file = new File(path);
file.delete();
MediaScannerConnection.scanFile(this, new String[]{path}, null, new MediaScannerConnection.OnScanCompletedListener() {
@Override
public void onScanCompleted(final String path, final Uri uri) {
runOnUiThread(new Runnable() {
@Override
public void run() {
reloadViewPager();
}
});
}
});
}
private void reloadViewPager() {
final MyPagerAdapter adapter = (MyPagerAdapter) pager.getAdapter();
final int pos = pager.getCurrentItem();
photos = getPhotos();
if (photos.size() <= 0) {
finish();
return;
}
pager.setAdapter(null);
adapter.updateItems(photos);
pager.setAdapter(adapter);
final int newPos = Math.min(pos, adapter.getCount());
pager.setCurrentItem(newPos);
updateActionbarTitle();
}
private List<String> getPhotos() {
final List<String> photos = new ArrayList<>();
final String fileDir = new File(path).getParent();
final Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

View file

@ -4,10 +4,10 @@ import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import java.util.List;
import com.simplemobiletools.gallery.ViewPagerFragment;
import java.util.List;
public class MyPagerAdapter extends FragmentStatePagerAdapter {
private List<String> paths;
@ -30,4 +30,10 @@ public class MyPagerAdapter extends FragmentStatePagerAdapter {
fragment.setPath(paths.get(position));
return fragment;
}
public void updateItems(List<String> newPaths) {
paths.clear();
paths.addAll(newPaths);
notifyDataSetChanged();
}
}

View file

@ -1,10 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_remove"
android:icon="@android:drawable/ic_menu_delete"
android:title="@string/remove"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/menu_share"
android:icon="@android:drawable/ic_menu_share"
android:title="Share"
app:showAsAction="always"/>
</menu>