繁体   English   中英

如何从Android内部存储器中删除文件?

[英]How to delete a file from the internal memory in Android?

我正在使用HTC M9和Android 5.0.2,我有一个图像,我在屏幕上显示在ImageView中。

单击按钮,我将图像保存在设备本地。 在另一个按钮单击,我试图从我的设备和我的容器中删除图像。

我正在使用Android中的内存保存和读取位图/图像来保存和检索图像:

在Save_ButtonClick上:

 private void saveToInternalStorage(Bitmap bitmapImage, int ImageNumber){
        ContextWrapper cw = new ContextWrapper(getApplicationContext());

        // path to /data/data/yourapp/app_data/imageDir
        File MyDirectory = cw.getDir("imageDir", Context.MODE_PRIVATE);

        // Create imageDir
        File MyPath = new File(MyDirectory,"Image" + ImageNumber + ".jpg");

        // Add the path to the container
        ImagePathes.add("Image" + ImageNumber + ".jpg");

        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(MyPath);

            // Use the compress method on the BitMap object to write image to the OutputStream
            bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //return MyDirectory.getAbsolutePath();
    }

我尝试从内部存储删除文件中的答案, ImagesPathes仍然没有改变,我仍然在屏幕上看到图像。 我使用此代码删除我的文件:

public void DeleteImage(View view)
{
    try
    {
        // remove the file from internal storage
        ContextWrapper cw = new ContextWrapper(this);
        File MyDirectory = cw.getDir("imageDir", Context.MODE_PRIVATE);
        String path = MyDirectory.getAbsolutePath();
        File fileToBeDeleted = new File(getFilesDir(), ImagesNames.get(SelectedIndex)); // current image from my ArrayList - Bitmap
        boolean WasDeleted = fileToBeDeleted.delete();

        //Or
        //File dir = getFilesDir();
        //File file = new File(dir, ImagesNames.get(SelectedIndex));
        //boolean deleted = file.delete();

        // remove file name from my array
        ImagesNames.remove(SelectedIndex);
        SelectedIndex++;
    } catch (Exception e)
    {
        System.err.println(e.toString());
    }
}

没有错误,只是WasDeleted总是假的, ImagesPathes不会改变。 问题是什么?

编辑:

的Manifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.kkhalaf.mpconbot.test"> 
    <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="23" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <application>
        <uses-library android:name="android.test.runner" />
    </application>
    <instrumentation android:name="android.test.InstrumentationTestRunner"
                     android:targetPackage="com.example.kkhalaf.mpconbot"
                     android:handleProfiling="false"
                     android:functionalTest="false"
                     android:label="Tests for com.example.kkhalaf.mpconbot"/>
</manifest>

保存时的路径: /data/data/com.example.kkhalaf.mpconbot/files/Image0.jpg

删除时的路径: /data/data/com.example.kkhalaf.mpconbot/files/Image0.jpg

事实证明我的问题是在这一行:

ContextWrapper cw = new ContextWrapper(this);
File MyDirectory = cw.getDir("imageDir", Context.MODE_PRIVATE);
String path = MyDirectory.getAbsolutePath();
File fileToBeDeleted = new File(getFilesDir(), ImagesNames.get(SelectedIndex)); // here
boolean WasDeleted = fileToBeDeleted.delete();

而我应该这样做:

ContextWrapper cw = new ContextWrapper(this);
File MyDirectory = cw.getDir("imageDir", Context.MODE_PRIVATE);
String path = MyDirectory.getAbsolutePath();
File fileToBeDeleted = new File(path + "//Image" + SelectedIndex + ".jpg"); // current image
boolean WasDeleted = fileToBeDeleted.delete();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM