mirror of https://github.com/termux/termux-app
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:
parent
74b23cb209
commit
7bbc12c7c9
|
@ -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);
|
||||
|
|
|
@ -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.
|
||||
*
|
||||
|
|
Loading…
Reference in New Issue