如何从图像路径中删除图像

时间:2016-07-13 09:03:12

标签: android image delete-file

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

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

Milestones.objects.filter(teammember__id)

3 个答案:

答案 0 :(得分:0)

我认为你的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())));

答案 1 :(得分:0)

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

}

答案 2 :(得分:0)

您可以看到问题回答link

对于你的情况,删除后检查一个布尔变量是否为true,然后重新加载/刷新你的列表。