implement deleting files from viewpager
This commit is contained in:
parent
fdc2e17d39
commit
6f629cc5f5
3 changed files with 58 additions and 7 deletions
|
@ -2,6 +2,7 @@ package com.simplemobiletools.gallery.activities;
|
||||||
|
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.database.Cursor;
|
import android.database.Cursor;
|
||||||
|
import android.media.MediaScannerConnection;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.provider.MediaStore;
|
import android.provider.MediaStore;
|
||||||
|
@ -29,6 +30,7 @@ public class ViewPagerActivity extends AppCompatActivity implements ViewPager.On
|
||||||
private ActionBar actionbar;
|
private ActionBar actionbar;
|
||||||
private List<String> photos;
|
private List<String> photos;
|
||||||
private MyViewPager pager;
|
private MyViewPager pager;
|
||||||
|
private String path;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
|
@ -40,10 +42,10 @@ public class ViewPagerActivity extends AppCompatActivity implements ViewPager.On
|
||||||
actionbar = getSupportActionBar();
|
actionbar = getSupportActionBar();
|
||||||
hideSystemUI();
|
hideSystemUI();
|
||||||
|
|
||||||
final String path = getIntent().getStringExtra(Constants.PHOTO);
|
path = getIntent().getStringExtra(Constants.PHOTO);
|
||||||
final MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager());
|
final MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager());
|
||||||
pager = (MyViewPager) findViewById(R.id.view_pager);
|
pager = (MyViewPager) findViewById(R.id.view_pager);
|
||||||
photos = getPhotos(path);
|
photos = getPhotos();
|
||||||
adapter.setPaths(photos);
|
adapter.setPaths(photos);
|
||||||
pager.setAdapter(adapter);
|
pager.setAdapter(adapter);
|
||||||
pager.setCurrentItem(pos);
|
pager.setCurrentItem(pos);
|
||||||
|
@ -63,7 +65,7 @@ public class ViewPagerActivity extends AppCompatActivity implements ViewPager.On
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCreateOptionsMenu(Menu menu) {
|
public boolean onCreateOptionsMenu(Menu menu) {
|
||||||
getMenuInflater().inflate(R.menu.menu, menu);
|
getMenuInflater().inflate(R.menu.viewpager_menu, menu);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -73,6 +75,9 @@ public class ViewPagerActivity extends AppCompatActivity implements ViewPager.On
|
||||||
case R.id.menu_share:
|
case R.id.menu_share:
|
||||||
shareImage();
|
shareImage();
|
||||||
return true;
|
return true;
|
||||||
|
case R.id.menu_remove:
|
||||||
|
deleteImage();
|
||||||
|
return true;
|
||||||
default:
|
default:
|
||||||
return super.onOptionsItemSelected(item);
|
return super.onOptionsItemSelected(item);
|
||||||
}
|
}
|
||||||
|
@ -89,7 +94,43 @@ public class ViewPagerActivity extends AppCompatActivity implements ViewPager.On
|
||||||
startActivity(Intent.createChooser(sendIntent, shareTitle));
|
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 List<String> photos = new ArrayList<>();
|
||||||
final String fileDir = new File(path).getParent();
|
final String fileDir = new File(path).getParent();
|
||||||
final Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
|
final Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
|
||||||
|
|
|
@ -4,10 +4,10 @@ import android.support.v4.app.Fragment;
|
||||||
import android.support.v4.app.FragmentManager;
|
import android.support.v4.app.FragmentManager;
|
||||||
import android.support.v4.app.FragmentStatePagerAdapter;
|
import android.support.v4.app.FragmentStatePagerAdapter;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import com.simplemobiletools.gallery.ViewPagerFragment;
|
import com.simplemobiletools.gallery.ViewPagerFragment;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class MyPagerAdapter extends FragmentStatePagerAdapter {
|
public class MyPagerAdapter extends FragmentStatePagerAdapter {
|
||||||
private List<String> paths;
|
private List<String> paths;
|
||||||
|
|
||||||
|
@ -30,4 +30,10 @@ public class MyPagerAdapter extends FragmentStatePagerAdapter {
|
||||||
fragment.setPath(paths.get(position));
|
fragment.setPath(paths.get(position));
|
||||||
return fragment;
|
return fragment;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void updateItems(List<String> newPaths) {
|
||||||
|
paths.clear();
|
||||||
|
paths.addAll(newPaths);
|
||||||
|
notifyDataSetChanged();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,14 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<menu xmlns:android="http://schemas.android.com/apk/res/android"
|
<menu xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
xmlns:app="http://schemas.android.com/apk/res-auto">
|
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
|
<item
|
||||||
android:id="@+id/menu_share"
|
android:id="@+id/menu_share"
|
||||||
android:icon="@android:drawable/ic_menu_share"
|
android:icon="@android:drawable/ic_menu_share"
|
||||||
android:title="Share"
|
android:title="Share"
|
||||||
app:showAsAction="always"/>
|
app:showAsAction="always"/>
|
||||||
|
|
||||||
</menu>
|
</menu>
|
Loading…
Reference in a new issue