从外部文件目录获取图片
Posted
技术标签:
【中文标题】从外部文件目录获取图片【英文标题】:Getting a picture from an external file directory 【发布时间】:2018-12-28 23:21:28 【问题描述】:我按照 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;
【问题讨论】:
您知道确切的文件路径,因为您在createImageFile()
中创建了它。如果您特别需要文件中的Bitmap
对象,可以使用BitmapFactory.decodeFile()
。
【参考方案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
【讨论】:
如果我错了,请纠正我,但据我在文档中阅读,这只会给我“小”图像,如我在上面链接的文档中所述“注意:此缩略图从“数据”可能对图标有好处,但不是更多。处理全尺寸图像需要更多工作。但我需要处理完整的照片并确保其分辨率足够高,以便 TextRecognizer 工作。以上是关于从外部文件目录获取图片的主要内容,如果未能解决你的问题,请参考以下文章