How to delete image from its image path

青春壹個敷衍的年華 提交于 2019-12-23 01:04:25

问题


Problem in deleting image from its path. really confused in deleting it from the application as well from the gallery I'm facing problem in deleting image from image View 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 FullScreenViewActivity extends Activity {

    private Utils utils;
    private FullScreenImageAdapter adapter, image;
    private ViewPager viewPager;
    Button btnClose, btnShare, btnDelete;
    private static Context mContext;

     ContentResolver contentResolver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fullscreen_view);

        viewPager = (ViewPager) findViewById(R.id.pager);

        utils = new Utils(getApplicationContext());

        Intent i = getIntent();
        int position = i.getIntExtra("position", 0);

        adapter = new FullScreenImageAdapter(FullScreenViewActivity.this,
                utils.getFilePaths());

        viewPager.setAdapter(adapter);

        // displaying selected image first
        viewPager.setCurrentItem(position);
        btnClose = (Button) findViewById(R.id.btnClose);
        btnShare = (Button) findViewById(R.id.btnshare0);
        btnDelete = (Button) findViewById(R.id.btndelete);

        btnClose.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                finish();
            }
        });

        btnShare.setOnClickListener(new OnClickListener() {

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

                File file = new File(adapter._imagePaths.get(viewPager
                        .getCurrentItem()));
                Intent mShareIntent = new Intent(Intent.ACTION_SEND);
                mShareIntent.setType("image/*");
                mShareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
                startActivity(Intent.createChooser(mShareIntent, "Shareith:"));
            }
        });

        btnDelete.setOnClickListener(new OnClickListener() {

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

                DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        switch (which) {
                        case DialogInterface.BUTTON_POSITIVE:
                            deleteTmpFile(viewPager.getCurrentItem());

                            break;
                        case DialogInterface.BUTTON_NEGATIVE:
                            break;
                        }
                    }

                };

                AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                builder.setMessage("Delete this Photo?")
                        .setPositiveButton("Yes", dialogClickListener)
                        .setNegativeButton("No", dialogClickListener).show();


            }
        });
    }

    public void deleteTmpFile(int pos) {
        //String Foldername = mContext.getResources()
            //  .getString(R.string.app_name);
        /*
         * String filepath = Environment.getExternalStorageDirectory().getPath()
         * + "/" + Foldername + "/" +data.get(pos) ;
         */
        File f = new File(adapter._imagePaths.get(viewPager
                .getCurrentItem()));
        //File f = new File(filepath);
        if (f.exists()) {
            f.delete();
            deleteFileFromMediaStore(mContext.getContentResolver(), f);
            // final Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
            // mContext.getContentResolver().delete(uri,
            // MediaStore.MediaColumns.DATA + " =?",
            // new String[] { filepath });

            notifyAll();
        }
        Toast.makeText(mContext, "Delete Successfully..", Toast.LENGTH_SHORT)
                .show();
    }

        public static void deleteFileFromMediaStore(
                final ContentResolver contentResolver, final File file) {
            String canonicalPath;
            try {
                canonicalPath = file.getCanonicalPath();
            } catch (IOException e) {
                canonicalPath = file.getAbsolutePath();
            }
            final Uri uri = MediaStore.Files.getContentUri("external");
            final int result = contentResolver.delete(uri,
                    MediaStore.Files.FileColumns.DATA + "=?",
                    new String[] { canonicalPath });
            if (result == 0) {
                final String absolutePath = file.getAbsolutePath();
                if (!absolutePath.equals(canonicalPath)) {
                    contentResolver.delete(uri, MediaStore.Files.FileColumns.DATA
                            + "=?", new String[] { absolutePath });
                }
            }

        }


}

回答1:


I think your MediaStore is not updated.Have you tried this function after deletion

sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(fileDeleted)));

For 4.4 and lower

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));



回答2:


  package com.example.fakenewspapermaker;

import info.androidhive.imageslider.helper.Utils;

import java.io.File;
import java.io.IOException;



import android.R.string;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class FullScreenViewActivity extends Activity {

    private Utils utils;
    private FullScreenImageAdapter adapter, image;
    private ViewPager viewPager;
    Button btnClose, btnShare, btnDelete;
    private static Context mContext;

     ContentResolver contentResolver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fullscreen_view);

        viewPager = (ViewPager) findViewById(R.id.pager);

        utils = new Utils(getApplicationContext());

        Intent i = getIntent();
        int position = i.getIntExtra("position", 0);

        adapter = new FullScreenImageAdapter(FullScreenViewActivity.this,
                utils.getFilePaths());

        viewPager.setAdapter(adapter);

        // displaying selected image first
        viewPager.setCurrentItem(position);
        btnClose = (Button) findViewById(R.id.btnClose);
        btnShare = (Button) findViewById(R.id.btnshare0);
        btnDelete = (Button) findViewById(R.id.btndelete);

        btnClose.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                finish();
            }
        });

        btnShare.setOnClickListener(new OnClickListener() {

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

                File file = new File(adapter._imagePaths.get(viewPager
                        .getCurrentItem()));
                Intent mShareIntent = new Intent(Intent.ACTION_SEND);
                mShareIntent.setType("image/*");
                mShareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
                startActivity(Intent.createChooser(mShareIntent, "Share with:"));
            }
        });

        btnDelete.setOnClickListener(new OnClickListener() {

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

                DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        switch (which) {


                        case DialogInterface.BUTTON_POSITIVE:
                            File file = new File(adapter._imagePaths.get(viewPager
                                    .getCurrentItem()));
                            if (file.exists())
                            {
                                file.delete();

                            }
                            finish();


                            break;


                        case DialogInterface.BUTTON_NEGATIVE:
                            break;
                        }
                    }

                };

                AlertDialog.Builder builder = new AlertDialog.Builder(FullScreenViewActivity.this);
                builder.setMessage("Delete this Photo?")
                        .setPositiveButton("Yes", dialogClickListener)
                        .setNegativeButton("No", dialogClickListener).show();


            }
        });
    }

    public void deleteTmpFile(int pos) {
        //String Foldername = mContext.getResources()
            //  .getString(R.string.app_name);
        /*
         * String filepath = Environment.getExternalStorageDirectory().getPath()
         * + "/" + Foldername + "/" +data.get(pos) ;
         */
        File f = new File(adapter._imagePaths.get(viewPager
                .getCurrentItem()));
        //File f = new File(filepath);
        if (f.exists()) {
            f.delete();
            deleteFileFromMediaStore(mContext.getContentResolver(), f);
            // final Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
            // mContext.getContentResolver().delete(uri,
            // MediaStore.MediaColumns.DATA + " =?",
            // new String[] { filepath });

            notifyAll();
        }
        Toast.makeText(mContext, "Delete Successfully..", Toast.LENGTH_SHORT)
                .show();
    }

        public static void deleteFileFromMediaStore(
                final ContentResolver contentResolver, final File file) {
            String canonicalPath;
            try {
                canonicalPath = file.getCanonicalPath();
            } catch (IOException e) {
                canonicalPath = file.getAbsolutePath();
            }
            final Uri uri = MediaStore.Files.getContentUri("external");
            final int result = contentResolver.delete(uri,
                    MediaStore.Files.FileColumns.DATA + "=?",
                    new String[] { canonicalPath });
            if (result == 0) {
                final String absolutePath = file.getAbsolutePath();
                if (!absolutePath.equals(canonicalPath)) {
                    contentResolver.delete(uri, MediaStore.Files.FileColumns.DATA
                            + "=?", new String[] { absolutePath });
                }
            }

        }
        @Override
        public void onBackPressed()
        {
            finish();
              Intent newIntent = new Intent(FullScreenViewActivity.this,
                MySavedNews.class);
              newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_NEW_TASK);
              startActivity(newIntent);
        }

}



回答3:


You can see questions answer link

and for your case after delete by checking a boolean variable if true then reload/refresh your list.



来源:https://stackoverflow.com/questions/38347152/how-to-delete-image-from-its-image-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!