0

I am trying to save the following bitmap either in the external storage, if available and if not available, to save it in the internal storage. I thought that the following code should work but when I removed my sd card, I am still getting the toast message from the sd card section. I can get it to work if I just use either the external or internal section of the code but can't seem to figure out how to get both of them working together...

save.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {

        LinearLayout ll = findViewById(R.id.form_linear_layout);
        Bitmap bitmap = getBitmapFromView(ll);

        String state = Environment.getExternalStorageState();
        if (!Environment.MEDIA_MOUNTED.equals(state)) {

            pathInternal = saveInternalImage(bitmap);
            //If it isn't mounted - we can't write into it.
            // return "";
        } else {
            path = saveImage(bitmap);
        }
    }

    public String saveInternalImage(Bitmap myBitmap) {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        myBitmap.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
        File wallpaperDirectory = new File(
                Environment.getDataDirectory() + IMAGE_DIRECTORY /*iDyme folder*/);

        // have the object build the directory structure, if needed.
        if (!wallpaperDirectory.exists()) {
            wallpaperDirectory.mkdirs();
            Log.d("hhhhh", wallpaperDirectory.toString());
        }

        try {
            File f = new File(wallpaperDirectory, Calendar.getInstance()
                    .getTimeInMillis() + ".jpg");
            f.createNewFile();
            FileOutputStream fo = new FileOutputStream(f);
            fo.write(bytes.toByteArray());
            MediaScannerConnection.scanFile(MainActivity.this,
                    new String[]{f.getPath()},
                    new String[]{"image/jpeg"}, null);
            fo.close();
            Log.d("TAG", "File Saved::--->" + f.getAbsolutePath());
            //  Toast.makeText(MainActivity.this, "You have successfully saved into the phone internal memory!", Toast.LENGTH_SHORT).show();
            return f.getAbsolutePath();

        } catch (IOException e1) {
            e1.printStackTrace();
        }
        Toast.makeText(MainActivity.this, "You have successfully saved into the phone internal memory!", Toast.LENGTH_SHORT).show();
        return "";
    }

    public String saveImage(Bitmap myBitmap) {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        myBitmap.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
        File wallpaperDirectory = new File(
                Environment.getExternalStorageDirectory() + IMAGE_DIRECTORY /*iDyme folder*/);

        String state = Environment.getExternalStorageState();
        if (!Environment.MEDIA_MOUNTED.equals(state)) {

            Toast.makeText(MainActivity.this, "You do not have an SD Card!", Toast.LENGTH_SHORT).show();

            //If it isn't mounted - we can't write into it.
            //    return "";
        }

        // have the object build the directory structure, if needed.
        if (!wallpaperDirectory.exists()) {
            wallpaperDirectory.mkdirs();
            Log.d("hhhhh", wallpaperDirectory.toString());
        }

        try {
            File f = new File(wallpaperDirectory, Calendar.getInstance()
                    .getTimeInMillis() + ".jpg");
            f.createNewFile();
            FileOutputStream fo = new FileOutputStream(f);
            fo.write(bytes.toByteArray());
            MediaScannerConnection.scanFile(MainActivity.this,
                    new String[]{f.getPath()},
                    new String[]{"image/jpeg"}, null);
            fo.close();
            Log.d("TAG", "File Saved::--->" + f.getAbsolutePath());
            //     Toast.makeText(MainActivity.this, "You have successfully saved into the phone External memory!", Toast.LENGTH_SHORT).show();
            //        return f.getAbsolutePath();
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        Toast.makeText(MainActivity.this, "You have successfully saved into the phone External memory!", Toast.LENGTH_SHORT).show();
        return "";
    }
}
Andrew
  • 1,947
  • 2
  • 23
  • 61
piano0011
  • 11
  • 2

1 Answers1

0

For example check bool condition:

Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);

and more info you can find here.

Andrew
  • 1,947
  • 2
  • 23
  • 61
  • I just read from one of the threads that external storage here also means the phone's internal storage, which is what was putting me off but when having a sd card inserted, I can't find the bitmap saved to my sd card? It is under storage but there is not folder in the sd card called storage? – piano0011 Dec 01 '20 at 03:24