I'm developing an app that contains a gallery, where the user takes photos and the image is shown in GridView
, but I do not know how to delete the selected image
I have been able to do something like this: the user presses the image and the icon of an erase recycle bin appears but does not turn off.
Follow my code:
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
public class GaleriaaSp extends AppCompatActivity {
FloatingActionButton fab_plus,fab_twitterr,fab_fb;
Animation FabOpen,FabClose,FabRClockwise,FabRantiClockwise;
boolean isOpen=false;
final int CAMERA_GALERIA =12;
FloatingActionButton captureBtn = null;
final int CAMERA_CAPTURE = 1;
private Uri picUri;
private DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private GridView grid;
private List <String> listOfImagesPath;
private ArrayList <String> list;
int count=0;
public static final String GridViewDemo_ImagePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/GridViewDemo/";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_galeriaa_sp);
fab_plus =(FloatingActionButton)findViewById(R.id.fab_plus);
fab_twitterr =(FloatingActionButton)findViewById(R.id.fab_camera);
fab_fb =(FloatingActionButton)findViewById(R.id.fab_galeria);
FabOpen = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fab_open);
FabClose = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fab_close);
FabRClockwise = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.rotate_clockwise);
FabRantiClockwise = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.rotate_anticlockwise);
grid = ( GridView) findViewById(R.id.GridView);
listOfImagesPath = null;
listOfImagesPath = RetriveCapturedImagePath();
if(listOfImagesPath!=null){
grid.setAdapter(new ImageListAdapter(this,listOfImagesPath));
}
// CHAAMAR CAMERA twitterr
fab_twitterr.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(captureIntent, CAMERA_CAPTURE);
fab_fb.startAnimation(FabClose);
fab_twitterr.startAnimation(FabClose);
fab_plus.startAnimation(FabRantiClockwise);
fab_twitterr.setClickable(false);
fab_fb.setClickable(false);
isOpen = false;
}
});
// CHAAMAR Galeria twitter
fab_fb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Selecione uma imagem"), CAMERA_GALERIA);
fab_fb.startAnimation(FabClose);
fab_twitterr.startAnimation(FabClose);
fab_plus.startAnimation(FabRantiClockwise);
fab_twitterr.setClickable(false);
fab_fb.setClickable(false);
isOpen = false;
}
});
fab_plus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isOpen) {
fab_fb.startAnimation(FabClose);
fab_twitterr.startAnimation(FabClose);
fab_plus.startAnimation(FabRantiClockwise);
fab_twitterr.setClickable(false);
fab_fb.setClickable(false);
isOpen = false;
} else {
fab_fb.startAnimation(FabOpen);
fab_twitterr.startAnimation(FabOpen);
fab_plus.startAnimation(FabRClockwise);
fab_twitterr.setClickable(true);
fab_fb.setClickable(true);
isOpen = true;
}
}
});
grid.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL);
grid.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
@Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
count = count + 1;
mode.setTitle(count + " items selecionado");
}
@Override
public boolean onCreateActionMode(ActionMode arg0, Menu arg1) {
MenuInflater inflater = arg0.getMenuInflater();
inflater.inflate(R.menu.munu_delete, arg1);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode arg0, Menu arg1) {
return false;
}
@Override
public boolean onActionItemClicked(ActionMode arg0, MenuItem arg1) {
return true;
}
@Override
public void onDestroyActionMode(ActionMode arg1) {
}
});
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
String imagem = listOfImagesPath.get(position).toString();
Intent i = new Intent(GaleriaaSp.this, Zomgaleria.class);//mude aqui de acordo com a sua activity
i.putExtra("img", imagem);
startActivity(i);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
// getMenuInflater().inflate(R.menu.menu_galeriaa_sp, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
/// RECEBIMENTO DA CAMERA
if (resultCode == RESULT_OK) {
if(requestCode == CAMERA_CAPTURE){
Bundle extras = data.getExtras();
Bitmap thePic = extras.getParcelable("data");
String imgcurTime = dateFormat.format(new Date());
File imageDirectory = new File(GridViewDemo_ImagePath);
imageDirectory.mkdirs();
String _path = GridViewDemo_ImagePath + imgcurTime+".jpg";
try {
FileOutputStream out = new FileOutputStream(_path);
thePic.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.close();
} catch (FileNotFoundException e) {
e.getMessage();
} catch (IOException e) {
e.printStackTrace();
}
listOfImagesPath = null;
listOfImagesPath = RetriveCapturedImagePath();
if(listOfImagesPath!=null){
grid.setAdapter(new ImageListAdapter(this,listOfImagesPath));
}
}
}
if (resultCode == RESULT_OK) {
if(requestCode == CAMERA_GALERIA){
Bundle extras = data.getExtras();
Bitmap thePic = extras.getParcelable("data");
String imgcurTime = dateFormat.format(new Date());
File imageDirectory = new File(GridViewDemo_ImagePath);
imageDirectory.mkdirs();
String _path = GridViewDemo_ImagePath + imgcurTime+".jpg";
try {
FileOutputStream out = new FileOutputStream(_path);
thePic.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.close();
} catch (FileNotFoundException e) {
e.getMessage();
} catch (IOException e) {
e.printStackTrace();
}
listOfImagesPath = null;
listOfImagesPath = RetriveCapturedImagePath();
if(listOfImagesPath!=null){
grid.setAdapter(new ImageListAdapter(this,listOfImagesPath));
}
}
}
/// RECEBIMENTO DA GALERIA
}
private List<String> RetriveCapturedImagePath() {
List<String> tFileList = new ArrayList<String>();
File f = new File(GridViewDemo_ImagePath);
if (f.exists()) {
File[] files=f.listFiles();
Arrays.sort(files);
for(int i=0; i<files.length; i++){
File file = files[i];
if(file.isDirectory())
continue;
tFileList.add(file.getPath());
}
}
return tFileList;
}
public class ImageListAdapter extends BaseAdapter
{
private Context context;
private List<String> imgPic;
public ImageListAdapter(Context c, List<String> thePic)
{
context = c;
imgPic = thePic;
}
public int getCount() {
if(imgPic != null)
return imgPic.size();
else
return 0;
}
//---returns the ID of an item---
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
//---returns an ImageView view---
public View getView(final int position, final View convertView, ViewGroup parent)
{
final ImageView imageView;
BitmapFactory.Options bfOptions=new BitmapFactory.Options();
bfOptions.inDither=false; //Disable Dithering mode
bfOptions.inPurgeable=true; //Tell to gc that whether it needs free memory, the Bitmap can be cleared
bfOptions.inInputShareable=true; //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future
bfOptions.inTempStorage=new byte[32 * 1024];
if (convertView == null) {
imageView = new ImageView(context);
imageView.setLayoutParams(new GridView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
imageView.setPadding(0, 0, 0, 0);
} else {
imageView = (ImageView) convertView;
}
FileInputStream fs = null;
final Bitmap bm;
try {
fs = new FileInputStream(new File(imgPic.get(position).toString()));
if(fs!=null) {
bm=BitmapFactory.decodeFileDescriptor(fs.getFD(), null, bfOptions);
imageView.setImageBitmap(bm);
imageView.setId(position);
imageView.setLayoutParams(new GridView.LayoutParams(360, 370));
/* imageView.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v){
// imgPic.remove(position).toString();
//String bm = null;
//String imagem = imgPic.get(position).toString();
//Intent i = new Intent(GaleriaSp.this, Zomgaleria.class);//mude aqui de acordo com a sua activity
//i.putExtra("img", imagem);
//startActivity(i);
}
});*/
}
} catch (IOException e) {
e.printStackTrace();
} finally{
if(fs!=null) {
try {
fs.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return imageView;
}
}
}