Monday 11 March 2013

How to get absolute path of drawable images ?


Hello Everyone , After lots of searching on internet I am able to get absolute path of images that are stored in drawable folder of android application.

There is no any straight forward way to get the absolute path. What I have done is that I have stored images on device’s internal memory/sdcard.

If your application is going to be installed on sdcard then you can store it in sdcard.

Make sure that depending on size of images , you decide where to store.


To store Image on sdcard.


Bitmap bitMap = BitmapFactory.decodeResource(getResources(),R.id.img1);

File mFile1 = Environment.getExternalStorageDirectory();

String fileName =”img1.jpg”;

File mFile2 = new File(mFile1,fileName);
try {
       FileOutputStream outStream;

       outStream = new FileOutputStream(mFile2);

       bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);

       outStream.flush();

       outStream.close();

       } catch (FileNotFoundException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
       } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
                         }

   String    sdPath = mFile1.getAbsolutePath().toString()+"/"+fileName;

Log.i(“MAULIK”, “Your IMAGE ABSOLUTE PATH:-”+sdPath); 

       File temp=new File(sdPath);

       if(!temp.exists()){
              Log.e("file","no image file at location :"+sdPath);
       }
                          
Store Image in internal Memory


Bitmap bitMap = BitmapFactory.decodeResource(getResources(),R.id.img1);

String fileName =”img1.jpg”;

try {
                                 
FileOutputStream out1=openFileOutput(fileName, Context.MODE_PRIVATE);

bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out1);

out1.flush();

out1.close();

File f=getFileStreamPath(fileName);

String mPath=f.getAbsolutePath();

       } catch (FileNotFoundException e1) {
                                  // TODO Auto-generated catch block
                                  e1.printStackTrace();
                           } catch (IOException e) {
                                  // TODO Auto-generated catch block
                                  e.printStackTrace();
                           }

      
Log.i(“MAULIK”, “Your IMAGE ABSOLUTE PATH:-”+mPath);   

      

I am sure this will be helpful to many. If you have any problem kindly contact me.

If you have better approach than this, kindly inform me.

Thank You.  

No comments:

Post a Comment