After shooting, the picture is not saved

I can press a button, open my own application for the camera and successfully take a picture. But then, when I view my own Gallery or Photos apps on my phone, the image is not saved there. I am very new to Android, so most likely I am missing something important in my code.

Questions:

1) Where are these images stored?

2) Can I somehow modify the code below to save the internal storage instead, so all the pictures taken with my application are private and available only through my application?

3) If I wanted to save these pictures in the object, as well as some text / other input, what would be the best way? Should I just save the Uri or some identifier for the image link later or save the actual BitMap image?

Any help is much appreciated, thanks!

Here is my code to take a picture:

 mImageButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); imageUri = CameraUtils.getOutputMediaFileUri(CameraUtils.MEDIA_TYPE_IMAGE); intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); startActivityForResult(intent, REQUEST_IMAGE); } } 

CameraUtils class, taken directly from the Google Developer's Guide :

 public static Uri getOutputMediaFileUri(int type) { return Uri.fromFile(getOutputMediaFile(type)); } public static File getOutputMediaFile(int type) { File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES), "camera"); if (!mediaStorageDir.exists()) { if (!mediaStorageDir.mkdirs()) { return null; } } String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); File mediaFile; if (type == MEDIA_TYPE_IMAGE) { mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg"); } else if(type == MEDIA_TYPE_VIDEO) { mediaFile = new File(mediaStorageDir.getPath() + File.separator + "VID_" + timeStamp + ".mp4"); } else { return null; } return mediaFile; } 
+6
source share
1 answer

1) Having looked at the code, I expect that the pictures will be saved in a directory called β€œcamera”, which will be found in the β€œImages” folder on your device (external storage). They may not appear immediately in your gallery, but in later versions of Android (Kitkat and possibly jellybean, although I can’t check it right now) you should be able to open the Photos app and find them somewhere there . If this is not the case, launch the application for the file explorer (for example, the application - ASTRO File Manager or X-Plore) and go to the "Pictures / Cameras" directory, where you will see your images. The next time your media is reindexed (rebooting the phone or re-indexing from other sources), you should see these photos in your gallery / photo applications. If you want to update media files programmatically, this may help here . Lastly, make sure you have READ_EXTERNAL_STORAGE permission in your Android manifest, as indicated by this (Android doc).

2) If you want to save images only for your application, you need to save them in your internal application data directory. Take a look at this right from your Android doc. Be sure to use the MODE_PRIVATE flag.

3) To do this, you need to save the file path available for your application. Either you can save your paths to a text file with some other text data, or use the sqlite database. Finally, you can use ORM, such as ORMLite for Android, to save a java object that can contain data for your image, and have some fields that you want to save (name, description, path, etc.). Here and here is an introduction to getting started with the SQLite database on Android (directly from the white paper). If you want to use ORMLite, there is a lot of information on their site here . The developer spent a lot of time answering StackOverflow questions.

All your questions can be answered with a few simple Google searches. They are very standard and basic things that you can do on Android, so you have to find a lot of information and tutorials on the Internet.

EDIT :

In response to your comment on the second question. This is what I will probably do (or something similar):

Please note that I have not tested this. This is from the head. If you have more comments about comments!

Operation code...

 mImageButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); imageUri = CameraUtils.getOutputMediaFileUri(currentActivity, CameraUtils.MEDIA_TYPE_IMAGE); intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); startActivityForResult(intent, REQUEST_IMAGE); } } public void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_IMAGE) { if (resultCode == RESULT_OK) { String pathToInternallyStoredImage = CameraUtils.saveToInternalStorage(this, imageUri); // Load the bitmap from the path and display it somewhere, or whatever } else if (resultCode == RESULT_CANCELED) { //Cancel code } } } 

CameraUtils Class Code ...

 public static Uri getOutputMediaFileUri(int type) { return Uri.fromFile(getOutputMediaFile(type)); } public static File getOutputMediaFile(int type) { File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES), "camera"); createMediaStorageDir(mediaStorageDir); return createFile(type, mediaStorageDir); } private static File getOutputInternalMediaFile(Context context, int type) { File mediaStorageDir = new File(context.getFilesDir(), "myInternalPicturesDir"); createMediaStorageDir(mediaStorageDir); return createFile(type, mediaStorageDir); } private static void createMediaStorageDir(File mediaStorageDir) // Used to be 'private void ...' { if (!mediaStorageDir.exists()) { mediaStorageDir.mkdirs(); // Used to be 'mediaStorage.mkdirs();' } } // Was flipped the other way private static File createFile(int type, File mediaStorageDir ) // Used to be 'private File ...' { String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); File mediaFile = null; if (type == MEDIA_TYPE_IMAGE) { mediaFile = new File(mediaStorageDir .getPath() + File.separator + "IMG_" + timeStamp + ".jpg"); } else if(type == MEDIA_TYPE_VIDEO) { mediaFile = new File(mediaStorageDir .getPath() + File.separator + "VID_" + timeStamp + ".mp4"); } return mediaFile; } public static String saveToInternalStorage(Context context, Uri tempUri) { InputStream in = null; OutputStream out = null; File sourceExternalImageFile = new File(tempUri.getPath()); File destinationInternalImageFile = new File(getOutputInternalMediaFile(context).getPath()); try { destinationInternalImageFile.createNewFile(); in = new FileInputStream(sourceExternalImageFile); out = new FileOutputStream(destinationInternalImageFile); // Transfer bytes from in to out byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } } catch (IOException e) { e.printStackTrace(); //Handle error } finally { try { if (in != null) { in.close(); } if (out != null) { in.close(); } } catch (IOException e) { // Eh } } return destinationInternalImageFile.getPath(); } 

So now you have a path pointing to your internally saved image, which you can then manipulate / load from your onActivityResult.

+4
source

Source: https://habr.com/ru/post/976321/


All Articles