1
votes

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

        }

    }
1
thnx for help, now i am saving image using random number or deleting that folder before saving to prevent saving same image again. - android question
I believe that you have to use both together. deleting the folder and use random name. because, the intent will always put the old image. if you did't use random name. delete alone without random name want work. random name alone works. but your folder will keep saving images and get larger. - hasan

1 Answers

0
votes

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

    }

}