0

The error I am receiving is at this particular file

    FileInputStream fstream = new FileInputStream("data/data/com.demo.filesave/AllData");

==>>07-27 10:41:30.710: java.io.FileNotFoundException: /data/data/com.demo.filesave/AllData (Permission denied)

==>>07-27 10:41:33.914: V/Log_tag(9274): java.io.FileNotFoundException: /data/data/com.demo.filesave/AllData/signature.png (Permission denied)

//File SignSave = Environment.getExternalStorageDirectory();

FileWrite(SignSave,"confirmation"); 
    public  void FileWrite(File aPath,String aBody)
        {
            try 
            {
                //System.out.println("@@@@ Inside Try FileWrite @@@@");
                Log.e("BEFORE FILE","BEFORE FILE");
                aPath.createNewFile();
                Log.e("AFTER FILE","BEFORE AFTER");
                PrintWriter out1 = new PrintWriter(aPath);
                Log.e("AFTER FILE","AFTER PRINT WRITER");
                out1.write(aBody);  
                Log.e("AFTER FILE","AFTER WRITING");
                //System.out.println (aBody.trim());
                out1.flush();
                out1.close();
            }
            catch (IOException ioe)
            {
                //System.out.println("@@@@ Inside Catch FileWrite @@@@"); 
                ioe.printStackTrace();
            }

        }

==>>07-27 10:41:33.921: W/System.err(9274): at java.io.FileNotFoundException /mnt/sdcard (Is a directory) on line PrintWriter out1 = new PrintWriter(aPath);

Permissions in manifest file

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
    </uses-permission>

Please help me in solving the issue.

Regards,

2 Answers2

0

you can use this(rl is the relative layout with drawingCache enabled)

newimg=rl.getDrawingCache();
String myPath=Environment.getExternalStorageDirectory()+"/saved_images";
File myDir=new File(myPath);
        try
        {
        myDir.mkdirs();
        }
        catch(Exception e)
        {
            Toast.makeText(getBaseContext(), "error: "+e.getMessage(), Toast.LENGTH_LONG).show();
        }
String fname = imagename+".jpg";
        File file = new File (myDir, fname);
        if (file.exists ()) file.delete ();
        String filename = file.getAbsolutePath ();
        FileOutputStream fos = null;
try {
        fos = new FileOutputStream (file);
        newimg.compress (CompressFormat.JPEG, 95, fos);
        Toast.makeText(getBaseContext(), filename+"  saved", Toast.LENGTH_LONG).show();
        } catch (Throwable ex) {
            Toast.makeText(getBaseContext(), "error: "+ex.getMessage(), Toast.LENGTH_LONG).show();
        }
Siddhesh
  • 1,370
  • 11
  • 28
0

data/data/ is a directory in the internal storage.

You are trying to write in the internal storage explicitly which, i think, is not allowed.

The permission:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
    </uses-permission>

gives your application to programmatically write to the sd card( or other 'external storage'). So if you are looking to store files in the cache directory , you should follow what the official documentation says :

If you're using API Level 7 or lower, use getExternalStorageDirectory(), to open a File representing the root of the external storage. You should then write your data in the following directory:

/Android/data/<package_name>/files/

If you're using API Level 8 or greater, use getExternalCacheDir() to open a File that represents the external storage directory where you should save cache files.

Vinay W
  • 9,912
  • 8
  • 41
  • 47