1

I tried to save picture to external storage (in this example I'm using simple drawable resourse) , but this file doesn't exist whatever I did:rescanned SD, changed getExternalStorageDirectory() to getExternalStoragePublicDirectory

it just doesn't work...

I have all permissions:

  <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

there is a code:

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new DrawView(this));
    }

    class DrawView extends View {

        Paint paint;
        Bitmap bitmap;

        public DrawView(Context context) {
            super(context);
            paint = new Paint(Paint.ANTI_ALIAS_FLAG);
            paint.setTextSize(40);

            Bitmap bmpIcon = BitmapFactory.decodeResource(getResources(), R.drawable.launcher);
            bmpIcon = Bitmap.createScaledBitmap(bmpIcon, 500, 500, true);

            bitmap = Bitmap.createBitmap(500, 500, Bitmap.Config.RGB_565);
            Canvas canvas = new Canvas(bitmap);
            canvas.drawColor(Color.WHITE);
            canvas.drawBitmap(bmpIcon, 0,0, paint);
            canvas.drawText("Saved bitmap", 100, 50, paint);

            File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "savedBitmap.png");
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                FileOutputStream fos = null;
                try {
                    fos = new FileOutputStream(file);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
                } finally {
                    if (fos != null) fos.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        @Override
        protected void onDraw(Canvas canvas) {
            canvas.drawARGB(80, 102, 204, 255);
            canvas.drawBitmap(bitmap, 100, 100, paint);
        }

    }
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Alisa
  • 23
  • 4
  • "I have all permissions" -- there is no `READ_INTERNAL_STORAGE` permission, and there is no `WRITE_INTERNAL_STORAGE` permission. You do not have any code that updates the `MediaStore` (e.g., `MediaScannerConnection.scanFile()`). Beyond that, are you getting any stack traces in LogCat? How are you determining that "this file doesn't exist"? – CommonsWare Aug 10 '16 at 15:04
  • This is the tutorial and I tried to do it step by step. READ_INTERNAL_STORAGE and WRITE_INTERNAL_STORAGE are exist in example above. thanks for the tip MediaScannerConnection.scanFile() – Alisa Aug 10 '16 at 15:27
  • "READ_INTERNAL_STORAGE and WRITE_INTERNAL_STORAGE are exist in example above" -- then use a better example, as [those permissions do not exist in Android](https://developer.android.com/reference/android/Manifest.permission.html). – CommonsWare Aug 10 '16 at 15:32
  • really, what a mistake! thank you! but it still seems don't save. – Alisa Aug 10 '16 at 15:52
  • Now file is saved on tablet with 14 api somehow in internal storage and don't on phone with 23 api, so maybe this could be the reason? – Alisa Aug 10 '16 at 16:12

1 Answers1

0

file is saved on tablet with 14 api somehow in internal storage and don't on phone with 23 api

You are not implementing runtime permissions, most likely.

Note that if you look at LogCat, you should see stack traces showing that you do not have permission to work with external storage.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • You're right-this was the real reason! When I downgraded to targed sdk 22 file saved, when I implemented runtime permission (http://stackoverflow.com/a/34722591/6570875) to targetSdkVersion 23 file has been saved succesfully but into internal phone storage-I don't know why. Thank you very much for help! – Alisa Aug 10 '16 at 20:54