title: How to get camera result as a uri in data folder?
body: <p>I am creating an application in which I want to capture a image and then I want to send that image in the email as a attachment.</p>
  
  <p>I am opening a camera using <code>android.provider.MediaStore.ACTION_IMAGE_CAPTURE</code> intent action and I am passing the Uri of the file as a parameter <code>EXTRA_OUTPUT</code> to get the image back to the file. This is working perfectly and I am able to get the captured image if I use the <code>external storage uri</code> as a  <code>EXTRA_OUTPUT</code> but if I use the data folder uri it is not working and the camera is not closing and its all buttons are not working.</p>
  
  <p>Here is my code for get the result in the external storage directory </p>
  
  <pre><code>Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
  File out = Environment.getExternalStorageDirectory();
  out = new File(out, imagename);
  i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(out));
  startActivityForResult(i, CAMERA_RESULT);
  </code></pre>
  
  <p>And this code is for get the image in the data folder</p>
  
  <pre><code>Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
  File out = getFilesDir();
  out = new File(out, MyPharmacyOptions.PRESCRIPTION_IMAGE_NAME);
  i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(out));
  startActivityForResult(i, CAMERA_RESULT);
  </code></pre>
  
  <p>I knew that the <strong>data folder is not accessible to third application</strong> so may be this causes an issue so I have create one content provider to share the file.</p>
  
  <p>Here is my content provide class</p>
  
  <pre><code>public class MyContentProvider extends ContentProvider {
      private static final String Tag = RingtonContentProvider.class.getName();
      public static final Uri CONTENT_URI = Uri
              .parse("content://x.y.z/");
      private static final HashMap&lt;String, String&gt; MIME_TYPES = new HashMap&lt;String, String&gt;();
  
      static {
          MIME_TYPES.put(".mp3", "audio/mp3");
          MIME_TYPES.put(".wav", "audio/mp3");
          MIME_TYPES.put(".jpg", "image/jpeg");
      }
  
      @Override
      public boolean onCreate() {
          return true;
      }
  
      @Override
      public String getType(Uri uri) {
          String path = uri.toString();
  
          for (String extension : MIME_TYPES.keySet()) {
              if (path.endsWith(extension)) {
                  return (MIME_TYPES.get(extension));
              }
          }
  
          return (null);
      }
  
      @Override
      public ParcelFileDescriptor openFile(Uri uri, String mode)
              throws FileNotFoundException {
          File f = new File(getContext().getFilesDir(), uri.getPath());
  
          if (f.exists()) {
              return (ParcelFileDescriptor.open(f, ParcelFileDescriptor.MODE_READ_ONLY));
          }
  
          throw new FileNotFoundException(uri.getPath());
      }
  
      @Override
      public Cursor query(Uri url, String[] projection, String selection,
              String[] selectionArgs, String sort) {
          throw new RuntimeException("Operation not supported");
      }
  
      @Override
      public Uri insert(Uri uri, ContentValues initialValues) {
          File file = new File(getContext().getFilesDir(), uri.getPath());
          if(file.exists()) file.delete();
          try {
              file.createNewFile();
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
  
          return Uri.fromFile(file);
      }
  
      @Override
      public int update(Uri uri, ContentValues values, String where,
              String[] whereArgs) {
          throw new RuntimeException("Operation not supported");
      }
  
      @Override
      public int delete(Uri uri, String where, String[] whereArgs) {
          File f = new File(getContext().getFilesDir(), "image1.jpg");
          if(f.exists()) f.delete();
          f = new File(getContext().getFilesDir(), "image2.jpg");
          if(f.exists()) f.delete();
  
          getContext().getContentResolver().notifyChange(CONTENT_URI, null);
  
      }
  }
  </code></pre>
  
  <p>So to use this content provide I am using following code to pass the uri to the camera activity</p>
  
  <pre><code>Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
  Uri uri = MyContentProvider.CONTENT_URI;
  uri = Uri.withAppendedPath(uri, imagename);
  getContentResolver().insert(uri, null);
  getContentResolver().notifyChange(RingtonContentProvider.CONTENT_URI, null);
  Log.d(Tag, uri.toString());
  i.putExtra(MediaStore.EXTRA_OUTPUT, uri);
  
  startActivityForResult(i, CAMERA_RESULT);
  </code></pre>
  
  <p>Now if I pass the url other then external storage directory the camera is opening but it is not closing in emulator but in device the camera is going to closed but I am not getting the result.</p>
  
  <p>I have declared this content provide in the manifest file </p>
  
  <pre><code>&lt;provider
  android:name=".contentproviders.MyContentProvider"
  android:authorities="x.y.z" /&gt;
  </code></pre>
  
  <p>Also I have given the permission to <strong>write the external storage</strong> and also for <strong>use the camera</strong>.</p>
  
  <p>I am able to capture the image using the external storage but I want to store the image in the data directory instead of external storage because if the external storage in not available I want to capture the image and want to send mail.</p>
  
  <p>If I create content provide then I can also share my image to the email application.</p>
  
  <p><strong>If we not provide the extras with the camera intent it will return the image as a byte[] in the activity result as a data extra but this is for the purpose of the thumbnail so I can't get the high resolution image using this way.</strong></p>