How to make the photos taken from the camera be stored in /DCIM/Camera/
under the names obtained as image_i
, where i
are consecutive natural numbers (image_1, image_2, image_3, etc.).
Here is my code:
int n = 0; public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); final Button buttonCheck = (Button)findViewById(R.id.mainButton); buttonCheck.setOnClickListener(new View.OnClickListener(){ public void onClick(View v){ File imagesFolder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM + "/Camera/"); File image = new File(imagesFolder, "image_" + n +".jpg"); Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); Uri uriSavedImage = Uri.fromFile(image); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage); startActivityForResult(cameraIntent, 1112); if(image.exists()){ n++; image = new File(imagesFolder, "image_" + n + ".jpg"); cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); uriSavedImage = Uri.fromFile(image); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage); startActivityForResult(cameraIntent, 1112); } } }); } protected void onActivityResult(int requestCode, int resultCode, Intent data){ super.onActivityResult(requestCode, resultCode, data); Intent intent = new Intent(MainActivity.this, Result.class); startActivity(intent); }
In the code, the variable n
is a counter. The problem is that when you return from the camera activity to the main one, the counter is reset, and the new image file replaces the old one created.
n
static. If you just need to form files in this format, loop throughn
and check that a file with that name does not exist. If you need to find a file with the maximumn
and create a file withn+1
- loop through all the files with the appropriate format, find the maximumn
, etc. The problem is what? - zRrrn
to a text file, and then read it. Since when the application is restarted, the value ofn
reset - Nikito's