Numbering Of Image Saved From App Resource To Sd Card
Solution 1:
From your question and comments i can understand that you want to save n number of images to SDCard.
To save follow the steps
STEP 1: Get All the Images you need. Make sure you getting the list of images correctly here.
STEP 2: Count number of images in the list and store it in variale
int numberOfImages = 15;// Get it dynamically
STEP 3: Now loop it to store all the images in sequential order
//Create Directory to store images in SDCardStringroot= Environment.getExternalStorageDirectory().toString();
FilemyDir=newFile(root + "/saved_images");
if(!myDir.exists()){
myDir.mkdirs();
}
// You have to get next image here from the resource here
bm = BitmapFactory.decodeResource( mContext.getResources(), images[i]);// value for itemPos should be given here.// Get Last Saved NumberSharedPreferencessavedNumber= getSharedPreferences(PREFS_NAME, 0);
intlastSavedNumber= savedNumber.getInt("lastsavednumber",0);
lastSavedNumber++;
Stringfname="Image-"+lastSavedNumber+".png";
Filefile=newFile (myDir, fname);
if (file.exists ()) {file.delete (); }
try {
FileOutputStreamout=newFileOutputStream(file);
bm.compress(Bitmap.CompressFormat.JPEG, 90, out);//Your Bitmap from the resouce
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
//To Store the last NumberSharedPreferencessaveNumber= getApplicationContext().getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editoreditorset= saveNumber.edit();
editorset.putInt("lastsavednumber",lastSavedNumber);
editorset.commit();
The duplication can take place if you do any thing wrong in your first step.
EDIT To Store All Images in Sequential Order Use SharedPreferences to Store last saved image number.
publicstaticfinalStringPREFS_NAME="ImageNumber";
// Get Last Saved NumberSharedPreferencessavedNumber= getSharedPreferences(PREFS_NAME, 0);
intlastSavedNumber= savedNumber.getInt("lastsavednumber",0);
lastSavedNumber++;
Stringfname="Image-"+lastSavedNumber+".png";
//To Store Last Saved NumberSharedPreferencessaveNumber= getApplicationContext().getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editoreditorset= saveNumber.edit();
editorset.putInt("lastsavednumber",lastSavedNumber);
editorset.commit();
Solution 2:
Simply use for
loop. if you getting size how much images you want save on SD card then,
for(int n=1 ; n <= size ; n++){
Stringfname="Image-"+ n +".png";
// you other stuff here
}
Hope this helps you.
Solution 3:
Forgive Random, if you want images in sequential order (as Pragnani suggested and you approved on comments above) and supposing that your code is ok, do this:
Override
publicvoidonClick(View arg0) {
Stringroot= Environment.getExternalStorageDirectory().toString();
FileimagesFolder=newFile(root + "/imagesFolder");
imagesFolder.mkdirs();
for (inti=0; i < 10; i++) {
Stringfname="Image-" + i + ".png";
Filefile=newFile (imagesFolder, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStreamout=newFileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
Toast.makeText(MainActivity.this, "Saved", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(MainActivity.this, e.toString(), Toast.LENGTH_SHORT).show();
}
}
}
Test it and let me know.
Post a Comment for "Numbering Of Image Saved From App Resource To Sd Card"