我是Android编程的新手,我需要编写一个应用程序,拍摄一张照片然后对其进行处理。 https://developer.android.com/training/camera/photobasics-我复制了本教程中的步骤,并使用标准的相机应用程序拍照。当我运行相机应用程序时,我将来自外部私人存储的文件传递给相机,相机应将图片写入该文件。一切正常,我可以拍照,应用程序返回我的应用程序中的活动,但是现在我不知道如何获取这张照片。我有一个应该存储图像的文件的绝对路径,但是本教程并未涵盖我应该读取此文件的方式。教程提到了一个名为 openFileInput()的函数,我尝试使用该函数,但是当我向其传递绝对路径时,我得到了一个异常,该异常告诉我传递给它的String对象中存在路径中断符。 / p>
String mCurrentPhotoPath;
/*Function creates a file in external private storage and stores path to it
in mCurrentPhotoPath*/
private File createImageFile() throws IOException {
// Create an image file name
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;
}
/*reakcjaZdjecie is called when user clicks button "Take a picture!"*/
public void reakcjaZdjecie(View widok) {
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) {
// Error occurred while creating the File
Log.i("Pictures", "Couldn't create file");
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.example.android.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch(requestCode)
{
case REQUEST_TAKE_PHOTO:
Log.d("Pobrano zdjecie", mCurrentPhotoPath);
try{
openFileInput(mCurrentPhotoPath);
}catch(FileNotFoundException e)
{
Log.d("Result", "Couldn't find file");
e.getStackTrace();
}
break;
}
}
}