Ashwani Sharma
Ashwani Sharma

Reputation: 9

Deleting image from imageview

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);

}
}

Upvotes: 0

Views: 1934

Answers (4)

Sagar Chavada
Sagar Chavada

Reputation: 5269

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"/> 

Upvotes: 2

Sohail Zahid
Sohail Zahid

Reputation: 8149

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.

Upvotes: 0

Narendra Sorathiya
Narendra Sorathiya

Reputation: 3830

Just set image bitmap to null like this -

 imgDisplay.setImageBitmap(null);

Upvotes: 4

Sathish Kumar J
Sathish Kumar J

Reputation: 4335

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 !

Upvotes: 0

Related Questions