Save image using the built-in camera

I would like to launch my own camera for Android and save the image in a specific place. The problem is that after I click on the photo, Save / Discard options will appear in the preview. After I click, start the camera again, and the image I captured is not saved in the specified location. Rather, it is stored in the default location. Actually I need the location of the image that I clicked. Here is the code that I use to start the camera.

MediaScannerConnection_MSC = null; String fileName = String.valueOf(System.currentTimeMillis())+".jpg"; f = new File(Environment.getExternalStorageDirectory(), fileName); _imageUri = Uri.fromFile(f); // create new Intent Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); intent.putExtra(MediaStore.EXTRA_OUTPUT, _imageUri); startActivityForResult(intent, 1); 

Here is the code after returning from the camera

 protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == 1) { if (resultCode == RESULT_OK) { // use imageUri here to access the image final String imagePath = f.getAbsolutePath(); _MSC = new MediaScannerConnection(this, new MediaScannerConnectionClient() { public void onMediaScannerConnected() { _MSC.scanFile(imagePath, null); } public void onScanCompleted(String path, Uri uri) { _MSC.disconnect(); _MSC = null; } }); _MSC.connect(); } } } 

what mistake am I making here

+4
source share
1 answer

I think I have your answer, I searched high and low for this, and it is quite difficult.

First of all, I think on some phones

 intent.putExtra(MediaStore.EXTRA_OUTPUT, _imageUri); 

ignored: - (

So, my best answer is for you: do not set the "EXTRA_OUTPUT" flag, instead let it start normally and return your new image URI. Then from there use:

 Uri u = intent.getData(); 

To get information about your new image, from there I use FileOutputStream to write the image file to the new location where I want to use it. Then finally I delete the source file. :-)

I know this is something like hokey, but it works, and I think it is pretty reliable .:-)

Hope this helps. :-)

-Jared

8-28-11

@llango J

Well, here, the whole code, you can copy and paste it and see if you can do it :-)

First you need to call the camera as follows:

 startActivityForResult(CameraIntent, TAKE_PICTURE); 

Then for the result you will need the following:

 @Override public void onActivityResult(int requestCode, int resultCode, Intent intent) { if (resultCode == Activity.RESULT_OK) { switch (requestCode) { case TAKE_PICTURE: Uri u = intent.getData(); //Toast.makeText(getApplicationContext(), u.getPath(), Toast.LENGTH_SHORT).show(); WriteFiles(u); break; } } } 

And then the main magic happens in this function:

 private void WriteFiles(Uri myNewPic) { String TempFilePath; String TempPath; File directory = new File("/sdcard/" + getString(R.string.app_name)); if (!directory.exists()) { directory.mkdirs(); } String TempPictureFile; try { TempPictureFile = myNewPic.getLastPathSegment() + ".jpg"; TempFilePath = directory.getPath() + "/" + TempPictureFile; FileOutputStream myOutStream = new FileOutputStream(TempFilePath); InputStream myInStream = getContentResolver().openInputStream(myNewPic); FileIO myFileIO = new FileIO(); myFileIO.copy(myInStream, myOutStream); //now delete the file after copying getContentResolver().delete(myNewPic, null, null); TempFilePaths.add(TempFilePath); } catch (Exception e) { Toast.makeText(getApplicationContext(), getString(R.string.ErrorOpeningFileOutput), Toast.LENGTH_SHORT).show(); } } 

I think this is all you need to save money using your own camera app. However, I must warn you that I eventually abandoned this code because I needed more flexibility and reliability than suggested (I think if you use this application on a ton of different phones, I could see that he has problems for one reason or another. Like, for example, I had TON problems as a result of Samsung setting up its user interface, so everything works fine on all phones except Samsung .: -P). So I just created my own application for the camera, but I'm sure that depending on your application, this might work for you. Anyway .... good luck! :-)

Ok, here is my FileIO class that I use, sorry, I did not include this initially:

 import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import android.util.Log; public class FileIO { private static final int BUFFER_SIZE = 1024 * 2; public FileIO() { // Utility class. } public int copy(InputStream input, OutputStream output) throws Exception, IOException { byte[] buffer = new byte[BUFFER_SIZE]; BufferedInputStream in = new BufferedInputStream(input, BUFFER_SIZE); BufferedOutputStream out = new BufferedOutputStream(output, BUFFER_SIZE); int count = 0, n = 0; try { while ((n = in.read(buffer, 0, BUFFER_SIZE)) != -1) { out.write(buffer, 0, n); count += n; } out.flush(); } finally { try { out.close(); } catch (IOException e) { Log.e(e.getMessage(), null); } try { in.close(); } catch (IOException e) { Log.e(e.getMessage(), null); } } return count; } } 
+2
source

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


All Articles