android: save images to specific folder in the sd card

后端 未结 3 1856
隐瞒了意图╮
隐瞒了意图╮ 2021-01-15 22:09

i have here a code snippet that saves a bitmap on sd card:

String filename = String.valueOf(System.currentTimeMillis()) ;
ContentValues values = new ContentV         


        
相关标签:
3条回答
  • 2021-01-15 22:35

    try this

    File myDir=new File("/sdcard/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 this in manifest

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    

    check my answer here : https://stackoverflow.com/a/7887114/964741

    0 讨论(0)
  • 2021-01-15 22:37

    Please try following code,

    File sdCard = Environment.getExternalStorageDirectory();
            File dir = new File (sdCard.getAbsolutePath());
            dir.mkdirs();
    
        File out = new File(dir,filename);
        try {
            out.createNewFile();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        DataOutputStream fo = null;
    
            try {
                fo = new DataOutputStream( new FileOutputStream(out));
                //write what you want to fo
                fo.close();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
    0 讨论(0)
  • 2021-01-15 22:37

    Please try following code.

    URL imageUrl = new URL("url");
    
    Bitmap bitmap=BitmapFactory.decodeStream(imageURLFacebook.openConnection().getInputStream());
    
    saveToInternalStorage(bitmap);//call method and pass bitmap
    

    Method to save in internal storage

     private String saveToInternalStorage(Bitmap bitmapImage) {
            File directory = Environment.getExternalStorageDirectory();
            File childDirectory = new File(directory, "FolderName");
            if (!childDirectory.exists()) {
                childDirectory.mkdirs();
            }
    
            // Create imageDir
            File mypath = new File(childDirectory, "profile.jpeg");
    
            try {
                FileOutputStream fos = new FileOutputStream(mypath);
                // Use the compress method on the BitMap object to write image to the OutputStream
                bitmapImage.compress(Bitmap.CompressFormat.JPEG, 100, fos);
                fos.close();
            } catch (Exception e) {
    
                e.printStackTrace();
            }
            return String.valueOf(mypath);
        }
    

    Add this in manifest

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    
    0 讨论(0)
提交回复
热议问题