且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

prevent复制的图像保存到SD卡

更新时间:2022-10-20 20:18:31

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

 字符串目录=/父母/中/ 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