prevent复制的图像保存到SD卡 [英] Prevent duplicated image saved to SD card

查看:120
本文介绍了prevent复制的图像保存到SD卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个listactivity应用形成多行。每一行打开包含的意见,其中之一是一个按钮,点击打开无限画廊上课的时候​​一个活动(存储在RES图像=内部应用程序>绘制文件夹),每个图像都有其下按钮,当pressed这样可以节省图像到SD卡目录下的文件夹名为(saved_images)。

我用共享preferences 在画廊类来存储顺序的所有图像,工作正常 -

但是我想:


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

    假设你保存的图像-1成功,那么你preSS下的图像-1相同的按钮,它会再次在SD卡的文件夹中保存,所以最后你会拥有相同的图像(图像-1)的两倍,

    所以,我想:当我preSS按钮下的图像已经保存已保存必须上升举杯的形象,因此,所有的应用程序的图像将SD卡的文件夹曾经救过


  2. 一保持后重装节省顺序图片:

    在安装设备的应用程序和文件夹中保存一些图像后(saved_images)
    这已在SD卡中创建,假设你卸载从设备应用程序,并保持
    在SD卡(saved_images)文件夹,然后重新安装应用程序,并要保存
    一些新的图像,会发生什么是新的图像替换previously保存的图像,

    但是我希望它:继续保存新的图像与previous顺序保存的图像


code用来将图像保存到SD卡:

 公共无效的onClick(查看为arg0){
    。字符串根= Environment.getExternalStorageDirectory()的toString();
    文件MYDIR =新的文件(根+/ saved_images);
    如果(!myDir.exists()){
        myDir.mkdirs();
        共享preferences saveNumber = mContext.getApplicationContext()
                .getShared preferences(preFS_NAME,0);
        共享preferences.Editor editorset = saveNumber.edit();
        editorset.putInt(lastsavednumber,0);
        editorset.commit();
    }
    BM = BitmapFactory.de codeResource(mContext.getResources()
            图像[itemPos]);
    holder.image.setImageBitmap(BM);    共享preferences savedNumber = mContext.getShared preferences(
            preFS_NAME,0);
    INT lastSavedNumber = savedNumber.getInt(lastsavednumber,0);
    lastSavedNumber ++;
    字符串FNAME =图像 - + lastSavedNumber +的.png;
    档案文件=新的文件(MYDIR,FNAME);
    如果(file.exists()){
        file.delete();
    }
    尝试{
        FileOutputStream中出=新的FileOutputStream(文件);
        bm.com preSS(Bitmap.Com pressFormat.PNG,100,出);
        了out.flush();
        out.close();
    }赶上(例外五){
        e.printStackTrace();
    }
    共享preferences saveNumber = mContext.getApplicationContext()
            .getShared preferences(preFS_NAME,0);
    共享preferences.Editor editorset = saveNumber.edit();
    editorset.putInt(lastsavednumber,lastSavedNumber);
    editorset.commit();
    Toast.makeText(mContext,拯救,Toast.LENGTH_SHORT).show();
    vi.setTag(保持器);
}


解决方案

要在文件夹中获得最后的文件名,你可以:

 字符串目录=/父母/中/ saved_images /;
文件savedImagesDir =新的文件(目录saved_images);
如果(savedImagesDir.mkdirs()及&放大器; savedImagesDir.isDirectory()){
    //您刚才创建的目录
}其他{
    文件[] =文件savedImagesDir.listFiles();
    如果(文件== NULL){
        //哎呀savedImagesDir不是目录
    }其他{
        INT最大= -​​1;
        模式p = Pattern.compile( - [\\\\ D]。+);
        对于(文件文件:文件){
            Log.w(文件,file.getName());
            匹配器匹配= p.matcher(file.getName());
            如果(matcher.find()){
                最后弦乐组= matcher.group();
                最终的String [] =拆分group.split( - );
                Log.w(本集团,file.getName());
                Log.w(分裂,分裂[1]);
                INT CURENT =的Integer.parseInt(分割[1]);
                Log.w(当前的,CURENT +);
                如果(CURENT>最大)最大= CURENT;
            }
        }
        Log.w(MAX,MAX +);
        共享preferences saveNumber = mContext.getApplicationContext()
                    .getShared preferences(preFS_NAME,0);
        共享preferences.Editor editorset = saveNumber.edit();
        editorset.putInt(lastsavednumber,0);
        editorset.commit();
    }
}

您需要做的,只要你先卸载后运行您的活动。当然,常规的前pression我用,如果用户把东西有可能会失败,或者如果你改变你的命名方案等,你必须这样做仔细

至于不节能再次你应该存储(共享preferences)在saved_images照片的校验和(哈希值)。当你试图把一个文件在那里计算其哈希值,如果哈希已经存储然后显示你的面包(已保存图像),否则把它放在

为您开始在Java看看哈希这里这里

I have a listactivity app forming multiple rows. Each row opens an activity containing views, one of them is a button, when clicked open infinite gallery class ( images stored in RES => drawable folder inside the app), each image has button under it, when pressed it saves the image to SD card directory in a folder named ( saved_images ) .

I'm using SharedPreferences in gallery class to store all the images in sequential order, that works fine -

but I'm trying to :

  1. Prevent a repeat of images saved in the SD Card folder (saved_images):

    Say you saved image-1 successfully then you press the same button under image-1 it will be saved again in SD card folder so finally you will have same image (image-1) twice ,

    so what i want to get : when i press a button under image already saved a Toast 'image already saved must rise, so all app images will be saved once in Sd card folder.

  2. Keep saving images in sequential order after a reinstall:

    after installing the app in device and saving some images in folder ( saved_images ) which already created in SD card , suppose you uninstall the app from device and keep the ( saved_images ) folder in SD card , then reinstall the app again and want to save some new images, what happens is the new images replace the previously saved images,

    but I want it to : continue saving new images with previous saved images in sequential order.

Code used to save images to SDcard :

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);
}

解决方案

To get the last filename from the folder you could :

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();
    }
}

You need to do this whenever you first run your activity after an uninstall. Of course the regular expression I used could fail if the user puts stuff there or if you change your naming scheme etc. You must do this carefully

As for the "not saving again" you should store (in shared preferences) a checksum (hash) of the photos in the saved_images. Whenever you try to put a file in there calculate its hash and if the hash is already stored then display your toast ("image already saved") otherwise put it in.

To get you started with hashes in java look here and here

这篇关于prevent复制的图像保存到SD卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆