如何在保存多个图像时消除此问题

时间:2013-12-02 11:32:48

标签: android image button save

我正在开发一个图像和图像的应用程序随按钮cliks更改。当我保存ImageView的图像然后它保存到设备,当我保存另一个图像,然后第一个被删除。所以我重新找不到保存多个图像。 Plz的帮助。 这是主要的java。

 public class Main extends Activity {
 private ImageView hImageViewPic;
 private Button iButton, gButton; 
 Bitmap bbicon;

  private int currentImage = 0; 
  int[] images = { R.drawable.r1, R.drawable.r2, R.drawable.r3, R.drawable.r4, R.drawable.r5, R.drawable.r6, R.drawable.r7, R.drawable.r8, R.drawable.r9, R.drawable.r10 };
  public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main); 

  hImageViewPic = (ImageView)findViewById(R.id.idImageViewPic);
  iButton = (Button) findViewById(R.id.bNext); 
  gButton = (Button) findViewById(R.id.bPrev); 

  //Just set one Click listener for the image 

  iButton.setOnClickListener(iButtonChangeImageListener);
  gButton.setOnClickListener(gButtonChangeImageListener); 
    } 

  View.OnClickListener iButtonChangeImageListener = new OnClickListener() {
  public void onClick(View v) {

  //Increase Counter to move to next Image

  currentImage++; 
  currentImage = currentImage % images.length;
   hImageViewPic.setImageResource(images[currentImage]);
   }
      };

  View.OnClickListener gButtonChangeImageListener = new OnClickListener() {
 `public void onClick(View v) {

  //Increase Counter to move to next Image 

  currentImage--;
  currentImage = (currentImage + images.length) % images.length; 
  hImageViewPic.setImageResource(images[currentImage]);` 

   // here we set 3rd button to save the images

  Button bSaveImg = (Button) findViewById (R.id.bSave):
  ImageView imagePreview = (ImageView) findViewById (R.id.idImageViewPic);
  imagePreview.setImageResource (images [currentImage]);
  bSaveImg.setOnClickListener (new Button.OnClickListener () {

  @Override
    public void onClick (View arg0) {

  bbicon=BitmapFactory.decodeResource(getResources(),images [cirrentImage]);
  //ByteArrayOutputStream baosicon = new ByteArrayOutputStream(); 
  //bbicon.compress(Bitmap.CompressFormat.PNG,0, baosicon); 
  //bicon=baosicon.toByteArray();

   String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
   OutputStream outStream = null; 
   File file = new File(extStorageDirectory, "er.PNG");

   try { 
     outStream = new FileOutputStream(file); 
     bbicon.compress(Bitmap.CompressFormat.PNG, 100, outStream);
     outStream.flush(); 
     outStream.close(); 
    } catch(Exception e) {
       } 
     }
       });
    }
      };
    }

因此,plz有助于根除这个问题。

3 个答案:

答案 0 :(得分:0)

更改png文件的名称,例如将png文件的名称与当前时间或日期放在一起,每个图像都是唯一的。

更改

File file = new File(extStorageDirectory, "er.PNG");

File file = new File(extStorageDirectory, System.currentTimeMillis()+".PNG");

答案 1 :(得分:0)

我更喜欢使用DateTime保存我的图像,因为它充当唯一实例

String strFileName=android.text.format.DateFormat.format("dd-MM-yyyy-hh-mm-ssaa", new java.util.Date()).toString();
File file = new File(path, strFileName+ ".png");

另外建议记住在 ExtStorageDirectory 上存储图像是不可取的。而不是在外部存储中创建一个文件夹并将其保存在那里

File sdCard = Environment.getExternalStorageDirectory();
String path = sdCard.getAbsolutePath() + "/YourFolder/";
File dir = new File(path);
if (!dir.exists()) {
    if (dir.mkdirs()) {
    }
}

编辑用户

bSaveImg.setOnClickListener (new Button.OnClickListener () {

@Override
public void onClick (View arg0) {

bbicon=BitmapFactory.decodeResource(getResources(),images [cirrentImage]);
//ByteArrayOutputStream baosicon = new ByteArrayOutputStream(); 
//bbicon.compress(Bitmap.CompressFormat.PNG,0, baosicon); 
//bicon=baosicon.toByteArray();
///below code
File sdCard = Environment.getExternalStorageDirectory();
String path = sdCard.getAbsolutePath() + "/YourFolder/";
File dir = new File(path);
if (!dir.exists()) {
    if (dir.mkdirs()) {
    }
}
  OutputStream outStream = null; 
 ///below Code
String strFileName=android.text.format.DateFormat.format("dd-MM-yyyy-hh-mm-ssaa", new java.util.Date()).toString();
File file = new File(path, strFileName+ ".png");

 try { 
   outStream = new FileOutputStream(file); 
   bbicon.compress(Bitmap.CompressFormat.PNG, 100, outStream);
   outStream.flush(); 
   outStream.close(); 
  } catch(Exception e) {
   } 
   }
   });
  }

答案 2 :(得分:0)

 //Create Folder
 File folder = new File(Environment.getExternalStorageDirectory().toString()+"/Images");
 folder.mkdirs();

 //Save the path as a string value
 String extStorageDirectory = folder.toString();
 File file = new File(extStorageDirectory, System.currentTimeMillis()+".PNG");

别忘了把try和catch放入并检查文件夹是否存在,然后再做其他事情,否则创建文件夹并将下面的行放在清单文件中。

uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"