gpt4 book ai didi

android - 从相机中选择照片

转载 作者:行者123 更新时间:2023-11-29 01:30:04 24 4
gpt4 key购买 nike

当我测试应用程序时,我遇到了一个问题。事实是我需要拍照发帖。我写了自己的画廊,加载所有图片的方法如下所示:

    @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);
}

当我从相机拍摄照片(第一项)并返回我的画廊时,内容提供者重新加载加载器并与其他人一起显示我的照片。

为了从相机获取照片,我使用了本教程。 http://developer.android.com/training/camera/photobasics.html#TaskGallery它运作良好。但是在某些设备上存在照片重复的问题: enter image description here当我在模拟器 (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);
}

最佳答案

最好使用 Android Gallery!

关于android - 从相机中选择照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31698502/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com