2017-04-19 9 views
0

私はMainActivityとShowPhotoDescriptionActivityの2つのアクティビティを持っています。最初のアクティビティにはFloatingActionButtonがあり、これを押すとカメラが起動し、写真を撮ってこのアプリのフォルダに保存します。その後、2番目のアクティビティが呼び出されます。このアクティビティでは、ImageViewで最後に撮影した写真が表示されます。シンプルに見えますが、何も動作しません。 私はすでにいくつかの解決策を試してきましたが、そのほとんどは私の問題を解決しませんでした。私は写真を撮って別のアクティビティのImageViewで表示

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); 
    } 
} 

よしを持ってMainActivityで

、これはちょうど、写真を撮る「Photocrowd」という名前のフォルダに保存することができ、ギャラリーでそれを表示し、他の活動へのパスを送ります。 PhotoDescriptionActivityで

私はすでに(ライブラリ、setImageUri、等を使用して)他の方法を使用することを試みた

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)); 

} 

を有します。

E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/Pictures/Photocrowd/photocrowd20170419_130554.jpg: open failed: EACCES (Permission denied) 

私はマニフェストでこれらの権限を書いた:imageRootへのパスは、このコードは、私が試したし、それがログに表示されていること最後でした(/storage/emulated/0/Pictures/Photocrowd/photocrowd20170419_124851.jpg

私のAndroidの際、写真付きの説明に示したと同じです

<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が私の問題を簡単に解決できると言いました。私はランタイムのアクセス許可を知らなかったので、[documentation](https://developer.android.com/training/permissions/requesting.html)を読んでそれを行うことができました。 –

関連する問題