Why does my file not exist even when I gave it the path that exists?

My if condition on imgFile.exist() gives me false, but I see that the file exists in the path when Log.d prints the result.

I'm just trying to load an image from a path to put it as the background of a thumbnail image of an activity.

I also have read / write permission in my manifest android.permission.WRITE_EXTERNAL_STORAGE

UPLOAD IMAGE:

 File imgFile = new File(getRowItem.getImgURI().trim()); Log.d("TAGG", "FILE PATH: " + imgFile.getPath()); // imgFile path: /storage/emulated/0/TravelDiary/img_1461684314337.jpg if(imgFile.exists()){ //it never reach inside here Log.d("TAGG", "FILE ABSOLUTE PATH: " + imgFile.getAbsolutePath()); Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath()); Drawable drawable = new BitmapDrawable(getResources(), myBitmap); imageView.setBackground(drawable); } 

SAVING THE IMAGE:

the image is saved when the user, from the intention of Chooser, selects the hood of the image from the camera or selects the image from the gallery

  public void ImagePicker(View v){ if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) { if (PackageManager.PERMISSION_GRANTED == ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) && PackageManager.PERMISSION_GRANTED == ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)) { final File rootdir = new File(Environment.getExternalStorageDirectory() + File.separator + "TravelDiary"); rootdir.mkdirs(); final String filename = "img_" + System.currentTimeMillis() + ".jpg"; final File sdImageMainDirecotry = new File(rootdir, filename); outputFileUri = Uri.fromFile(sdImageMainDirecotry); ... ... } else { //if permission is not granted, then we ask for it ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, 1); }else{ Toast.makeText(this, "External storage not available", Toast.LENGTH_SHORT).show(); } } 

Permissions in my manifest:

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.muddii.traveldiary"> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> ... ... ... </manifest> 
0
android
source share
3 answers

Add this to your manifest.

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
0
source share

What is the meaning of your SDK in gradle? If it's 23, you need to ask for permission at runtime.

http://developer.android.com/training/permissions/requesting.html

0
source share

try this instead of imgFile.exists ()

 File file = new File(path); if (!file.isFile()) { //your code }else{ //your code } 
0
source share

All Articles