你读过这个吗https://developer.android.com/training/camera/photobasics.html https://developer.android.com/training/camera/photobasics.html ?
尤其是这部分:
private void galleryAddPic() {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(mCurrentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}
您似乎没有将照片保存在外部存储中,所以它应该可以工作。
编辑:我尝试使用 galleryAddPic 方法按照文档制作一个非常基本的应用程序。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button photoButton = (Button) findViewById(R.id.photobutton);
photoButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
dispatchTakePictureIntent();
}
});
}
static final int REQUEST_IMAGE_CAPTURE = 1;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
//Bundle extras = data.getExtras();
//Bitmap imageBitmap = (Bitmap) extras.get("data");
//mImageView.setImageBitmap(imageBitmap);
galleryAddPic();
}
}
String mCurrentPhotoPath;
private File createImageFile() throws IOException {
File storageDir = Environment.getExternalStorageDirectory();
File image = File.createTempFile(
"example", /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
ex.printStackTrace();
}
if (photoFile != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}
private void galleryAddPic() {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(mCurrentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}
这对我有用!我使用相机应用程序拍照,然后将其显示在图库应用程序中。
我修改了有关 FileProvider 部分的代码,现在它借助 Uri.fromFile(photoFile) 方法传递 Uri。我也将照片保存在不同的位置,请检查 createImageFile 方法中的代码。
在 Android Studio 模拟器中工作正常,请告诉我您的情况。