2017-10-06 8 views
0

私はこのようなカメラを使って画像をキャプチャするアプリを持っています。ファイルがアンドロイドに存在しませんか?

 String[] galleryPermissions = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}; 

    if (EasyPermissions.hasPermissions(this, galleryPermissions)){ 
    //Camera 
    if (index == 0) { 
     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) {    
      Toast.makeText(getApplicationContext(),ex.getMessage(),Toast.LENGTH_LONG).show(); 
      } 
      // Continue only if the File was successfully created 
      if (photoFile != null) { 

       Uri photoURI = null; 

       // N is for Nougat Api 24 Android 7 
       if (Build.VERSION_CODES.N <= android.os.Build.VERSION.SDK_INT) { 
        // FileProvider required for Android 7. Sending a file URI throws exception. 


        photoURI = FileProvider.getUriForFile(this, 
          BuildConfig.APPLICATION_ID + ".provider", 
          photoFile); 


        Log.v("highbuild",photoURI.toString()); 

       } else { 
        // For older devices: 
        // Samsung Galaxy Tab 7" 2 (Samsung GT-P3113 Android 4.2.2, API 17) 
        // Samsung S3 
        photoURI = Uri.fromFile(photoFile); 
        Log.v("lowbuild",photoURI.toString()); 
       } 
       imageuriduplicate = photoURI; 
       takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI); 
       startActivityForResult(takePictureIntent, 0); 
      } 
      else{ 
       Log.v("file", "photo file is null"); 
       Toast.makeText(getApplicationContext(),"Photo file is null", Toast.LENGTH_LONG).show(); 
      } 
     } 

    } 

    //Gallery 
    else if (index == 1) { 

     Intent pickPhoto = new Intent(Intent.ACTION_PICK, 
       MediaStore.Images.Media.INTERNAL_CONTENT_URI); 
     pickPhoto.setType("image/*"); 
     startActivityForResult(pickPhoto, 1);//one can be replaced with any action code 
    } 
} 

else { 
    EasyPermissions.requestPermissions(this, "Access for storage", 
      101, galleryPermissions); 
} 

マイファイルイメージメソッドを作成し、私はその後ArrayListにパスを保存し、それをアクセスし、この

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

     boolean s = new File(storageDir.getPath()).mkdirs(); 

     if(!s){ 
      Log.v("not", "not created"); 
     } 
     else{ 
      Log.v("cr","directory created"); 
     } 
    } 
    else{ 
     Log.v("directory", "directory exists"); 
    } 

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

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

のようなものです。

しかし、私は、私は、エラーが存在するdoesntのファイルを取得し、この

   Uri imageuri = Uri.parse(receipts.get(i).imageURI); 
       File file = new File(imageuri.getPath()); 
      if(file.exists()){ 
       Log.v("exists","file exists"); 
      } 
      else{ 
       Log.v("no","file doesnt exist"); 
      } 

のようにファイルにアクセスしようとします。

私は必要な権限をすべて与えていますが、私はまだこのエラーが発生しています。どのようにこれを修正できますか?

+0

Uri(自分自身のAndroidクラスですか?)には "file:..."が含まれています。 –

+0

mkdirs()の戻り値をチェックしていません。さらに、ファイルの存在を直接確認する必要があります。あなたがデータベースを使いこなした後ではありません。 – greenapps

+0

mkdirs()がfalseを返す場合は、IOExceptionをスローする必要があります。またはnullを返します。 – greenapps

答えて

1

storageDirが存在することを確認してください。そうでない場合は、ディレクトリを作成する

new File(path_to_dir).mkdirs(); 
+0

このチェックを追加したばかりです。if(!storageDir.exists()){ 新しいファイル(storageDir.getPath())。mkdirs(); } しかし、私はまだファイルには存在しないエラーが表示されます。 –

+0

完全なエラーメッセージを追加してください –

+0

mkdirs()の戻り値を確認してください。まず、ファイルを置く前にディレクトリが存在するかどうかを確認します。 – greenapps

関連する問題