0

我的 android 应用程序中有一个寻呼机活动,我需要根据寻呼机中的位置保存图像。我设法完成了保存部分,但是当我在第一张图像中单击保存时,它保存第二张图像与第二张图像相同,它保存第三张我不知道我的代码有什么问题!`

enter code here

public boolean onOptionsItemSelected(MenuItem item) 
{
    // Handle item selection
    if (item.getItemId()==R.id.menuFinale) 
    {

        ImageView imageView = (ImageView) findViewById(R.id.image_one);
        imageView.setDrawingCacheEnabled(true);
        Bitmap bitmap = imageView.getDrawingCache();
        File root = Environment.getExternalStorageDirectory();
        MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "My pic" ,"Saved to gallery");
        File file = new File(root.getAbsolutePath()+"/DCIM/Camera/img.jpg");
        try 
        {
            file.createNewFile();
            FileOutputStream ostream = new FileOutputStream(file);
            bitmap.compress(CompressFormat.JPEG, 100, ostream);
            ostream.close();
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }



        return true;
    }
        else  {
                return super.onOptionsItemSelected(item);
            }
    }
4

2 回答 2

0

尝试以下操作:

 button=(Button)vi.findViewById(R.id.button_save);

        button.setOnClickListener(new OnClickListener() {

  private Bitmap bm;
  private String PREFS_NAME;
public void onClick(View arg0) {
    String root = Environment.getExternalStorageDirectory().toString();
       File myDir = new File(root + "/saved_images");
       if(!myDir.exists()){
           myDir.mkdirs();}
       bm = BitmapFactory.decodeResource( mContext.getResources(), images[itemPos]);
         holder.image.setImageBitmap(bm);
         SharedPreferences savedNumber = mContext.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.PNG, 100, out);//Your Bitmap from the resouce 
        out.flush(); 
        out.close(); }
         catch (Exception e) { 
        e.printStackTrace(); 
        } 

        SharedPreferences saveNumber = mContext.getApplicationContext().getSharedPreferences(PREFS_NAME, 0); 
        SharedPreferences.Editor editorset = saveNumber.edit(); 
        editorset.putInt("lastsavednumber",lastSavedNumber); 
        editorset.commit();
          Toast.makeText(mContext, "saved", Toast.LENGTH_SHORT). show();}});

希望能帮到你。

于 2013-07-18T00:35:21.200 回答
0

我终于设法解决了我的问题,而不是我指的缓存的 imageView 我必须改为参考 ViewPager 来缓存包括 imageView 在内的所有内容,而不是这里是我的新代码

enter code here

public boolean onOptionsItemSelected(MenuItem item) 
{
    // Handle item selection
    if (item.getItemId()==R.id.menuFinale) 
    {


        pager.setDrawingCacheEnabled(true);

        pager.buildDrawingCache(true);
        pager.setDrawingCacheEnabled(true);

        Bitmap b = pager.getDrawingCache(true);
        File root = Environment.getExternalStorageDirectory();
        MediaStore.Images.Media.insertImage(getContentResolver(), b, "My pic" ,"Saved to gallery");
        File file = new File(root.getAbsolutePath()+"/DCIM/HD.jpg");
        try 
        {
            file.createNewFile();
            FileOutputStream ostream = new FileOutputStream(file);
            b.compress(CompressFormat.JPEG, 100, ostream);
            ostream.close();

        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }
        return true;
    }
        else  
            {
                return super.onOptionsItemSelected(item);
            }     

}

于 2013-07-18T23:28:47.090 回答