mirror of
https://github.com/fankes/termux-app.git
synced 2025-09-06 02:35:19 +08:00
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:
@@ -142,6 +142,7 @@ public class TermuxFileReceiverActivity extends Activity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (attachmentFileName == null) attachmentFileName = subjectFromIntent;
|
if (attachmentFileName == null) attachmentFileName = subjectFromIntent;
|
||||||
|
if (attachmentFileName == null) attachmentFileName = UriUtils.getUriFileBasename(uri, true);
|
||||||
|
|
||||||
InputStream in = getContentResolver().openInputStream(uri);
|
InputStream in = getContentResolver().openInputStream(uri);
|
||||||
promptNameAndSave(in, attachmentFileName);
|
promptNameAndSave(in, attachmentFileName);
|
||||||
|
@@ -5,6 +5,8 @@ import android.net.Uri;
|
|||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
|
|
||||||
|
import com.termux.shared.file.FileUtils;
|
||||||
|
|
||||||
public class UriUtils {
|
public class UriUtils {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -30,6 +32,28 @@ public class UriUtils {
|
|||||||
return path + (DataUtils.isNullOrEmpty(fragment) ? "" : "#" + fragment);
|
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.
|
* Get {@link ContentResolver#SCHEME_FILE} {@link Uri} for path.
|
||||||
*
|
*
|
||||||
|
Reference in New Issue
Block a user