2017-08-29 4 views
1

だから私は(カメラの意図を持つ)アンドロイドチュートリアルキャプチャ、それはgetExternalStoragePublicDirectory呼び出したときに(画像を作成する「ファイル」)を使用するために特別に述べているチュートリアルでは単にアンドロイド:getExternalPublicStorageDirectoryに保存し、ギャラリーに表示

を以下ました例ではgetExternalStorageDirectoryを使用しています。基本的には、「私がしているようには言いません」という例です。

私はgetExternalStoragePublicDirectoryを使用してみましたが、すべてのエラーが発生しました。私は結局それを動作させました、そして、私はこれらのファイルが実際にADBシェルを介して存在するのを見ることができます。私の人生のために、私はそれらをギャラリーに現われることができません。ファイルをスキャンするためにメディアスキャナ接続を使用することは、そのトリックをしていないようです。誰かがファイルをギャラリーに表示する方法を知っていますか?

コードスニペット:電話が内部ストレージ内の任意の写真をピックアップしなければならないよう

private File createImageFile() throws IOException { // Create an image file name 
     String timeStamp = new 
     SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
     String imageFileName = "JPEG_" + timeStamp + "_"; 
     File pictureDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); 


     // if the file does not exist, then create the file. 
     if(!pictureDir.exists()) 
     { 
      pictureDir.mkdirs(); 
     } 

     File image = File.createTempFile(
       imageFileName, /* prefix */ 
       ".jpg",   /* suffix */ 
       pictureDir  /* directory */ 
     ); 

     boolean exists = image.exists(); 

     this.currentPhotoPath = image.getAbsolutePath(); 

     // Save a file: path for use with ACTION_VIEW intents 
     return image; 
    } 

    private void dispatchTakePictureIntent() 
    { 

    verifyStoragePermissions(this); 
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 

     // i believe this is checking if the activity exists. 
     if(takePictureIntent.resolveActivity(getPackageManager()) != null) 
     { 
      File imageFile = null; 

      try 
      { 
       imageFile = createImageFile(); 
      } 
      catch (IOException e) 
      { 
       Log.i("sf", e.getMessage()); 
       Log.i("sf",e.getCause().getMessage()); 
      } 

      if(imageFile != null) 
      { 
       imageFile.toURI(); 
       takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageFile.toURI()); 
       startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE); 
      } 
     } 
    } 

     private void addGalleryPic() throws IOException { 
      Intent mediaScanIntent = new 
      Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); 
      File f = new File(this.currentPhotoPath); 

      if(f.exists()) 
      { 
       Log.i("sdf", f.getAbsolutePath() + " exists"); 

      } 

      MediaScannerConnection.scanFile(this, 
        new String[]{currentPhotoPath}, null, 
        new MediaScannerConnection.OnScanCompletedListener() { 
         public void onScanCompleted(String path, Uri uri) { 
          String p = uri.getPath(); 
          Log.i("ExternalStorage", "Scanned " + path + ":"); 
          Log.i("ExternalStorage", "-> uri=" + uri); 
         } 
        }); 
     } 
+0

"ファイルをスキャンするためのメディアスキャナ接続を使用しても、このトリックはしていないようです" - そうすべきです。しかし、 'addGalleryPic()'をどこで呼んでいるのか分かりません。 – CommonsWare

+0

onActivityResultをオーバーライドしたと仮定したので、私はそれを含めませんでした。 @CommonsWare – j209

+0

次に、 'currentPhotoPath'が' null'です。保存されたインスタンス状態 'Bundle'に保存する方法がわからないので、カメラアプリケーションがフォアグラウンドになっている間にプロセスが終了する可能性があります。 – CommonsWare

答えて

0

は内部ストレージにそれらを保存してください。

希望すると助かります!

関連する問題