Question

I'm trying to convert file *logo_image.png* in `drawable/` folder to a `File`. 

The following code fails as f is null;

int drawableResourceId = 
     context.getResources().getIdentifier("logo_image", 
                                          "drawable", 
                                           context.getPackageName());                 

File f=new File("logo_image"+".png");
try
{         
     InputStream inputStream = (InputStream) 
                                  context.getResources()
                                            .openRawResource(drawableResourceId);
     OutputStream out=new FileOutputStream(f);
     byte buf[]=new byte[1024];
     int len;
     while((len=inputStream.read(buf))>0)
         out.write(buf,0,len);

     out.close();
     inputStream.close();
}
catch (IOException e){
}  
Was it helpful?

Solution

1 . Check if you have a set the permission:

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

2 .Set a outdirectory for the output file

3 . check if file exists otherwise call f.createNewFile();

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top