2017-04-19 66 views
0

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

我怎樣才能得到這張最後的照片已經拍攝並保存在我的畫廊?我知道它看起來很簡單,但沒什麼用。

+0

您可能忘記了要求用戶確認所請求的權限。你使用Android版本> = 6? Google提供運行時權限。 – greenapps

回答

0

您未被授予權限「WRITE_EXTERNAL_STORAGE」,如日誌中所述: 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,雖然很容易使用,讓你的代碼乾淨多了。

+0

感謝您的解釋和@greenapps說我可以輕鬆解決我的問題。我不知道運行時權限,所以我讀它是[文檔](https://developer.android.com/training/permissions/requesting.html),並可以做到這一點。 –

相關問題