【问题标题】:How to delete image from its image path如何从图像路径中删除图像
【发布时间】:2016-07-13 09:03:11
【问题描述】:

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

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

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

        }


}

【问题讨论】:

  • 那么什么不起作用?什么有效?
  • 当我点击删除按钮然后应用程序崩溃
  • 在这种情况下,您可以在 LogCat 中找到原因。请在此处发布相关部分。您必须找到导致此问题的代码行。然后告诉我们。
  • 我解决了这个问题,但是现在当我按下返回按钮时,图像又出现了,但它被删除了,但又显示回来了..为什么?
  • 说出问题所在并发布修改后的代码。

标签: android image delete-file


【解决方案1】:

我认为你的 MediaStore 没有更新。删除后你有没有尝试过这个功能

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

适用于 4.4 及更低版本

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】:

    你可以看到问题回答link

    对于删除后的情况,如果为真,则检查布尔变量,然后重新加载/刷新您的列表。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多