Fixed: Get file basename from Uri path when opening files in termux if failed to get it ContentResolver and EXTRA_TITLE

This commit is contained in:
agnostic-apollo 2021-10-23 22:41:08 +05:00
parent 74b23cb209
commit 7bbc12c7c9
2 changed files with 25 additions and 0 deletions

View File

@ -142,6 +142,7 @@ public class TermuxFileReceiverActivity extends Activity {
}
if (attachmentFileName == null) attachmentFileName = subjectFromIntent;
if (attachmentFileName == null) attachmentFileName = UriUtils.getUriFileBasename(uri, true);
InputStream in = getContentResolver().openInputStream(uri);
promptNameAndSave(in, attachmentFileName);

View File

@ -5,6 +5,8 @@ import android.net.Uri;
import androidx.annotation.NonNull;
import com.termux.shared.file.FileUtils;
public class UriUtils {
/**
@ -30,6 +32,28 @@ public class UriUtils {
return path + (DataUtils.isNullOrEmpty(fragment) ? "" : "#" + fragment);
}
/**
* Get the file basename from a {@link Uri}. The file basename is anything after last forward
* slash "/" in the path, or the path itself if its not found.
*
* @param uri The {@link Uri} to get basename from.
* @param withFragment If the {@link Uri} fragment should be included in basename.
* @return Returns the file basename if found, otherwise {@code null}.
*/
public static String getUriFileBasename(Uri uri, boolean withFragment) {
if (uri == null) return null;
String path;
if (withFragment) {
path = getUriFilePath(uri);
} else {
path = uri.getPath();
if (DataUtils.isNullOrEmpty(path)) return null;
}
return FileUtils.getFileBasename(path);
}
/**
* Get {@link ContentResolver#SCHEME_FILE} {@link Uri} for path.
*