Android没有从画廊获得正确的照片

时间:2015-05-15 22:28:54

标签: android imageview gallery onactivityresult

我正在从图库中实现挑选照片,然后在ImageView中显示它。我有一些问题,并且由于这个解决方案:Null Intent passed back on Samsung,部分解决了。我可以拍照并将其保存在自定义位置:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (Helpers.isExternalStorageWritable()) {
    Intent imageIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    File imagesFolder = new File(Environment.getExternalStorageDirectory(), Globals.Constants.APP_NAME);
    imagesFolder.mkdirs();
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmm").format(new Date());
    File image = new File(imagesFolder, "report" + timeStamp + ".jpg");
    Uri uriSavedImage = Uri.fromFile(image);
    imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
    getActivity().startActivityForResult(imageIntent, CAMERA_REQUEST);

然而,在onActivityResult中,当我在ImageView中显示拍摄的照片时,我从旧照片中得到了一张随机照片。这是我的onActivityResult代码:

try {
Log.i("TAG", "inside Samsung Phones");
String[] projection = {
        MediaStore.Images.Thumbnails._ID, // The columns we want
        MediaStore.Images.Thumbnails.IMAGE_ID,
        MediaStore.Images.Thumbnails.KIND,
        MediaStore.Images.Thumbnails.DATA };
String selection = MediaStore.Images.Thumbnails.KIND + "=" + MediaStore.Images.Thumbnails.MINI_KIND;
String sort = MediaStore.Images.Thumbnails._ID + " DESC";
Cursor myCursor = getActivity().getContentResolver().query(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, projection, selection, null, sort);
long imageId = 0l;
long thumbnailImageId = 0l;
String thumbnailPath = "";

try {
    myCursor.moveToFirst();
    imageId = myCursor.getLong(myCursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.IMAGE_ID));
    thumbnailImageId = myCursor.getLong(myCursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID));
    thumbnailPath = myCursor.getString(myCursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA));
} finally {
    // myCursor.close();
}
String[] largeFileProjection = {
        MediaStore.Images.ImageColumns._ID,
        MediaStore.Images.ImageColumns.DATA };
String largeFileSort = MediaStore.Images.ImageColumns._ID + " DESC";
myCursor = getActivity().getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, largeFileProjection, null, null, largeFileSort);
String largeImagePath = "";

try {
    myCursor.moveToFirst();
    largeImagePath = myCursor.getString(myCursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.DATA));
    mImageCaptureUri_samsung = Uri.fromFile(new File(largeImagePath));
    mImageCaptureUri = null;
} finally {
    // myCursor.close();
}

Uri uriLargeImage = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, String.valueOf(imageId));
Uri uriThumbnailImage = Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, String.valueOf(thumbnailImageId));

 image1.setImageURI(uriThumbnailImage);
}

另一个问题是.thumbnail既不会立即显示实际照片,也不会在图库或文件管理器中显示几分钟。但是现在如果有人能帮助我显示随机图片,我会很高兴。先谢谢你了!

0 个答案:

没有答案