Android使用OnClick从SD卡中删除图像

Android使用OnClick从SD卡中删除图像

本文介绍了Android使用OnClick从SD卡中删除图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从简单的应用程序中简单地删除图像。我拥有这个功能,当你点击图片时,它会弹出一个对话框,提供删除选项的选项。我认为这只是简单的事情,但我一直在尝试的一切似乎都没有做任何事情。以下是我的代码。任何想法将不胜感激。

  delete.setOnClickListener(new View.OnClickListener(){

@Override
public void onClick(View v){
// TODO自动生成的方法存根
int id = viewIt.getId();

文件file = new File(file:/ /+ arrPath [id] +。jpg);
file.delete();



});


解决方案

任何声明 WRITE_EXTERNAL_STORAGE 权限的应用程序都被隐式授予。 code> READ_EXTERNAL_STORAGE 权限

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

获取所需文件的路径并删除文件

  File file = new File(android.os.Environment.getExternalStorageDirectory()+/myfolder/myimage.jpg); 
if(file.exists())
{
file.delete();
}


I'm trying to simply delete an image from a simple app. I have it so that when you click on the image, it'll bring up an a dialog with the option to delete it. I thought this would just be something simple, but everything I have been trying doesn't seem to be doing anything. Below is my code. Any ideas would be greatly appreciated.

    delete.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
    int id = viewIt.getId();

    File file = new File("file://" + arrPath[id]+".jpg");
    file.delete();


    }
});
解决方案

Have you added permission in manifest file ?

Any app that declares the WRITE_EXTERNAL_STORAGE permission is implicitly granted READ_EXTERNAL_STORAGE permission.

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

Get the path of the file required and delete the file

File file= new File(android.os.Environment.getExternalStorageDirectory()+ "/myfolder/myimage.jpg");
    if(file.exists())
    {
         file.delete();
    }

这篇关于Android使用OnClick从SD卡中删除图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 18:56