从gallery / filesystem中删除图像

时间:2018-04-04 05:28:42

标签: android file gallery delete-file

我正在开发类似于图库的应用,我正在尝试删除选项。正如在其他线程中所建议的那样,我尝试在删除后使用媒体扫描,但由于某些未知原因,它无效。该文件已被删除,但在我的应用程序中,它显示文件仍然存在。

在其他图库APPS中,我可以看到文件被删除,但即使在卸载我的应用程序并重新安装后,列表文件也可用,但点击它我会得到一个空屏幕进行预览。

这是我的活动:

package com.example.sumukha.galleri.Activities;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.BaseColumns;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.example.sumukha.galleri.Adapter.FullScreenImageAdapter;
import com.example.sumukha.galleri.R;
import com.example.sumukha.galleri.Utils.Function;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

public class FullScreenImageActivity extends AppCompatActivity {


    ArrayList<HashMap<String, String>> imageList = new ArrayList<HashMap<String, String>>();
    String imagePath;
    int position;
    ArrayList<String> allPaths;
    private FullScreenImageAdapter adapter;
    private ViewPager viewPager;

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

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        imageList = (ArrayList<HashMap<String, String>>) getIntent().getSerializableExtra("list");
        imagePath = getIntent().getStringExtra("path");
        position = getIntent().getIntExtra("position",0);

        viewPager = findViewById(R.id.media_pager);

        updatePageTitle(position);

        allPaths = new ArrayList<>();
        System.out.println("******* imageList.size() "+imageList.size());
        for(int i=0; i<imageList.size();i++){

            allPaths.add(imageList.get(i).get(Function.KEY_PATH));
            System.out.println("******* path at "+i+" is "+imageList.get(i).get(Function.KEY_PATH));
        }

        adapter = new FullScreenImageAdapter(FullScreenImageActivity.this,
                allPaths);
        viewPager.setAdapter(adapter);
        // displaying selected image first
        viewPager.setCurrentItem(position);

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                updatePageTitle(position);
                imagePath = imageList.get(position).get(Function.KEY_PATH);
                System.out.println("******* new path "+imagePath);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void updatePageTitle(int position) {
        getSupportActionBar().setTitle(getString(R.string.of, (position+1), imageList.size()));
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()){
            case android.R.id.home:
                this.finish();
                return true;

            case  R.id.action_share :
                shareImage(imagePath);
                //Toast.makeText(FullScreenImageActivity.this,"ABt to share",Toast.LENGTH_SHORT).show();
                break;

            case  R.id.action_settings :
                Toast.makeText(FullScreenImageActivity.this,"ABt d pic..",Toast.LENGTH_SHORT).show();
                break;

            case  R.id.fav_image :
                Toast.makeText(FullScreenImageActivity.this,"Mark himmmm",Toast.LENGTH_SHORT).show();
                break;

            case  R.id.delete_image :
                deleteImage(imagePath);
                //Toast.makeText(FullScreenImageActivity.this,"ABt to delete",Toast.LENGTH_SHORT).show();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.viewimage_menu, menu);
        return true;
    }


public void shareImage(String filePath){

    Intent share = new Intent(Intent.ACTION_SEND);
    share.setType("image/*");
    Uri uri = Uri.fromFile(new File(filePath));
    share.putExtra(Intent.EXTRA_STREAM, Uri.parse(String.valueOf(uri)));
    startActivity(Intent.createChooser(share, "Share Image"));
    Uri.fromFile(new File(filePath));

}

    public void deleteImage(String filePath) {

        System.out.println("******* imgpth recvd to delete "+filePath);

        //String file_dj_path = filePath;
        String smallPath = filePath.substring(19);

        File fdelete = new File(Environment.getExternalStorageDirectory()+smallPath);

        if (fdelete.exists()) {
            if (fdelete.delete()) {
                System.out.println("******* imgpth deleted "+filePath);
                callBroadCast();
            } else {
                System.out.println("******* imgpth not deleted "+filePath);
            }
        }


    }

    public void callBroadCast() {
        if (Build.VERSION.SDK_INT >= 14) {
            System.out.println("******* callBroadCast > 14");
            MediaScannerConnection.scanFile(this, new String[]{Environment.getExternalStorageDirectory().toString()}, null, new MediaScannerConnection.OnScanCompletedListener() {
                /*
                 *   (non-Javadoc)
                 * @see android.media.MediaScannerConnection.OnScanCompletedListener#onScanCompleted(java.lang.String, android.net.Uri)
                 */
                public void onScanCompleted(String path, Uri uri) {
                    System.out.println("******* Scanned "+path);
                    System.out.println("******* uri "+uri);
                }
            });
        } else {
            System.out.println("******* SDK < 14 ");
            sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
                    Uri.parse("file://" + Environment.getExternalStorageDirectory())));
        }
    }
}

0 个答案:

没有答案