Android 如何在安卓Q上向画廊播放照片
我使用这些代码拍摄照片,并将照片广播到gallery,效果很好 我发现我的广播功能使用了Android 如何在安卓Q上向画廊播放照片,android,camera,broadcast,android-10.0,Android,Camera,Broadcast,Android 10.0,我使用这些代码拍摄照片,并将照片广播到gallery,效果很好 我发现我的广播功能使用了MediaStore.Images.ImageColumns.DATA和Intent.ACTION\u MEDIA\u SCANNER\u SCAN\u FILE,这些都是不推荐使用的 如果不使用MediaStore.Images.ImageColumns.DATA和Intent.ACTION\u MEDIA\u SCANNER\u SCAN\u FILE,如何替换这些函数 多谢各位 // set open
MediaStore.Images.ImageColumns.DATA
和Intent.ACTION\u MEDIA\u SCANNER\u SCAN\u FILE
,这些都是不推荐使用的
如果不使用MediaStore.Images.ImageColumns.DATA
和Intent.ACTION\u MEDIA\u SCANNER\u SCAN\u 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(就像您现在从FileProvider获取的一样),并让摄像头应用程序使用它。在清单和运行时代码中不需要写权限
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
处理图片
这些图片位于图片目录中。取消注释其中一个相对路径行以将其放置在其他位置。将创建子目录
适应Q以下的版本。Q上现在会发生什么?你不能告诉我。如果你省略了这一行?@blackapps我的代码现在仍然可以工作,但我担心它明年不会工作,所以我想知道保存照片并向gallery广播的其他方法。Q已经在这里了。你为什么说明年?谈论Q.或P.我尝试了你的方法,它可以拍摄照片并成功保存,但我在图库中找不到照片,我如何将照片广播到图库?它们将在图库中。只需重新启动你的手机。我重新启动手机并在多媒体资料中找到照片,但我无法告诉用户在拍照后,你需要重新启动手机才能在多媒体资料中找到这张照片。是否有任何方法可以让用户在拍照后立即找到照片。是的。你的问题已经在这里被报道过一百次了。还有解决办法。我不知道Q是否也有效。我发现几分钟后图片就会出现在图库中,谢谢!