2017-10-06 5 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; 
} 

처럼 만들어 내.

하지만 난 오류가 존재 나던 파일을 얻을이

   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 (소유 안드로이드 클래스?)는 "file : ..."을 포함 할 수 있습니다. –

+0

mkdirs()의 반환 값을 확인하지 않습니다. 또한 파일의 존재를 직접 확인해야합니다. 네가 데이터베이스를 망칠 때가 아냐. – greenapps

+0

mkdirs()가 false를 반환하면 IOException을 throw해야합니다. 또는 null을 반환합니다. – greenapps

답변

1

storageDir이 있는지 확인하십시오. 디렉토리를 만들지 않을 경우

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

방금이 검사를 추가했습니다. if (! storageDir.exists()) { 새 파일 (storageDir.getPath()). mkdirs(); } 하지만 여전히 파일에 오류가 없습니다. –

+0

전체 오류 메시지를 추가하십시오. –

+0

mkdirs()의 반환 값을 확인하십시오 !!! 먼저 파일을 넣기 전에 디렉토리가 존재하는지 확인하십시오. – greenapps

관련 문제