2017-01-28 12 views
0

私はカメラを使用しているアプリを書いています。私は奇妙な問題に遭遇しました。 "createImageFile()"は、API 16を使用して仮想デバイスにファイルを作成します。ただし、Sony Xperia Z3(Android 6.0.1、API 23)でアプリを実行するとファイルは作成されません。私もエラーはありません。私はちょうどcameraChoosen()を実行するボタンをクリックして何も起こりません。写真のためのImageFileを作成できません

debbugingの後、私はcreateImageFile()に3つのトーストを作成しました。

すべてのトーストは16

のみToast1は私のXperia Z3

public void cameraChoosen(){ 
    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 { 
      photoFile = createImageFile(); 
     } catch (IOException ex) { 
      // Error occurred while creating the File 
      //... 
     } 

     // Continue only if the File was successfully created 
     if (photoFile != null) { 
      Uri photoURI = FileProvider.getUriForFile(this, "com.example.karol.carendal", photoFile); 
      takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI); 
      startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO); 
     } 
    } 
} 

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

    Toast.makeText(getBaseContext(), "Toast1", Toast.LENGTH_SHORT).show(); 


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

    Toast.makeText(getBaseContext(), "Toast2", Toast.LENGTH_SHORT).show(); 

    if (image.exists()){ 
     Toast.makeText(getBaseContext(), "File created successfully", Toast.LENGTH_SHORT).show(); 
    } 

    // Save a file: path for use with ACTION_VIEW intents 
    mCurrentPhotoPath = image.getAbsolutePath(); 
    return image; 
} 

問題の原因となっているすべてのアイデアは、上で動作仮想デバイスAPIに取り組んでいますか?

Androidの6のためによろしく:)

+0

あまりにもそのキャッチブロックにトーストを入れてください。あなたはそのキャッチを持っています。例外はlogcatにも表示されます。 – greenapps

+0

また、そのキャッチが起こった場合、それだけでなく返されるトーストを表示するだけでなく、それでコードを続けていくのは意味がありません。トーストにe.getMessageを表示します。 – greenapps

+0

さらに、File.createTempFile()でファイルを作成しないでください。それを除く。適切なイメージファイルパスを作成する必要があります。 – greenapps

答えて

0

とあなただけの要求マニフェストの外部記憶許可を書くべきではありません以上。

また、実行時にこのアクセス許可を一度確認する必要があります。

Google for runtime permission。

また、アプリのユーザーは、アプリのAndroid設定にアクセスして、ストレージ権限スイッチを切り替えることができます。

関連する問題