allow renaming files

This commit is contained in:
tibbi 2016-03-05 13:59:32 +01:00
parent f0de223155
commit a242fb783d
4 changed files with 98 additions and 3 deletions

View file

@ -1,5 +1,6 @@
package com.simplemobiletools.gallery.activities;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.media.MediaScannerConnection;
@ -8,10 +9,13 @@ import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.simplemobiletools.gallery.Constants;
import com.simplemobiletools.gallery.Helpers;
@ -76,6 +80,9 @@ public class ViewPagerActivity extends AppCompatActivity
case R.id.menu_remove:
deleteImage();
return true;
case R.id.menu_edit:
editImage();
return true;
default:
return super.onOptionsItemSelected(item);
}
@ -84,7 +91,7 @@ public class ViewPagerActivity extends AppCompatActivity
private void shareImage() {
final String shareTitle = getResources().getString(R.string.share_via);
final Intent sendIntent = new Intent();
final File file = new File(photos.get(pager.getCurrentItem()));
final File file = getCurrentFile();
final Uri uri = Uri.fromFile(file);
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_STREAM, uri);
@ -94,8 +101,7 @@ public class ViewPagerActivity extends AppCompatActivity
private void deleteImage() {
Helpers.showToast(this, R.string.deleting);
final String path = photos.get(pager.getCurrentItem());
final File file = new File(path);
final File file = getCurrentFile();
file.delete();
MediaScannerConnection.scanFile(this, new String[]{path}, null, this);
}
@ -109,6 +115,50 @@ public class ViewPagerActivity extends AppCompatActivity
return false;
}
private void editImage() {
final File file = getCurrentFile();
final String fullName = file.getName();
final int pos = fullName.lastIndexOf(".");
if (pos <= 0)
return;
final String name = fullName.substring(0, pos);
final String extension = fullName.substring(pos + 1, fullName.length());
final View renameFileView = getLayoutInflater().inflate(R.layout.rename_file, null);
final EditText fileNameET = (EditText) renameFileView.findViewById(R.id.file_name);
fileNameET.setText(name);
final EditText extensionET = (EditText) renameFileView.findViewById(R.id.extension);
extensionET.setText(extension);
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle(getResources().getString(R.string.rename_file));
alertDialog.setView(renameFileView);
alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
final String fileName = fileNameET.getText().toString().trim();
final String extension = extensionET.getText().toString().trim();
final File newFile = new File(file.getParent(), fileName + "." + extension);
if (!fileName.isEmpty() && !extension.isEmpty() && file.renameTo(newFile)) {
photos.set(pager.getCurrentItem(), newFile.getAbsolutePath());
final String[] changedFiles = {file.getAbsolutePath(), newFile.getAbsolutePath()};
MediaScannerConnection.scanFile(getApplicationContext(), changedFiles, null, null);
updateActionbarTitle();
} else {
Toast.makeText(getApplicationContext(), getResources().getString(R.string.rename_error), Toast.LENGTH_SHORT).show();
}
}
});
alertDialog.setNegativeButton("Cancel", null);
alertDialog.show();
}
private void reloadViewPager() {
final MyPagerAdapter adapter = (MyPagerAdapter) pager.getAdapter();
final int pos = pager.getCurrentItem();
@ -147,6 +197,7 @@ public class ViewPagerActivity extends AppCompatActivity
final int pathIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
do {
final String curPath = cursor.getString(pathIndex);
if (curPath.matches(pattern)) {
photos.add(curPath);
@ -195,6 +246,10 @@ public class ViewPagerActivity extends AppCompatActivity
setTitle(Helpers.getFilename(photos.get(pager.getCurrentItem())));
}
private File getCurrentFile() {
return new File(photos.get(pager.getCurrentItem()));
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

View file

@ -0,0 +1,30 @@
<?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="match_parent"
android:orientation="vertical"
android:padding="@dimen/activity_margin">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/file_name"/>
<EditText
android:id="@+id/file_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_margin"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/extension"/>
<EditText
android:id="@+id/extension"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_margin"/>
</LinearLayout>

View file

@ -1,6 +1,11 @@
<?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_edit"
android:icon="@android:drawable/ic_menu_edit"
android:title="@string/edit"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/menu_remove"
android:icon="@android:drawable/ic_menu_delete"

View file

@ -4,7 +4,12 @@
<string name="no_permissions">Not much to do in a gallery without accessing your photos</string>
<string name="remove">Remove</string>
<string name="deleting">Deleting</string>
<string name="edit">Edit</string>
<string name="undo">Undo</string>
<string name="rename_file">Rename file</string>
<string name="rename_error">Could not rename the file</string>
<string name="file_name">File name</string>
<string name="extension">Extension</string>
<plurals name="files_deleted">
<item quantity="one">1 file deleted</item>