我按照Android开发人员文档的Take photo指南拍照。我想将此图片用于Firebase MLKit,但是我只是临时需要它,所以我将其保存到我的私人存储中,并在使用后删除(提取文本)。
因此,我正在使用一个Intent来为我进行捕获并将其保存到特定文件中。 如果Intent完成,如何提取图片,以便将位图传递给Firebase API?
在前面提到的文档中,它说
注意:如果将照片保存到getExternalFilesDir()提供的目录中,则媒体扫描器将无法访问文件,因为它们是应用程序专用的文件。
但是它没有说明如何访问该文件。这是我的问题:如何访问?
对于到目前为止对我的代码感兴趣的所有人:
public class MainActivity extends AppCompatActivity {
static final int REQUEST_TAKE_PHOTO = 1;
private Uri photoURI;
//ToDo: delete picture at the end of app usage [finally->delete]
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
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
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
//ToDo:Logging/Toast msg
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (photoFile != null) {
photoURI = FileProvider.getUriForFile(this,
"mypackagename",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
} else {
//ToDo: logging/Toast msg
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == Activity.RESULT_OK && requestCode == REQUEST_TAKE_PHOTO){
//This is where I need to access the picture I saved.
}else{
//ToDo: logging/Toast msg
}
}
public void scanText(View view) {
//Firebase stuff, not relevant
}
String mCurrentPhotoPath;
private File createImageFile() throws IOException {
// Create an image file name
@SuppressLint("SimpleDateFormat") String timeStamp = new SimpleDateFormat
("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
}
答案 0 :(得分:1)
添加此行以获取图像位图
if(resultCode == Activity.RESULT_OK && requestCode == REQUEST_TAKE_PHOTO)
{
//This is where I need to access the picture I saved.
// get the image bitmap from this code
Bitmap imageBitmap = (Bitmap) data.getExtras().get("data");
}
else
{
//ToDo: logging/Toast msg
}