如何删除文件及其存储路径?

时间:2016-02-05 09:58:56

标签: java android file

我正在尝试从图库中删除视频文件,但以下代码无法正常工作 它只删除内容,但视频仍保留在图库中,当我尝试播放视频时,表示视频无法播放。

public void deleteVideo(View view)
{
    File f = new File(delete);
    f.delete();
    if(f.exists())
    {
        try
        {
            Toast.makeText(
                TestVideoRecordingActivity.this,
                "inside try",
                Toast.LENGTH_SHORT
            ).show();
            f.getCanonicalFile().delete();
        }
        catch (IOException e)
        {
            e.printStackTrace();
            dialog.dismiss();
        }
    }
    dialog.dismiss();
}

1 个答案:

答案 0 :(得分:0)

在以编程方式删除视频文件后添加以下行 -

对于api< 14

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + 
Environment.getExternalStorageDirectory())));

for api> = 14

MediaScannerConnection.scanFile(MainActivity.this, 
new String[] { Environment.getExternalStorageDirectory().toString() }, 
null, 
new MediaScannerConnection.OnScanCompletedListener() {

    public void onScanCompleted(String path, Uri uri) {

              Log.i("ExternalStorage", "Scanned " + path + ":");
              Log.i("ExternalStorage", "-> uri=" + uri);
    }
});