如何将图像从imageView保存到SD

时间:2014-08-04 08:24:34

标签: android image memory imageview save

我将图片从imageView保存到SD。图像已保存。 问题是存在第一个图像,并且保存下一个图像再次使用不同的名称保存第一个图像。 据我所知,需要抓住imageView中的图片加载到playImage的时刻。但怎么办呢? 感谢的。

将图像加载到imageView中并保存到sd:

 public class Gallery extends Activity implements OnClickListener {
    String item;

    Button btnsave, btnhome;
    ImageView playImage;

    String fotoname;


        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.gallery);

            btnhome = (Button) findViewById(R.id.btn_home);
            btnhome.setOnClickListener(this);

            btnsave = (Button)findViewById(R.id.btn_save);
            btnsave.setOnClickListener(this);


            playImage = (ImageView)findViewById(R.id.displayImage);


           final ImageView playImage = (ImageView) findViewById(R.id.displayImage);
           final LinearLayout myGallery = (LinearLayout) findViewById(R.id.mygallery1);

            Bundle extras = getIntent().getExtras();
            if(extras !=null) {
                 item = extras.getString("item");
                if(item.equals("Item")){    
            try {   
                String galleryDirectoryName = "ITEM/item";
                String[] listImages = getAssets().list(galleryDirectoryName);
                for (String imageName : listImages) {
                    InputStream is = getAssets().open(galleryDirectoryName + "/" + imageName);
                    final Bitmap bitmap = BitmapFactory.decodeStream(is);
                    ImageView imageView = new ImageView(getApplicationContext());
                    imageView.setLayoutParams(new ViewGroup.LayoutParams(350, 225));
                    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                    imageView.setImageBitmap(bitmap);
                    imageView.setPadding(10, 70, 10, 70);
                    imageView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            playImage.setImageBitmap(bitmap);
                            playImage.setPadding(5, 0, 5, 0);
                        }
                    });
                    myGallery.addView(imageView);     
                }
            } catch (IOException e) {
                Log.e("GalleryWithHorizontalScrollView", e.getMessage(), e);
            }     

    }
            }

        }

    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch(v.getId()){
        case R.id.btn_save:
         playImage.setDrawingCacheEnabled(true);
                        Bitmap bitmap = playImage.getDrawingCache();

                        String root = Environment.getExternalStorageDirectory().toString();
                        File newDir = new File(root + "/." + (getString(R.string.app_name)));    
                        newDir.mkdirs();
                        Random gen = new Random();
                        int n = 10000;
                        n = gen.nextInt(n);
                        String fotoname = "photo-"+ n +".jpg";
                        File file = new File (newDir, fotoname);
                        if (file.exists ()) file.delete (); 
                            try {
                               FileOutputStream out = new FileOutputStream(file);
                               bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
                               out.flush();
                               out.close();
                               Toast.makeText(this, (getString(R.string.saved)), Toast.LENGTH_SHORT ).show();

                            } catch (Exception e) {     
                            } 
                             {
                                Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                                File f = new File(newDir, fotoname);
                                Uri contentUri = Uri.fromFile(f);
                                mediaScanIntent.setData(contentUri);
                                this.sendBroadcast(mediaScanIntent);
                            }



        break;
        case R.id.btn_home:

        finish();
    }
    }
    }

1 个答案:

答案 0 :(得分:0)

检查一下。

save.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                ll.setDrawingCacheEnabled(true);
                Bitmap bitmap = ll.getDrawingCache();
                // bitmap = Bitmap.createBitmap(480, 800,
                // Bitmap.Config.ARGB_8888);
                String root = Environment.getExternalStorageDirectory()
                        .toString();
                File newDir = new File(root + "/Collage_Maker");
                newDir.mkdirs();
                Random gen = new Random();
                int n = 10000;
                n = gen.nextInt(n);
                String fotoname = "cm_"+n + ".jpg";
                File file = new File(newDir, fotoname);
                String s = file.getAbsolutePath();
                Log.i("Path of saved image.", s);
                System.err.print("Path of saved image." + s);

                try {
                    FileOutputStream out = new FileOutputStream(file);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);

                    out.flush();
                    Toast.makeText(getApplicationContext(), "Photo Saved "+ fotoname,
                            Toast.LENGTH_SHORT).show();
                    out.close();
                } catch (Exception e) {

                    Log.e("Exception", e.toString());
                }
            }

        });