Android app Image is not saving in specified Sd Card folder after Crop, Showing empty folder

641 views Asked by At

I am capturing the image using camera in my android app.

then Cropping the image.

after Cropping saving the image in specified folder.

Folder is creating, But image is not saving in the folder.(Its Empty)

Help me to resolve it

code I have used,

link referred

                    if (extras != null) {

                            Bitmap photooutput = extras.getParcelable("data");
                            // Camera Output

                            if (pick == 1) {

                                viewImage.setImageBitmap(photooutput);


                                String path = Environment.getExternalStorageDirectory().toString();
                                File m_imgDirectory = new File(path + "/WallPaper/");

                                if (!m_imgDirectory.exists()) m_imgDirectory.mkdir();

                                FileOutputStream m_fOut = null;
                                File directory2 = new File(path);
                                directory2.delete();
                                String m_fileid = System.currentTimeMillis() + "";
                                directory2 = new File(path, "/Wall/" + m_fileid + ".png");  


                                try
                                {
                                    if (!directory2.exists()) directory2.createNewFile();
                                    m_fOut = new FileOutputStream(directory2);      
                                    Bitmap m_bitmap = photooutput.copy(Bitmap.Config.ARGB_8888, true);
                                    m_bitmap.compress(Bitmap.CompressFormat.PNG, 100, m_fOut);
                                    m_fOut.flush();
                                    m_fOut.close();
                                    MediaStore.Images.Media.insertImage(getContentResolver(),
     directory2.getAbsolutePath(), directory2.getName(), directory2.getName());
                                }
                                catch (Exception p_e)
                                {
                                }
                        }
}
1

There are 1 answers

4
DorAyalon On

When using Android KitKat and above, it is impossible for an app to save a file onto the SDCard. Check this Thread

UPDATE Save an image to internal storage:

public static Uri saveImage(Bitmap bmp) {
    Uri uri = null;
    try {
        String name = System.currentTimeMillis() + ".jpg";
        //you can create a new file name "test.jpg" in sdcard folder.
        File f = new File(getImagesDir() + File.separator + name);

        f.createNewFile();
        //write the bytes in file
        FileOutputStream fo = new FileOutputStream(f);
        fo.write(bitmapToByteArray(bmp));
        // remember close de FileOutput
        fo.close();

        uri = Uri.parse("file://" + f.getPath());
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return uri;
}

public static String getImagesDir() {
    String rootDir = Environment.getExternalStorageDirectory().toString();
    rootDir = rootDir + "/MyApp/Media/Images";

    // Create directory if not existed
    File dir = new File(rootDir);
    if (!dir.exists()) {
        dir.mkdir();
    }

    return dir.getPath();
}
public static byte[] bitmapToByteArray(Bitmap bmp) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] byteArray = stream.toByteArray();
    return byteArray;
}

UPDATE 2: missing code added

Add the following permissions to your AndroidManifest:

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