Save bitmap to location

前端 未结 19 2411
悲哀的现实
悲哀的现实 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 01:04

    The way I found to send PNG and transparency.

    String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() +
                        "/CustomDir";
    File dir = new File(file_path);
    if(!dir.exists())
      dir.mkdirs();
    
    String format = new SimpleDateFormat("yyyyMMddHHmmss",
           java.util.Locale.getDefault()).format(new Date());
    
    File file = new File(dir, format + ".png");
    FileOutputStream fOut;
    try {
            fOut = new FileOutputStream(file);
            yourbitmap.compress(Bitmap.CompressFormat.PNG, 85, fOut);
            fOut.flush();
            fOut.close();
         } catch (Exception e) {
            e.printStackTrace();
     }
    
    Uri uri = Uri.fromFile(file);     
    Intent intent = new Intent(android.content.Intent.ACTION_SEND);
    intent.setType("image/*");
    intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "");
    intent.putExtra(android.content.Intent.EXTRA_TEXT, "");
    intent.putExtra(Intent.EXTRA_STREAM, uri);
    
    startActivity(Intent.createChooser(intent,"Sharing something")));
    

提交回复
热议问题