0

I build application which allows user to make "graphic" notes. I got a problem, when tried to save Bitmap into my custom ContentProvider(NotesProvider extends ContentProvider). According to the Google devGuide should override openFile(Uri uri, String mode) method. And I got Error: File not found. I look through this problem and get solution here. Then I build my representation like so

    public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
        if(sUriMatcher.match(uri)!=NOTE_ID)
            throw new IllegalArgumentException("Unsupported open file on directori uri " +uri);
        File root = new File(Environment.getDataDirectory(),
            BITMAPS_PATH);
        root.mkdirs();
        File path=new File(root, uri.getEncodedPath());
        int imode = 0;
        if (mode.contains("w")) {
            imode |= ParcelFileDescriptor.MODE_WRITE_ONLY;
            if (!path.exists()) {
                try {
                    path.createNewFile();
                } catch (IOException e) {
                    // TODO decide what to do about it, whom to notify...
                    e.printStackTrace();
                }
            }
        }
        if (mode.contains("r")) imode |= ParcelFileDescriptor.MODE_READ_ONLY;
        if (mode.contains("+")) imode |= ParcelFileDescriptor.MODE_APPEND;

        return ParcelFileDescriptor.open(path, imode);
}

and application have IOException

12:42:12.714    2550    WARN    System.err  java.io.IOException: No such file or directory
Community
  • 1
  • 1
Kirill
  • 3
  • 2

1 Answers1

0

I had a similar problem to this. In your emulator, if the folder corresponding to Environment.getDataDirectory() does not exist then you will get an exception. You need to manually create the folder first (not by the code) through either the terminal or adb. Once you do that, try running your code again and it should work.

Hakan Ozbay
  • 4,639
  • 2
  • 22
  • 28
  • Thank you so much! on emuulator existed directory, but trouble was in that uri.getEncodedPath() it is not only file name, I changed my code with this fix and it works. – Kirill Jan 16 '11 at 18:31