From c840da8fc5870ab516e350f06ab184b31a636e1c Mon Sep 17 00:00:00 2001 From: Masood Azizi Date: Fri, 25 Oct 2024 16:57:24 +1100 Subject: [PATCH] Refactored and removed identical blocks of code with a helper method #1164 --- .../createpdf/util/RealPathUtil.java | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/swati4star/createpdf/util/RealPathUtil.java b/app/src/main/java/swati4star/createpdf/util/RealPathUtil.java index 3f02990b..c9f188fb 100644 --- a/app/src/main/java/swati4star/createpdf/util/RealPathUtil.java +++ b/app/src/main/java/swati4star/createpdf/util/RealPathUtil.java @@ -63,10 +63,7 @@ private String getMediaDocumentPath(Context context, Uri uri) { Cursor cursor = null; try { cursor = context.getContentResolver().query(uri, projection, null, null, null); - if (cursor != null && cursor.moveToFirst()) { - int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); - return cursor.getString(columnIndex); - } + return getColumnDataFromCursor(cursor, MediaStore.Images.Media.DATA); } catch (Exception e) { e.printStackTrace(); // Consider logging this instead of printing to console } finally { @@ -158,12 +155,8 @@ private String getFilePath(Context context, Uri uri) { final String[] projection = {MediaStore.Files.FileColumns.DISPLAY_NAME}; try (Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null)) { - if (cursor != null && cursor.moveToFirst()) { - final int index = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DISPLAY_NAME); - return cursor.getString(index); - } + return getColumnDataFromCursor(cursor, MediaStore.Files.FileColumns.DISPLAY_NAME); } - return null; } /** @@ -236,4 +229,19 @@ private boolean isRawDownloadsDocument(Uri uri) { private static class SingletonHolder { static final RealPathUtil INSTANCE = new RealPathUtil(); } + + /** + * Retrieves the data from the cursor for the specified column. + * + * @param cursor The cursor pointing to the data. + * @param columnName The name of the column to retrieve data from. + * @return The string value of the specified column, or null if the cursor is empty or the column is not found. + */ + private String getColumnDataFromCursor(Cursor cursor, String columnName) { + if (cursor != null && cursor.moveToFirst()) { + int columnIndex = cursor.getColumnIndexOrThrow(columnName); + return cursor.getString(columnIndex); + } + return null; + } } \ No newline at end of file