When I tested app I faced with one problem. The fact is that I need take photos for post. I wrote own gallery, the method that loads all the pictures looks like this:
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
String jpgExtension = "jpeg";
String pngExtension = "png";
String jpgMimeType = mimeTypeMap.getMimeTypeFromExtension(jpgExtension);
String pngMimeType = mimeTypeMap.getMimeTypeFromExtension(pngExtension);
Log.d("MIME", jpgMimeType + "; " + pngMimeType);
final String selection = MediaStore.Files.FileColumns.MIME_TYPE + " = ? OR " +
MediaStore.Files.FileColumns.MIME_TYPE + " = ?";
final String[] selectionArgs = new String[] { jpgMimeType, pngMimeType };
final String orderBy = MediaStore.Images.Media.DATE_ADDED + " DESC";
Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String[] projection = new String[] {
MediaStore.Images.ImageColumns._ID,
MediaStore.Images.ImageColumns.ORIENTATION
};
return new CursorLoader(getContext(), uri, projection, selection, selectionArgs, orderBy);
}
When I take photo from camera (first item) and go back to my gallery, the content provider reload Loader and shows my photo with others.
For get photo from camera I use this tutorial.
http://developer.android.com/training/camera/photobasics.html#TaskGallery
It works good. But there is the problem with photo duplicate on some devices:
When I tested it on emulator (google nexus 5) all is fine. But when I tested it on my device Lg G2 the taken image from camera showed twice. Because Lg and others devices uses own camera which saves photo to gallery too. How can I fix it?
My code:
@InstanceState
String mCurrentPhotoPath;
static final int REQUEST_TAKE_PHOTO = 1;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_TAKE_PHOTO) {
galleryAddPic();
}
}
//take photo from camera
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
try {
File photoFile = createImageFile();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
} catch (IOException ex) {
.....
}
}
}
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
return image;
}
private void galleryAddPic() {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File photoFile = new File(mCurrentPhotoPath);
Uri contentUri = Uri.fromFile(photoFile);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}
Using Android Gallery is the best practice!