I have app consist of days listview each day has its specific images placed in an infinite gallery class , what im trying to do is:
saving images with sequential number from app drawable resource ( infinite gallery class) to sd card,
im trying to get the sequential number of saved images as below :
first image :Image-1.png .
second image :Image-2.png .
third image : Image-3.png ,
and so on for all dayes .
with using :
Random generator = new Random();
This will lead to :
first issue : saved images with random numbers .
second issue : its not saving all images was choose by user to save to sd card also it save some of image twice or three times .
This is the part of code which related to saving images :
View vi=convertView;
final ViewHolder holder;
if(convertView==null){
vi = inflater.inflate(R.layout.gallery_items, null);
holder=new ViewHolder();
holder.text=(TextView)vi.findViewById(R.id.textView1);
holder.image=(ImageView)vi.findViewById(R.id.image);
holder.button=(Button)vi.findViewById(R.id.button_save);
bm = BitmapFactory.decodeResource( mContext.getResources(), images[itemPos]);
holder.image.setImageBitmap(bm);
holder.button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
String root = Environment.getExternalStorageDirectory().toString();
File imagesFolder = new File(root + "/Days pictures");
imagesFolder.mkdirs();
Random generator = new Random();
int n = 1000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".png";
File file = new File (imagesFolder, fname);
if (file.exists ())
file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
Toast.makeText(mContext, "Saved", Toast.LENGTH_LONG).show();}
catch (Exception e) {
e.printStackTrace();
Toast.makeText(mContext, e.toString(), Toast.LENGTH_SHORT).show();}}});
vi.setTag(holder);}
else holder=(ViewHolder)vi.getTag();
holder.text.setText(name[itemPos]);
final int stub_id=images[itemPos];
holder.image.setImageResource(stub_id);
return vi; }
private ImageView getImageView() {
ImageView i = new ImageView(mContext);
return i; } }
thanks for your help .
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 SDCard
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(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 Number
SharedPreferences savedNumber = getSharedPreferences(PREFS_NAME, 0);
int lastSavedNumber = savedNumber.getInt("lastsavednumber",0);
lastSavedNumber++;
String fname = "Image-"+lastSavedNumber+".png";
File file = new File (myDir, fname);
if (file.exists ()) {file.delete (); }
try {
FileOutputStream out = new FileOutputStream(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 Number
SharedPreferences saveNumber = getApplicationContext().getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editorset = 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.
public static final String PREFS_NAME = "ImageNumber";
// Get Last Saved Number
SharedPreferences savedNumber = getSharedPreferences(PREFS_NAME, 0);
int lastSavedNumber = savedNumber.getInt("lastsavednumber",0);
lastSavedNumber++;
String fname = "Image-"+lastSavedNumber+".png";
//To Store Last Saved Number
SharedPreferences saveNumber = getApplicationContext().getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editorset = saveNumber.edit();
editorset.putInt("lastsavednumber",lastSavedNumber);
editorset.commit();
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++){
String fname = "Image-"+ n +".png";
// you other stuff here
}
Hope this helps you.
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
public void onClick(View arg0) {
String root = Environment.getExternalStorageDirectory().toString();
File imagesFolder = new File(root + "/imagesFolder");
imagesFolder.mkdirs();
for (int i = 0; i < 10; i++) {
String fname = "Image-" + i + ".png";
File file = new File (imagesFolder, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(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.
来源:https://stackoverflow.com/questions/15665830/numbering-of-image-saved-from-app-resource-to-sd-card