Save bitmap to location

前端 未结 19 2402
悲哀的现实
悲哀的现实 2020-11-22 00:20

I am working on a function to download an image from a web server, display it on the screen, and if the user wishes to keep the image, save it on the SD card in a certain fo

19条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-22 00:59

    I would also like to save a picture. But my problem(?) is that I want to save it from a bitmap that ive drawed.

    I made it like this:

     @Override
     public boolean onOptionsItemSelected(MenuItem item) {
                switch (item.getItemId()) {
                case R.id.save_sign:      
    
                    myView.save();
                    break;
    
                }
                return false;    
    
        }
    
    public void save() {
                String filename;
                Date date = new Date(0);
                SimpleDateFormat sdf = new SimpleDateFormat ("yyyyMMddHHmmss");
                filename =  sdf.format(date);
    
                try{
                     String path = Environment.getExternalStorageDirectory().toString();
                     OutputStream fOut = null;
                     File file = new File(path, "/DCIM/Signatures/"+filename+".jpg");
                     fOut = new FileOutputStream(file);
    
                     mBitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
                     fOut.flush();
                     fOut.close();
    
                     MediaStore.Images.Media.insertImage(getContentResolver()
                     ,file.getAbsolutePath(),file.getName(),file.getName());
    
                }catch (Exception e) {
                    e.printStackTrace();
                }
    
     }
    

提交回复
热议问题