3

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?

rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
Emran
  • 1,031
  • 11
  • 25
  • Are you _sure_ the file's in the package root, and not in the `files` subdirectory? It's vary rare to put files in the package root itself. – David Given Mar 22 '12 at 23:23
  • In package root is folder _images_ and inside that folder I have files. I'm using internal storage in case if the device is without external storage. I don't know how to create folder inside _files_ with MODE_WORLD_WRITEABLE so cam app can write captured image. – Emran Mar 22 '12 at 23:30

2 Answers2

1

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

Sparky
  • 8,437
  • 1
  • 29
  • 41
  • 1
    hi, maybe you can include this in your answer because the most people want to show their pictures up in gallery after taking it and wondering why it doesnt work. Make a broadcast to register the file like this after saving it: `sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+file.getAbsolutePath())));` – Vossi Mar 23 '12 at 00:28
  • getExternalStoragePublicDirectory() returns external storage. I want to save images to internal storage if external is not available. – Emran Mar 23 '12 at 00:59
  • @Sparky: I was unable to find another solution so I used this one. Thanks :) – Emran Mar 24 '12 at 20:22
0

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.

Vossi
  • 231
  • 2
  • 8