Skip to content

Commit

Permalink
Refactored and removed identical blocks of code with a helper method S…
Browse files Browse the repository at this point in the history
  • Loading branch information
u7253836 committed Oct 25, 2024
1 parent f0e43ed commit c840da8
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions app/src/main/java/swati4star/createpdf/util/RealPathUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}
}

0 comments on commit c840da8

Please sign in to comment.