我有两个活动:MainActivity和ShowPhotoDescriptionActivity。第一个活动有一个FloatingActionButton,当按下时,它启动相机,拍摄照片,并将其保存到该应用程序的文件夹中。之后,第二个Activity被调用。此活动应显示在ImageView中拍摄的最后一张照片。它看起来很简单,但没有用。 我已经尝试了一些解决方案,但其中大多数都没有解决我的任何问题。拍照并在另一个活动的ImageView中显示它
在MainActivity我有
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Starts the device's camera
dispatchTakePicture();
}
});
/**
* Calls an existent camera app to take a picture, then is stores on device in a custom folder
*/
private void dispatchTakePicture() {
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 {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "photocrowd" + timeStamp + ".jpg";
final String appDirectoryName = "Photocrowd";
photoFile = createImageFile(appDirectoryName, imageFileName);
} catch (IOException ex) {
// Error occurred while creating the File
Toast.makeText(this, "Ocorreu um erro: Não foi posível armazenar a foto", Toast.LENGTH_SHORT).show();
Log.e("Main Activity", "It wasn't possible to catch the photo: "+ex);
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = Uri.fromFile(photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
/**
* Creates a folder and a name to the image on the device
*
* @return
* @throws IOException
*/
private File createImageFile(String name_folder, String name_image) throws IOException {
// Create an image file name
final File imageRoot = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), name_folder);
imageRoot.mkdir();
File image = new File(imageRoot, name_image);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
/**
* Allows the photo be found for the media scanner and shown in gallery
*/
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);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
galleryAddPic();
//Once the photo is taken and saved, the description activity is called
Intent i = new Intent(this, PhotoDescriptionActivity.class);
i.putExtra("path", mCurrentPhotoPath);
startActivity(i);
}
}
好吧,这只是允许拍照,保存的文件名为“Photocrowd”文件夹中,其显示在画廊和发送路径其他活动。
在PhotoDescriptionActivity我有
private ImageView mImageView;
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.show_photo_description_activity);
Log.v("DESCRIPTION", "Activite PHotoDescription");
mImageView = (ImageView) findViewById(R.id.photo_panel);
String imageRoot = getIntent().getStringExtra("path");
Log.v("DESCRIPTION", imageRoot);
mImageView.setImageBitmap(BitmapFactory.decodeFile(imageRoot));
}
我已经尝试使用其他方法(使用图书馆,setImageUri及其他)。到imageRoot路径是相同的显示在我的Android(/storage/emulated/0/Pictures/Photocrowd/photocrowd20170419_124851.jpg
)
此代码的照片说明是最后一个,我试图和它显示在登录:
E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/Pictures/Photocrowd/photocrowd20170419_130554.jpg: open failed: EACCES (Permission denied)
我在清单写这些权限:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
我怎样才能得到这张最后的照片已经拍摄并保存在我的画廊?我知道它看起来很简单,但没什么用。
您可能忘记了要求用户确认所请求的权限。你使用Android版本> = 6? Google提供运行时权限。 – greenapps