save file to internal memory in android?

前端 未结 2 1529
予麋鹿
予麋鹿 2021-01-23 18:17

I am downloading a file from server with help of a URL provided through web service. I am successful for every version of devices but getting exception in OS 4.1 devices. I am

2条回答
  •  情深已故
    2021-01-23 19:10

    try this code:

    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root + "/saved_images");
    myDir.mkdirs();
    Random generator = new Random();
    int n = 10000;
    n = generator.nextInt(n);
    String fname = "Image-" + n + ".jpg";
    File file = new File(myDir, fname);
    
    if (file.exists()) file.delete();
    
    try {
      FileOutputStream out = new FileOutputStream(file);
      finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
      out.flush();
      out.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
    

    and add in manifest:

     
    

提交回复
热议问题