Android saving file to external storage

后端 未结 12 925
抹茶落季
抹茶落季 2020-11-22 03:12

I have a little issue with creating a directory and saving a file to it on my android application. I\'m using this piece of code to do this :

String filename         


        
12条回答
  •  傲寒
    傲寒 (楼主)
    2020-11-22 03:41

    Use this function to save your bitmap in SD card

    private void SaveImage(Bitmap finalBitmap) {
    
        String root = Environment.getExternalStorageDirectory().toString();
        File myDir = new File(root + "/saved_images");    
         if (!myDir.exists()) {
                        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 this in manifest

     
    

    EDIT: By using this line you will be able to see saved images in the gallery view.

    sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
                             Uri.parse("file://" + Environment.getExternalStorageDirectory())));
    

    look at this link also http://rajareddypolam.wordpress.com/?p=3&preview=true

提交回复
热议问题