CMA
CMA

Reputation: 2818

android: save images to specific folder in the sd card

i have here a code snippet that saves a bitmap on sd card:

String filename = String.valueOf(System.currentTimeMillis()) ;
ContentValues values = new ContentValues();   
values.put(Images.Media.TITLE, filename);
values.put(Images.Media.DATE_ADDED, System.currentTimeMillis()); 
values.put(Images.Media.MIME_TYPE, "image/jpeg");

Uri uri = getContentResolver().insert(Images.Media.EXTERNAL_CONTENT_URI, values);

try {
    OutputStream outStream = getContentResolver().openOutputStream(uri);
    bm.compress(Bitmap.CompressFormat.JPEG, 90, outStream);
    outStream.flush();
    outStream.close();

the default storage is the "Camera" folder in the gallery.

my question is:

using the code above, can i save my images to a different folder on my sd card?

Upvotes: 1

Views: 12223

Answers (3)

Firdosh
Firdosh

Reputation: 352

Please try following code.

URL imageUrl = new URL("url");

Bitmap bitmap=BitmapFactory.decodeStream(imageURLFacebook.openConnection().getInputStream());

saveToInternalStorage(bitmap);//call method and pass bitmap

Method to save in internal storage

 private String saveToInternalStorage(Bitmap bitmapImage) {
        File directory = Environment.getExternalStorageDirectory();
        File childDirectory = new File(directory, "FolderName");
        if (!childDirectory.exists()) {
            childDirectory.mkdirs();
        }

        // Create imageDir
        File mypath = new File(childDirectory, "profile.jpeg");

        try {
            FileOutputStream fos = new FileOutputStream(mypath);
            // Use the compress method on the BitMap object to write image to the OutputStream
            bitmapImage.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.close();
        } catch (Exception e) {

            e.printStackTrace();
        }
        return String.valueOf(mypath);
    }

Add this in manifest

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

Upvotes: 0

RajaReddy PolamReddy
RajaReddy PolamReddy

Reputation: 22493

try this

File myDir=new File("/sdcard/saved_images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete (); 
try {
       FileOutputStream out = new FileOutputStream(file);
       finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
       out.flush();
       out.close();

} catch (Exception e) {
       e.printStackTrace();
}

and add this in manifest

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

check my answer here : https://stackoverflow.com/a/7887114/964741

Upvotes: 6

Lucifer
Lucifer

Reputation: 29672

Please try following code,

File sdCard = Environment.getExternalStorageDirectory();
        File dir = new File (sdCard.getAbsolutePath());
        dir.mkdirs();

    File out = new File(dir,filename);
    try {
        out.createNewFile();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    DataOutputStream fo = null;

        try {
            fo = new DataOutputStream( new FileOutputStream(out));
            //write what you want to fo
            fo.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

Upvotes: 1

Related Questions