如何从图像路径中删除图像 [英] How to delete image from its image path

查看:157
本文介绍了如何从图像路径中删除图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从路径中删除图片时出现问题。真的很困惑,从应用程序中删除它以及从画廊
我在从图像中删除图像时遇到问题查看热门从活动中删除它以及文件位置的外部媒体。

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.

从3天开始尝试,但尚未找到解决方案。我需要在此java文件中应用删除按钮代码

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

        }


}


推荐答案

你可以看到问题的答案 link

You can see questions answer link

如果删除后通过检查一个布尔变量来删除你的情况,然后重新加载/刷新你的列表。

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

这篇关于如何从图像路径中删除图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆