Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace and increment the number in the filename in an Android app

Please see below (both) answers for solution.


I can't seem to get my logic right, and for some reason this has really got me, yet it seems (and probably is) so simple. What I'd like to do is to capture an image, and, if it exists, increment the number. I.e. photo1.jpg exists, so save new file as photo2.jpg, etc.

At the moment when I run my code, and I take a picture, "photo.jpg" is saved, then with the next capture, "photo1.jpg" is saved, then "photo11.jpg", and then "photo111.jpg", etc.

Here is my code:

        String photoName = "photo.jpg";
        String i = "0";
        int num = 0;

        File photo = new File(Environment.getExternalStorageDirectory(), photoName);

        while(photo.exists()) {             
            //photo.delete();
            num = Integer.parseInt(i);
            num++;
            String concatenatedNum = Integer.toString(num);
            StringBuffer insertNum = new StringBuffer(photoName);
            photoName = insertNum.replace(5, 5, concatenatedNum).toString();
                                //insert

            photo = new File(Environment.getExternalStorageDirectory(), photoName);
        }

        FileOutputStream fostream = null;

        try {
            fostream = new FileOutputStream(photo.getPath());

            //MediaStore.Images.Media.insertImage(getContentResolver(), yourBitmap, yourTitle, yourDescription);

            //write jpeg to local drive
            fostream.write(jpeg[0]);
            fostream.close();
        }
        catch (IOException e) { e.printStackTrace(); }
        finally
        {
            if(fostream != null)
            {
                try
                {
                    fostream.flush();
                    fostream.close();
                }
                catch(IOException e)
                {
                    e.printStackTrace();
                }
            }
        }

Any help is much appreciated.

Thanks.


I've implemented the below solution, and now I only end up with 2 files no matter how many images I capture. 'photo.jpg' and 'photo1.jpg' were successfully saved, but no other image was saved. They're not even being written over. Any help?

Code now follows:

        String photoName = "photo.jpg";
        String i = "0";
        int num = 0;

        File photo = new File(Environment.getExternalStorageDirectory(), photoName);

        while(photo.exists()) {             
            //photo.delete();
            num = Integer.parseInt(i);
            num++;      
            photoName = "photo" + num + ".jpg";
            photo = new File(Environment.getExternalStorageDirectory(), photoName);
            //String concatenatedNum = Integer.toString(num);
            //StringBuffer insertNum = new StringBuffer(photoName); 

            //photoName = insertNum.insert(5, concatenatedNum).toString();

        }
like image 338
LKB Avatar asked Jan 13 '23 07:01

LKB


2 Answers

Replace your while loop with this:

   while(photo.exists()) {             
        num++;
        photoName = "photo"+num+".jpg";
        photo = new File(Environment.getExternalStorageDirectory(), photoName);
   }
like image 183
xagyg Avatar answered Jan 17 '23 14:01

xagyg


For the new solution - Why do you have the following line?

num = Integer.parseInt(i);

i never changes, so num also never changes. It looks like the code will just infinitely loop. It should work if you remove that line.

like image 30
Ren Avatar answered Jan 17 '23 14:01

Ren