我有兩個活動: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