如何始终保存新名称的形象和删除previouse一(机器人) [英] how to always save image with new name and delete previouse one (android)

查看:193
本文介绍了如何始终保存新名称的形象和删除previouse一(机器人)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作中,我现在的储蓄具有相同名称的图像并将其发送到画廊意图壁纸应用程序。

由于节能与我有每次都得到相同的图像在画廊意图的问题同名每一个形象。

什么发生的事情是新图像替换,但我在画廊意图变老的形象。
新的图像取代旧的图像,但仍意图画廊旧的显示图像,而不是新的图像

所以我想每次保存图像的新名称也删除旧的保存的图像。

请注意:永远保存图像为图像++,也删除previous形象太

我的code:

 公共无效setAsWallpaper(位图位图){        字符串dirname2 =/墙​​纸/;        文件myDir2 =新的文件(Environment.getExternalStorageDirectory()
                .getPath()+ dirname2);        myDir2.mkdirs();        字符串fname2 =形象+.JPG;
        档案文件2 =新的文件(myDir2,fname2);        如果(file2.exists())
            file2.delete();
        尝试{
            FileOutputStream中出=新的FileOutputStream(文件2);
            bitmap.com preSS(Bitmap.Com pressFormat.JPEG,100,出);
            了out.flush();
            out.close();
            成功= TRUE;        }赶上(例外五){
            Toast.makeText(_context,失败,Toast.LENGTH_SHORT).show();
        }        如果(成功){            意向意图=新的Intent();
             intent.setAction(Intent.ACTION_ATTACH_DATA);
             intent.setDataAndType(Uri.parse(文件://
             +/sdcard/Wallpaper/image.jpg),图像/ *);
             intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
             _context.startActivity(意向);        }其他{            Toast.makeText(_context,失败,Toast.LENGTH_SHORT).show();        }    }


解决方案

如果你没有为你发送给您的方法参数图像的原始名称。然后,下面的方法生成随机文件名:

 公共字符串随机的(){
    随机数发生器=新的随机();
    StringBuilder的randomStringBuilder =新的StringBuilder();
    INT randomLength = generator.nextInt(MAX_LENGTH);
    焦炭tempChar;
    的for(int i = 0; I< randomLength;我++){
        tempChar =(char)的(generator.nextInt(96)+ 32);
        randomStringBuilder.append(tempChar);
    }
    返回randomStringBuilder.toString();
}

我通常循环把我的文件夹中的所有文件,并删除所有图像存在(这始终是一个图像)。然后保存新的使用随机名称如下:

 公共无效setAsWallpaper(位图位图){    字符串dirname2 =/墙​​纸/;    文件myDir2 =新的文件(Environment.getExternalStorageDirectory()
            .getPath()+ dirname2);
    //删除文件夹,并在里面的所有文件。然后重新创建它。
    如果(myDir2.exists()){
        的String [] = MYFILES myDir2.list();
         的for(int i = 0; I< myFiles.length;我++){
             myfile文件=新的文件(myDir2,MYFILES [I]);
             myFile.delete();
         }
         myDir2.delete();
    }
    myDir2.mkdirs();    字符串fname2 =随机()+.JPG;
    档案文件2 =新的文件(myDir2,fname2);    如果(file2.exists())
        file2.delete();
    尝试{
        FileOutputStream中出=新的FileOutputStream(文件2);
        bitmap.com preSS(Bitmap.Com pressFormat.JPEG,100,出);
        了out.flush();
        out.close();
        成功= TRUE;    }赶上(例外五){
        Toast.makeText(_context,失败,Toast.LENGTH_SHORT).show();
    }    如果(成功){        意向意图=新的Intent();
         intent.setAction(Intent.ACTION_ATTACH_DATA);
         intent.setDataAndType(Uri.parse(文件://
         +/ SD卡/墙纸/+ fname2),图像/ *);
         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         _context.startActivity(意向);    }其他{        Toast.makeText(_context,失败,Toast.LENGTH_SHORT).show();    }}

i am working on a wallpaper app in which i am saving a image with same name and sending it to gallery intent.

because of saving every image with same name i am having problem of getting same image every time in gallery intent.

what happening is new image is replacing but i am getting older image in gallery intent. new image replace older image but still gallery intent shows older image instead of new image

so i want to save image with new name every time but also delete older saved image.

Note: always save image as image++ but also delete previous image too.

my code:

public void setAsWallpaper(Bitmap bitmap) {

        String dirname2 = "/Wallpaper/";

        File myDir2 = new File(Environment.getExternalStorageDirectory()
                .getPath() + dirname2);

        myDir2.mkdirs();

        String fname2 = "image" + ".jpg";
        File file2 = new File(myDir2, fname2);

        if (file2.exists())
            file2.delete();
        try {
            FileOutputStream out = new FileOutputStream(file2);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
            out.flush();
            out.close();
            success = true;

        } catch (Exception e) {
            Toast.makeText(_context, "failed", Toast.LENGTH_SHORT).show();
        }

        if (success) {

            Intent intent = new Intent();
             intent.setAction(Intent.ACTION_ATTACH_DATA);
             intent.setDataAndType(Uri.parse("file://"
             + "/sdcard/Wallpaper/image.jpg"), "image/*");
             intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
             _context.startActivity(intent);

        } else {

            Toast.makeText(_context, "failed", Toast.LENGTH_SHORT).show();

        }

    }

解决方案

If you don't have an original name for the image that you can send to your method as parameter. Then, the following method to generate random file name:

public String random() {
    Random generator = new Random();
    StringBuilder randomStringBuilder = new StringBuilder();
    int randomLength = generator.nextInt(MAX_LENGTH);
    char tempChar;
    for (int i = 0; i < randomLength; i++){
        tempChar = (char) (generator.nextInt(96) + 32);
        randomStringBuilder.append(tempChar);
    }
    return randomStringBuilder.toString();
}

I usually loops throw all files in my folder and delete all images there(which is always one image). then save the new one with the random name as follow:

public void setAsWallpaper(Bitmap bitmap) {

    String dirname2 = "/Wallpaper/";

    File myDir2 = new File(Environment.getExternalStorageDirectory()
            .getPath() + dirname2);
    // delete folder and all files in it. then re-create it.
    if(myDir2.exists()) {
        String[] myFiles = myDir2.list();  
         for (int i=0; i<myFiles.length; i++) {  
             File myFile = new File(myDir2, myFiles[i]);   
             myFile.delete();  
         }
         myDir2.delete();
    }
    myDir2.mkdirs();

    String fname2 = random() + ".jpg";
    File file2 = new File(myDir2, fname2);

    if (file2.exists())
        file2.delete();
    try {
        FileOutputStream out = new FileOutputStream(file2);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
        out.flush();
        out.close();
        success = true;

    } catch (Exception e) {
        Toast.makeText(_context, "failed", Toast.LENGTH_SHORT).show();
    }

    if (success) {

        Intent intent = new Intent();
         intent.setAction(Intent.ACTION_ATTACH_DATA);
         intent.setDataAndType(Uri.parse("file://"
         + "/sdcard/Wallpaper/" + fname2), "image/*");
         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         _context.startActivity(intent);

    } else {

        Toast.makeText(_context, "failed", Toast.LENGTH_SHORT).show();

    }

}

这篇关于如何始终保存新名称的形象和删除previouse一(机器人)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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