【问题标题】:Choose photo from camera从相机中选择照片
【发布时间】:2015-10-20 07:39:28
【问题描述】:

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

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

【问题讨论】:

    标签: android image camera android-contentprovider android-gallery


    【解决方案1】:

    使用 Android Gallery 是最佳做法!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-04
      • 2016-08-18
      • 1970-01-01
      • 2013-07-23
      • 1970-01-01
      • 1970-01-01
      • 2013-11-23
      • 2010-11-11
      相关资源
      最近更新 更多