从imageview中删除图像

时间:2016-07-13 07:31:08

标签: android image delete-file

我在从imageView hot删除图片时遇到问题,要将其从活动以及文件位置的外部媒体中删除。

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

public class FullScreenImageAdapter extends PagerAdapter {

private Activity _activity;
private ArrayList<String> _imagePaths;
private LayoutInflater inflater;

// constructor
public FullScreenImageAdapter(Activity activity,
        ArrayList<String> imagePaths) {
    this._activity = activity;
    this._imagePaths = imagePaths;
}

@Override
public int getCount() {
    return this._imagePaths.size();
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return view == ((RelativeLayout) object);
}

@Override
public Object instantiateItem(ViewGroup container, final int position) {
    TouchImageView imgDisplay;
    Button btnClose, deleteButton;

    inflater = (LayoutInflater) _activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View viewLayout = inflater.inflate(R.layout.layout_fullscreen_image,
            container, false);

    imgDisplay = (TouchImageView) viewLayout.findViewById(R.id.imgDisplay);
    btnClose = (Button) viewLayout.findViewById(R.id.btnClose);
    deleteButton = (Button) viewLayout.findViewById(R.id.btndelete);
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    Bitmap bitmap = BitmapFactory.decodeFile(_imagePaths.get(position),
            options);
    imgDisplay.setImageBitmap(bitmap);

    // close button click event
    btnClose.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            _activity.finish();
        }
    });




    deleteButton.setOnClickListener(new View.OnClickListener() {

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




            String myFile = "/FakeNewsMaker/File.jpg";  

            // File file = new File(imagePath);
         //     if(file.exists())
            //      file.delete();

        }
        });





    ((ViewPager) container).addView(viewLayout);

    return viewLayout;
}






protected void startActivity(Intent createChooser) {
    // TODO Auto-generated method stub

}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    ((ViewPager) container).removeView((RelativeLayout) object);

}
}

4 个答案:

答案 0 :(得分:4)

只需将图像位图设置为null,就像这样 -

 imgDisplay.setImageBitmap(null);

答案 1 :(得分:2)

你可以像这样删除imageFile。

 public void DeleteFile(String fullPath, int position) {

    try {
        File sdCardRoot = Environment.getExternalStorageDirectory();
        Log.w("SDCardRoot", "" + sdCardRoot);
        File yourDir = new File(sdCardRoot, "/YourFolderName");
        if (!yourDir.exists()) {
            yourDir.mkdirs();
            Log.w("DashBordAct DirName:", "" + yourDir);
        }

        if (yourDir.isDirectory()) {
            File[] content = yourDir.listFiles();
            for (int i = 0; i < content.length; i++) {
                if (content[i].toString().contains(fullPath)) {
                    boolean success = content[i].delete();
                    if (!success) {
                        Log.w("file", "not deleted);
                    } else {
                        Log.w("file", "deleted");
                        _imagePath.remove(position);
                        notifyItemRemoved(position);

                    }
                }
            }
        }
    } catch (NullPointerException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

并在DeleteButton侦听器中设置此方法,如下所示:

deleteButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            String myFile = "/FakeNewsMaker/File.jpg";  
            DeleteFile(imagePath, getAdapterPosition) //or myFile.
        }
        });

并在清单中设置权限。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

答案 2 :(得分:0)

要从ImageView中删除@Narendra Sorathiya解决方案,如果您还想从存储中删除它,那么请使用该组合。

deleteButton.setOnClickListener(new View.OnClickListener() {

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

            imgDisplay.setImageBitmap(null);
            String myFile = v.getTag().toString();  

            File file = new File(myFile);
            if(file.exists())
               file.delete();
            }
        });

<强>更新

 deleteButton = (Button) viewLayout.findViewById(R.id.btndelete);
 // add below line after deleteButton 
 deleteButton.setTag(_imagePaths.get(position));

然后在点击删除按钮时获取标记

String myFile = v.getTag().toString();

我更新了上一节中的代码。

答案 3 :(得分:0)

您可以使用,

imgView.setImageResource(0);

imgView.setImageResource(android.R.color.transparent);

imgView.setImageBitmap(null);
imgView.destroyDrawingCache();

我希望这适合你!