From caf830ce44dba61c1170a2c427a0f72301ae183d Mon Sep 17 00:00:00 2001 From: Ahmet Inan Date: Sun, 6 Aug 2023 13:08:58 +0200 Subject: [PATCH] check for null --- .../com/aicodix/assempix/MainActivity.java | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/com/aicodix/assempix/MainActivity.java b/app/src/main/java/com/aicodix/assempix/MainActivity.java index 8c1d2b4..8bb1efd 100644 --- a/app/src/main/java/com/aicodix/assempix/MainActivity.java +++ b/app/src/main/java/com/aicodix/assempix/MainActivity.java @@ -6,15 +6,6 @@ package com.aicodix.assempix; -import androidx.annotation.NonNull; -import androidx.appcompat.app.AlertDialog; -import androidx.appcompat.app.AppCompatActivity; -import androidx.appcompat.app.AppCompatDelegate; -import androidx.appcompat.widget.ShareActionProvider; -import androidx.core.app.ActivityCompat; -import androidx.core.content.ContextCompat; -import androidx.core.view.MenuItemCompat; - import android.Manifest; import android.content.ContentResolver; import android.content.ContentValues; @@ -41,6 +32,15 @@ import android.widget.LinearLayout; import android.widget.Toast; +import androidx.annotation.NonNull; +import androidx.appcompat.app.AlertDialog; +import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.app.AppCompatDelegate; +import androidx.appcompat.widget.ShareActionProvider; +import androidx.core.app.ActivityCompat; +import androidx.core.content.ContextCompat; +import androidx.core.view.MenuItemCompat; + import com.aicodix.assempix.databinding.ActivityMainBinding; import java.io.File; @@ -201,9 +201,17 @@ private void storePayload(String mime, String suffix, Date date) { values.put(MediaStore.Images.ImageColumns.MIME_TYPE, mime); ContentResolver resolver = getContentResolver(); Uri uri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); + if (uri == null) { + statusMessage(R.string.storing_picture_failed); + return; + } if (Build.VERSION.SDK_INT > Build.VERSION_CODES.P) { try { ParcelFileDescriptor descriptor = getContentResolver().openFileDescriptor(uri, "w"); + if (descriptor == null) { + statusMessage(R.string.storing_picture_failed); + return; + } FileOutputStream stream = new FileOutputStream(descriptor.getFileDescriptor()); stream.write(payload); stream.close();