当我测试app时,我遇到了一个问题。事实是我需要拍照留言。我写了自己的画廊,加载所有图片的方法如下:
@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);
}
当我从相机拍摄照片(第一项)并返回我的画廊时,内容提供商重新加载Loader并向其他人显示我的照片。
为了从相机中获取照片,我使用本教程。 http://developer.android.com/training/camera/photobasics.html#TaskGallery 它运作良好。但是在某些设备上存在照片复制的问题: 当我在模拟器(google nexus 5)上测试它时,一切都很好。但是当我在我的设备Lg G2上测试它时,相机拍摄的图像显示两次。因为Lg和其他设备也使用自己的相机将照片保存到图库。我该如何解决?
我的代码:
@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);
}
答案 0 :(得分:1)
使用Android Gallery是最佳做法!