我有两个活动:MainActivity和ShowPhotoDescriptionActivity。第一个活动有一个FloatingActionButton,当按下它时,它会启动相机,拍摄照片,将其保存到此应用程序的文件夹中。之后,调用第二个Activity。此活动应显示在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
)
此代码是我尝试的最后一个代码,它显示在Log:
中E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/Pictures/Photocrowd/photocrowd20170419_130554.jpg: open failed: EACCES (Permission denied)
我在Manifest中写了这些权限:
<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"/>
我怎样才能将最后一张照片拍摄下来并保存在我的画廊中?我知道它看起来很简单但没有任何效果。
答案 0 :(得分:0)
您未获得许可&#34; WRITE_EXTERNAL_STORAGE&#34;如您的日志中所述:
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.WRITE_EXTERNAL_STORAGE" />
是危险权限,从Android 6.0开始,您需要在运行时请求权限:
从Android 6.0(API级别23)开始,用户在应用程序运行时向应用程序授予权限,而不是在安装应用程序时。此方法简化了应用安装过程,因为用户在安装或更新应用时无需授予权限。它还使用户可以更好地控制应用程序的功能;
您可以在Google Guide
了解更多信息为了便于编码,我建议您使用:PermissionsDispatcher,但它易于使用,并使您的代码更清晰。