How to save file in storage on Android 12

Viewed 58

In my application I want create screen recorder and I want save it into storage!
For this I create service and write some code!
But after run service, Immediately stop it and show me Recorder stop toast!
I write below code into service :

Handler mHandler = new Handler(Looper.getMainLooper()) {
    @Override
    public void handleMessage(Message message) {
        Log.e("ServiceErrors","RecorderService - Handler : " + message.getData().toString());
        Toast.makeText(RecorderService.this, "Recorder stop", Toast.LENGTH_SHORT).show();
    }
};

/* Its weird that android does not index the files immediately once its created and that causes
 * trouble for user in finding the video in gallery. Let's explicitly announce the file creation
 * to android and index it */
private void indexFile() {
    //Create a new ArrayList and add the newly created video file path to it
    ArrayList<String> toBeScanned = new ArrayList<>();
    toBeScanned.add(SAVEPATH);
    String[] toBeScannedStr = new String[toBeScanned.size()];
    toBeScannedStr = toBeScanned.toArray(toBeScannedStr);

    //Request MediaScannerConnection to scan the new file and index it
    MediaScannerConnection.scanFile(this, toBeScannedStr, null, (path, uri) -> {
        //Show toast on main thread
        Message message = mHandler.obtainMessage();
        Log.e("ServiceErrors","RecorderService Index : " + message.toString());
        message.sendToTarget();
        stopSelf();
    });
}

//Stop and destroy all the objects used for screen recording
private void destroyMediaProjection() {
    this.mAudioManager.setParameters("screenRecordAudioSource=-1");
    try {
        mMediaRecorder.stop();
        indexFile();
    } catch (RuntimeException e) {
        if (new File(SAVEPATH).delete())
            Toast.makeText(this, "ذخیره ویدیو با مشکل روبرو شد", Toast.LENGTH_SHORT).show();
    } finally {
        mMediaRecorder.reset();
        mVirtualDisplay.release();
        mMediaRecorder.release();
        if (mMediaProjection != null) {
            mMediaProjection.unregisterCallback(mMediaProjectionCallback);
            mMediaProjection.stop();
            mMediaProjection = null;
        }
        stopSelf();
    }
    isRecording = false;
}

UPDATE Save to storage location code :

public void getValues() {
    String res = getResolution();
    setWidthHeight(res);
    FPS = 25;
    //BITRATE = 7130317;
    BITRATE = 5530317;
    audioRecSource = "1";
    //saveLocation = Environment.getExternalStorageDirectory() + File.separator + ConstKeys.APPDIR + APPDIR_ORIGINAL;
    saveLocation = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)
            + File.separator + ConstKeys.APPDIR + APPDIR_ORIGINAL;
    File saveDir = new File(saveLocation);
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) && !saveDir.isDirectory()) {
        saveDir.mkdirs();
    }

    String saveFileName = getFileSaveName();
    SAVEPATH = saveLocation + File.separator + saveFileName + ".mp4";
}

How can I fix it? Please help me <3

1 Answers

On Android 11+ you cannot save video files in a picture directory.

So no .mp4 in public DCIM directory.

Related