gpt4 book ai didi

android - 如何在 Android Q 上将照片广播到图库

转载 作者:行者123 更新时间:2023-12-05 00:13:45 24 4
gpt4 key购买 nike

我使用这些代码拍照并将照片广播到画廊,它有效。

我发现我的广播功能使用了 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);
}

最佳答案

无需先将图片文件保存在某处,然后再尝试将其插入 MediaStore。您可以从 MediaStore 获取一个 uri(就像您现在从 FileProvder 获取它一样)并让相机应用程序使用它。 list 和运行时代码中无需 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以下版本

关于android - 如何在 Android Q 上将照片广播到图库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59187727/

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