Search code examples
javaandroidsd-card

Get application external sd home directory


This post is ALL edited so...

I want to save a bitmap from @drawable in sdcard (/mnt/sdcard/Android/data/app_package/files/). If i try ContextWrapper.getFilesDirectory() it will return "/data/data/app_package/files/". I want to get "/mnt/sdcard/Android/data/app_package/files/". Is there any method to return it?

Thanks in advance,
Mateiaru


Solution

  • To save bitmap in external storage.

    First get the bitmap of drawable then save it into SD card.

    Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.yourBitmap);
    

    Saving it SD card.

    public void saveImageToExternalStorage(Bitmap image) {
        //image=scaleCenterCrop(image,200,200);
        String fullPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/";
        try
        {
            File dir = new File(fullPath);
            if (!dir.exists()) {
            dir.mkdirs();
            }
            OutputStream fOut = null;
            File file = new File(fullPath, "photo.png");
    
            if(file.exists())
                file.delete();
    
            file.createNewFile();
            fOut = new FileOutputStream(file);
            // 100 means no compression, the lower you go, the stronger the compression
            image.compress(Bitmap.CompressFormat.PNG, 100, fOut);
            fOut.flush();
            fOut.close();
        }
        catch (Exception e)
        {
            Log.e("saveToExternalStorage()", e.getMessage());
        }
    }