Search code examples
androidandroid-10.0

Andriod 10 How to check file exist


In Android Q, save pictures in app-specific directory,

path like = /data/user/0/xxx.xxx.xxx/files/phone/abc.jpg

not save in the external storage, use Device FileExplorer to view, need to check if file exist, avoid to download again ,but in Android Q file.exist() not work

File newFile = new File(path);
newFile.exists();

always return false

this question. I need to use MediaStore or SAF to resolver it. or other function to check it.

If I use MediaStore to check. use ContentResolver. May be like this:

public void getPhotoCursor(Uri uri) {
  Cursor cursor = getContentResolver().query(uri, null, null, null, null, null);
  try {
    if (cursor != null && cursor.moveToFirst()) {
        String displayName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));

    }
  } finally {
     cursor.close();
  }
}

But I can't get the Uri form app-specific directory. If I get the Uri, how to use file descriptor to check. or use SAF to check.

 File testFile = new File(getExternalFilesDir()+"phone", "abc.jpg");
 FileProvider.getUriForFile(,,testFile);

 Intent testIntent = new Intent(Intent.ACTION_VIEW);
 testIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 testIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
 testIntent.setDataAndType();
 startActivity(testIntent);

In the ActivityResult to check it

Any help will be apperciated


Solution

  • is my fault, every time open APP I will delete all the .jpg from APP-specific. so into APP I want to check avoid download again. file exist always return false.