如何在Android Q上将照片广播到画廊

时间:2019-12-05 03:19:36

标签: android camera broadcast android-10.0

我使用这些代码拍摄照片并将照片广播到画廊,

我发现我的广播功能使用MediaStore.Images.ImageColumns.DATAIntent.ACTION_MEDIA_SCANNER_SCAN_FILE,但已弃用!

如何在不使用MediaStore.Images.ImageColumns.DATAIntent.ACTION_MEDIA_SCANNER_SCAN_FILE的情况下替换这些功能。

谢谢。

// set open camera button.
private void setCameraButton() {
    cameraBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ActivityCompat.requestPermissions(PostActivity.this, mPermissionList, CAMERA_REQUEST_PERMISSION);
        }
    });
}


@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode){
        case 100: // camera.
            if(grantResults.length > 0){
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED)
                    openCameraIntent();
                else
                    Toast.makeText(this, "Please set permissions.", Toast.LENGTH_SHORT).show();
            }
            break;

            ...
        }
    }

private void openCameraIntent() {
    Intent pictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (pictureIntent.resolveActivity(getPackageManager()) != null) {
        String time = MyUtility.getSystemTaiwanDateAndTime();
        photoFileName = "IMG_" + time;

        File storageDir = new File(this.getExternalFilesDir(null).getAbsolutePath(), "myapp");
        MyUtility.createAppFolder(storageDir); // create app folder.

        photoFile = new File(this.getExternalFilesDir(null).getAbsolutePath(), "/myapp/" + photoFileName + ".jpg");

        Uri photoUri = FileProvider.getUriForFile(this, getPackageName() + ".provider", photoFile);
        pictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
        startActivityForResult(pictureIntent, IMAGE_CAPTURE_REQUEST_CODE);
    }
}

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

    if (requestCode == IMAGE_CAPTURE_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
        photoFilePath = photoFile.getAbsolutePath();
        broadcastAddPhotoToGallery(photoFile, photoFileName, photoFilePath); // Want to use this function to broadcast photo to gallery.

        ...
    }
        ...
}

private void broadcastAddPhotoToGallery(File _file, String _fileName, String _filePath) {

    int sdkVersion = Build.VERSION.SDK_INT;
    if(sdkVersion >= 28) 
        galleryAddPhotoAboveApi28(_fileName, _filePath);
    else 
        galleryAddPhotoBelowApi28(String.valueOf(_file));
}


private void galleryAddPhotoAboveApi28(String _fileName, final String _filePath) {

    OutputStream out = null;
        try {
            ContentValues values = new ContentValues();
            ContentResolver resolver = this.getContentResolver();

            values.put(MediaStore.Images.ImageColumns.DATA, _filePath); // MediaStore.Images.ImageColumns.DATA is deprecated!
            values.put(MediaStore.Images.ImageColumns.DISPLAY_NAME, _fileName);
            values.put(MediaStore.Images.ImageColumns.MIME_TYPE, "image/jpg");
            values.put(MediaStore.Images.ImageColumns.DATE_TAKEN, System.currentTimeMillis() + "");
            Uri uri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
            if (uri != null) {
                Bitmap bitmap = BitmapFactory.decodeFile(_filePath);
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
                out.flush();
                out.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

private void galleryAddPhotoBelowApi28(String _file) {
        File f = new File(_file);
        Uri contentUri = Uri.fromFile(f); 
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, contentUri); // Intent.ACTION_MEDIA_SCANNER_SCAN_FILE is deprecated!
        sendBroadcast(mediaScanIntent);
    }

1 个答案:

答案 0 :(得分:0)

无需先将图片文件保存在某个地方,然后再尝试将其插入MediaStore中。您可以从MediaStore中获取一个uri(就像现在从FileProvder中获取的一样),然后让Camera应用程序使用它。清单和运行时代码中不需要WRITE权限。

        if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
                {
                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

                    ContentValues values = new ContentValues(3);

                    String displayName =  "FromTakepicture." + System.currentTimeMillis() + ".jpg";

                    values.put(MediaStore.Images.Media.DISPLAY_NAME, displayName);
                    values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");

                    //values.put(MediaStore.Images.Media.RELATIVE_PATH, "DCIM/playground");
                    //values.put(MediaStore.Images.Media.RELATIVE_PATH, "Pictures/playground");

                    insertDisplayName = displayName;
                    insertUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

                    intent.putExtra(MediaStore.EXTRA_OUTPUT, insertUri);

                    startActivityForResult(intent, PICK_PHOTO_CODE_URI);

                    return;
                }

在onActivityResult()中使用insertUri处理图片。

图片落在“图片”目录中。取消注释RELATIVE_PATH行之一以将它们放在其他位置。子目录将创建。

适应低于Q的版本。