【问题标题】:why does mediastore take the last-1th picture from the gallery为什么 mediastore 会从图库中获取最后一张图片
【发布时间】:2015-02-25 14:52:08
【问题描述】:

你好! 当我在片段中使用相机意图时,我的 Android 应用程序中的图片库和媒体存储之间存在问题。 我查看了 android 网站以了解如何在片段中拍照,并在一些论坛上了解如何获取最后一张照片,但媒体商店总是给我最后一张照片,好像它无法找到最后一张照片。

这是我的代码:

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    File photoFile = null;
    try {
        photoFile = createImageFile();
    } catch (IOException ex) {
        Log.i(LOG_TAG,"ERRRRROR: "+ex.toString());
    }
    if (photoFile != null) {
        mCurrentPhotoPath = "file:" +photoFile.getAbsolutePath();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(photoFile));
        startActivityForResult(takePictureIntent, REQ_CAMERA_PICTURE);

    }
}

private void galleryAddPic() {
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    File f = new File(mCurrentPhotoPath);
    Uri contentUri = Uri.fromFile(f);
    mCurrentPhotoUri = contentUri;
    mediaScanIntent.setData(contentUri);
    getActivity().sendBroadcast(mediaScanIntent);
    Log.d(LOG_TAG,"Photo SAVED");
}


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 */
    );

    mCurrentPhotoAbsolutePath = image.getAbsolutePath();

    Log.i(LOG_TAG,"image path : "+image.getAbsolutePath());

    return image;
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == REQ_CAMERA_PICTURE)
    {
        if (resultCode == Activity.RESULT_OK)
        {
            galleryAddPic();
            String filePath = getActivity().getPreferences(Context.MODE_PRIVATE).getString(TMP_PHOTO_FILE_LATEST_KEY, null);
            handleCameraPicture(mCurrentPhotoUri.toString());
        }
        else
        {
            clearCurrentActionData();
        }
    }
}
  private String getLastImagePath() {
    String[] projection = new String[]{
            MediaStore.Images.ImageColumns._ID,
            MediaStore.Images.ImageColumns.DATA,
            MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME,
            MediaStore.Images.ImageColumns.DATE_TAKEN,
            MediaStore.Images.ImageColumns.MIME_TYPE
    };
    final Cursor cursor = getActivity().getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null,null, MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC");


    if (cursor.moveToFirst()) {
        String imageLocation = cursor.getString(1);
        File imageFile = new File(imageLocation);
        Bitmap bm = BitmapFactory.decodeFile(imageLocation);
        return imageFile.getPath().toString();
    }
    else{
        return "";
    }

}

当我尝试获取最后一个图像路径时,问题出现在这里

private void handleCameraPicture(String pictureFileUri)
{
    _currentDataObjectBuilder.addParameter(DataObject.Parameter.IMAGE, String.valueOf(getFileCount()));

    //create a copy of the file into the internal storage
    final File pictureFile = new File(pictureFileUri);

    Log.d(LOG_TAG,"picture file uri : "+pictureFileUri.toString());

    FileOutputStream fos =null;
    byte[] pictureData = new byte[0];
    try
    {

        pictureData = compressImage(getLastImagePath());
        fos = getActivity().openFileOutput(String.valueOf(getFileCount()), Context.MODE_PRIVATE);
        fos.write(pictureData);
        Log.i(LOG_TAG,"SETTING FILE OUTPUT STREAM");
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    finally {
        try {
            fos.close();
            pictureFile.delete();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    if (_currentAction.equals(Action.PICTURE_CAPTION) || _currentAction.equals(Action.ARCHIVE))
    {
        showMessageView();
    }
    else
    {
        sendCurrentActionData();
    }
}

handleCameraPicture 函数允许我将“最后一张图片”发送到外部网站。

我不知道我做错了什么,所以请帮助我!否则我会失去理智...

谢谢!

托马斯

【问题讨论】:

    标签: android android-fragments android-camera mediastore


    【解决方案1】:

    哇! 我终于找到了解决方案,实际上我创建图片时的uri与我尝试将图片保存在图库中时的uri不同,所以我的光标掉了一个空指针异常。

    我将 GalleryAddPic 函数更新为

    private void galleryAddPic(Uri uri) {
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        mCurrentPhotoUri = uri;
        mediaScanIntent.setData(uri);
        getActivity().sendBroadcast(mediaScanIntent);
        Log.d(LOG_TAG,"Photo SAVED");
    }
    

    现在一切正常!

    【讨论】:

      猜你喜欢
      • 2012-02-10
      • 2013-07-27
      • 1970-01-01
      • 1970-01-01
      • 2011-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-22
      相关资源
      最近更新 更多