简体   繁体   中英

Android internal storage reading file

I have a problem with Android internal storage. I have created folder in package root folder calling getDir() and with MODE_WORLD_WRITEABLE because I want camera app to write captured image in this folder. Anyway, I can see that captured image is inside that folder with DDMS . Problem is that I cannot read that file.

I tried to read file with this code:

File file = context.getDir("images", Context.MODE_WORLD_READABLE);
File image = new File(file, "image.jpeg");
if (image.canRead()) 
    Log.w("read", "can read");
else
    Log.w("read", "can't read");

And in LogCat there is only second message (can't read).

I have also tried to create FileInputStream with file name but I receive FileNotFoundException .

Can somebody tell me what I'm doing wrong here?

Thanks in advance

EDIT:

Reading file is correct but only problem is that when cam app is saving image to specified location, permission set by cam app to image file are -rwxrwx---. After changing permissions with

adb (chmod 777 image.jpeg) 

I was able to read image. Interesting thing is that cam app is writing images files to sdcard with ----rwxr-x .

Is there any way to change file permission in runtime?

Why not put it in the default photo directory?

File path = Environment.getExternalStoragePublicDirectory(
        Environment.DIRECTORY_PICTURES);
File file = new File(path, "DemoPicture.jpg");

( From: http://developer.android.com/reference/android/os/Environment.html )

See also a more complete example invoking the camera app: http://developer.android.com/training/camera/photobasics.html

Referencing to Android developers you should use openFileOutput() and openFileInput() to work on the internal storage... you are not allowed / not able to make dirs there. Android does it itself.. Those data will be cleared when your application will be deleted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM