Question of keeping the photo



  • This program keeps the camera photos in the folder. Pictures/Faces , giving the name to each of them on principle, "face_" + n + ".jpg" where n is the variable that was obtained from the schet.dat file (this file is also being created at the beginning. Its values are increased by 1, with each photograph retained. I managed to keep the photos I needed, but there's one problem. For example, the user took the first three photos, they were retained as face_1.jpg, face_2.jpg and face_3.jpg. It's okay, but when the user removes all the photos from the photos. Pictures/Faces , they continue to be face_4.jpg, face_5.jpg, face_6.jpg, etc. Okay. I've set up a condition checking whether the Faces file is empty, but it doesn't help. Please help.

    public class MainActivity extends Activity implements ImgPath
    {
    
    public void onCreate(Bundle savedInstanceState){
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        imagesFolder.mkdir();
    
        Button buttonCheck = (Button)findViewById(R.id.mainButton);
        buttonCheck.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    MyappDir.mkdir();
                    File schet = new File(MyappDir, "schet.dat");
                    if(imagesFolder == null | schet == null){ 
                        DataOutputStream out = null; 
                        try { 
                            Integer n = Integer.valueOf(1);
                            out = new DataOutputStream(new FileOutputStream(schet));
                            out.writeInt(n.intValue()); 
                            out.close();
                            File image = new File(imagesFolder, "face_" + n + ".jpg");
                            Uri uriSavedImage = Uri.fromFile(image);
                            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
                            startActivityForResult(cameraIntent, 1112);
                        } catch (Exception e) {
                            e.printStackTrace(); 
                        } 
                    } else {
                        Integer n = 0;
                        DataInputStream in = null; 
                        try { 
                            in = new DataInputStream(new FileInputStream(schet)); 
                            n = Integer.valueOf(in.readInt());
                            in.close();
                            n++;
                            File image = new File(imagesFolder, "face_" + n + ".jpg");
                            Uri uriSavedImage = Uri.fromFile(image);
                            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
                            startActivityForResult(cameraIntent, 1112);
                        } catch (Exception e) { 
                            e.printStackTrace();  
                        }
                        DataOutputStream out = null; 
                        try { 
                            out = new DataOutputStream(new FileOutputStream(schet));
                            out.writeInt(n.intValue()); 
                            out.close();
                        } catch (Exception e) {
                            e.printStackTrace(); 
                        } 
                    }
                }
        });
    }
    
    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        super.onActivityResult(requestCode, resultCode, data);
        Intent intent = new Intent(MainActivity.this, Result.class);
        startActivity(intent);
    }
    

    }



  • You have a condition. if(imagesFolder == null | schet == null) I'm checking on the folder only and if the file is schet.dat♪ I mean, it only works once.

    You're gonna have to another check on the number of elements in the folder:

    if(imagesFolder == null || imagesFolder.listFiles().length == 0 || schet == null){ 
      // 
    }
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2