0

I'm facing problem in deleting image from imageView hot to delete it from the activity as well as the external media of the file location.

Trying it since 3 days and found no solution yet. I need to apply the delete button code in this java file

public class FullScreenImageAdapter extends PagerAdapter {

private Activity _activity;
private ArrayList<String> _imagePaths;
private LayoutInflater inflater;

// constructor
public FullScreenImageAdapter(Activity activity,
        ArrayList<String> imagePaths) {
    this._activity = activity;
    this._imagePaths = imagePaths;
}

@Override
public int getCount() {
    return this._imagePaths.size();
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return view == ((RelativeLayout) object);
}

@Override
public Object instantiateItem(ViewGroup container, final int position) {
    TouchImageView imgDisplay;
    Button btnClose, deleteButton;

    inflater = (LayoutInflater) _activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View viewLayout = inflater.inflate(R.layout.layout_fullscreen_image,
            container, false);

    imgDisplay = (TouchImageView) viewLayout.findViewById(R.id.imgDisplay);
    btnClose = (Button) viewLayout.findViewById(R.id.btnClose);
    deleteButton = (Button) viewLayout.findViewById(R.id.btndelete);
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    Bitmap bitmap = BitmapFactory.decodeFile(_imagePaths.get(position),
            options);
    imgDisplay.setImageBitmap(bitmap);

    // close button click event
    btnClose.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            _activity.finish();
        }
    });




    deleteButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
        // TODO Auto-generated method stub




            String myFile = "/FakeNewsMaker/File.jpg";  

            // File file = new File(imagePath);
         //     if(file.exists())
            //      file.delete();

        }
        });





    ((ViewPager) container).addView(viewLayout);

    return viewLayout;
}






protected void startActivity(Intent createChooser) {
    // TODO Auto-generated method stub

}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    ((ViewPager) container).removeView((RelativeLayout) object);

}
}
SripadRaj
  • 1,687
  • 2
  • 22
  • 33

4 Answers4

4

Just set image bitmap to null like this -

 imgDisplay.setImageBitmap(null);
Onkar Nene
  • 1,359
  • 1
  • 17
  • 23
Narendra Sorathiya
  • 3,770
  • 2
  • 34
  • 37
2

you can delete the the imageFile like this.

 public void DeleteFile(String fullPath, int position) {

    try {
        File sdCardRoot = Environment.getExternalStorageDirectory();
        Log.w("SDCardRoot", "" + sdCardRoot);
        File yourDir = new File(sdCardRoot, "/YourFolderName");
        if (!yourDir.exists()) {
            yourDir.mkdirs();
            Log.w("DashBordAct DirName:", "" + yourDir);
        }

        if (yourDir.isDirectory()) {
            File[] content = yourDir.listFiles();
            for (int i = 0; i < content.length; i++) {
                if (content[i].toString().contains(fullPath)) {
                    boolean success = content[i].delete();
                    if (!success) {
                        Log.w("file", "not deleted);
                    } else {
                        Log.w("file", "deleted");
                        _imagePath.remove(position);
                        notifyItemRemoved(position);

                    }
                }
            }
        }
    } catch (NullPointerException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

and set this method in DeleteButton listener like this:

deleteButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            String myFile = "/FakeNewsMaker/File.jpg";  
            DeleteFile(imagePath, getAdapterPosition) //or myFile.
        }
        });

and also set permission in manifest.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
Sagar Chavada
  • 5,169
  • 7
  • 40
  • 67
0

To remove from ImageView @Narendra Sorathiya solution is fine if you also want delete it from storage then use the combination.

deleteButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
        // TODO Auto-generated method stub

            imgDisplay.setImageBitmap(null);
            String myFile = v.getTag().toString();  

            File file = new File(myFile);
            if(file.exists())
               file.delete();
            }
        });

Update:

 deleteButton = (Button) viewLayout.findViewById(R.id.btndelete);
 // add below line after deleteButton 
 deleteButton.setTag(_imagePaths.get(position));

And then get Tag when delete button clicked like this

String myFile = v.getTag().toString();

I update the code in above section.

Sohail Zahid
  • 8,099
  • 2
  • 25
  • 41
0

You may use,

imgView.setImageResource(0);

or

imgView.setImageResource(android.R.color.transparent);

or

imgView.setImageBitmap(null);
imgView.destroyDrawingCache();

I hope this works for you !

Sathish Kumar J
  • 4,280
  • 1
  • 20
  • 48