Preservation of different names
-
How do we keep the photos from the camera?
/DCIM/Camera/
under names received asimage_i
wherei
- these are consistent natural numbers (image_1, image_2, image_3 etc.).Here's 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); }
Code variable
n
- counter. The problem is, when the camera moves back to the main, the counter drops and the new image file replaces the old one.
-
Usually time is used to keep the photo in the name. In fact, it's always unique.
String currentTime = new SimpleDateFormat("yyyy-MM-dd_HHmmss").format(new Date()); File image = new File(imagesFolder, "image_" + currentTime +".jpg");