防止重复的图像保存到SD卡

时间:2013-04-22 13:22:39

标签: android sharedpreferences android-sdcard

我有一个listactivity app形成多行。每行打开一个包含视图的活动,其中一个是按钮,当点击打开无限图库类(图像存储在应用程序内的RES => drawable文件夹中)时,每个图像下都有按钮,当按下它时将图像保存到名为(saved_images)的文件夹中的SD卡目录。

我在图库类中使用SharedPreferences按顺序存储所有图像,效果很好 -

但我正在尝试:

  1. 防止重复保存在SD卡文件夹中的图像(saved_images):

    假设您成功保存了图像1然后按下图像-1下的相同按钮,它将再次保存在SD卡文件夹中,因此最终您将拥有相同的图像(图像-1)两次,

    所以我想得到的东西:当我按下已保存的图像下的按钮时,已保存的Toast图像必须上升,因此所有应用程序图像都会保存在Sd卡文件夹中。

  2. 重新安装后,请按顺序保存图像:

    在设备中安装应用程序并将一些图像保存在文件夹中(saved_images) 已经在SD卡中创建,假设您从设备卸载应用程序并保持 SD卡中的(saved_images)文件夹,然后重新安装应用程序并想要保存 一些新的图像,会发生什么是新图像取代以前保存的图像,

    但我想要:继续按照先前保存的图像按顺序保存新图像。

  3. 用于将图像保存到SD卡的代码:

    public void onClick(View arg0) {
        String root = Environment.getExternalStorageDirectory().toString();
        File myDir = new File(root + "/saved_images");
        if (!myDir.exists()) {
            myDir.mkdirs();
            SharedPreferences saveNumber = mContext.getApplicationContext()
                    .getSharedPreferences(PREFS_NAME, 0);
            SharedPreferences.Editor editorset = saveNumber.edit();
            editorset.putInt("lastsavednumber", 0);
            editorset.commit();
        }
        bm = BitmapFactory.decodeResource(mContext.getResources(),
                images[itemPos]);
        holder.image.setImageBitmap(bm);
    
        SharedPreferences savedNumber = mContext.getSharedPreferences(
                PREFS_NAME, 0);
        int lastSavedNumber = savedNumber.getInt("lastsavednumber", 0);
        lastSavedNumber++;
        String fname = "Image-" + lastSavedNumber + ".png";
        File file = new File(myDir, fname);
        if (file.exists()) {
            file.delete();
        }
        try {
            FileOutputStream out = new FileOutputStream(file);
            bm.compress(Bitmap.CompressFormat.PNG, 100, out);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        SharedPreferences saveNumber = mContext.getApplicationContext()
                .getSharedPreferences(PREFS_NAME, 0);
        SharedPreferences.Editor editorset = saveNumber.edit();
        editorset.putInt("lastsavednumber", lastSavedNumber);
        editorset.commit();
        Toast.makeText(mContext, "Saved", Toast.LENGTH_SHORT).show();
        vi.setTag(holder);
    }
    

2 个答案:

答案 0 :(得分:1)

您可以在保存图像时检查图像是否已存在。如果您使用不同的名称保存图像,则将图像矩阵与保存的图像矩阵进行比较(如果它与任何图像矩阵匹配,则显示Toast消息。

第二,如果文件夹已经存在,那么你没有删除它。当您重新安装应用程序时,文件夹的内容仍保留在那里。

尝试使用此代码检查图像是否相同我将此代码作为SO答案

bool imagesAreEqual(Image i1, Image i2)
{

    if (i1.getHeight() != i2.getHeight) return false;
    if (i1.getWidth() != i2.getWidth) return false;

    for (int y = 0; y < i1.getHeight(); ++y)
       for (int x = 0; x < i1.getWidth(); ++x)
            if (i1.getPixel(x, y) != i2.getPixel(x, y)) return false;

    return true;
}

了解文件夹问题 在您的代码中,如果文件夹存在,则不会删除文件夹。

答案 1 :(得分:1)

要从文件夹中获取最后一个文件名:

String directory = "/parent/of/saved_images/";
File savedImagesDir = new File(directory, "saved_images");
if (savedImagesDir.mkdirs() && savedImagesDir.isDirectory()) {
    // you just created the dir
} else {
    File[] files = savedImagesDir.listFiles();
    if (files == null) {
        // oops savedImagesDir is not a directory
    } else {
        int max = -1;
        Pattern p = Pattern.compile("-[\\d]+");
        for (File file : files) {
            Log.w("file", file.getName());
            Matcher matcher = p.matcher(file.getName());
            if (matcher.find()) {
                final String group = matcher.group();
                final String[] split = group.split("-");
                Log.w("group", file.getName());
                Log.w("split", split[1]);
                int curent = Integer.parseInt(split[1]);
                Log.w("curent", curent + "");
                if (curent > max) max = curent;
            }
        }
        Log.w("max", max + "");
        SharedPreferences saveNumber = mContext.getApplicationContext()
                    .getSharedPreferences(PREFS_NAME, 0);
        SharedPreferences.Editor editorset = saveNumber.edit();
        editorset.putInt("lastsavednumber", 0);
        editorset.commit();
    }
}

卸载后首次运行活动时,您需要执行此操作。当然,如果用户把东西放在那里或者你改变你的命名方案等,我使用的正则表达式可能会失败。你必须小心这样做

对于“不再保存”,您应该(在共享首选项中)存储saved_images中照片的校验和(哈希)。每当你试图把文件放在那里计算它的哈希,如果哈希已经存储,那么显示你的吐司(“图像已经保存”),否则把它放进去。

让您开始使用Java中的哈希值herehere