Browse Source

URL: add method to get filename (most useful for Android’s content:// scheme).

tags/2021-05-28
Lukasz Kozakiewicz 7 years ago
parent
commit
3b130b10fe
4 changed files with 80 additions and 0 deletions
  1. +58
    -0
      modules/juce_core/native/juce_android_Files.cpp
  2. +8
    -0
      modules/juce_core/native/juce_android_Network.cpp
  3. +5
    -0
      modules/juce_core/network/juce_URL.cpp
  4. +9
    -0
      modules/juce_core/network/juce_URL.h

+ 58
- 0
modules/juce_core/native/juce_android_Files.cpp View File

@@ -137,6 +137,28 @@ public:
return getCursorDataColumn (url);
}
static String getFileNameFromContentUri (const URL& url)
{
auto uri = urlToUri (url);
auto* env = getEnv();
LocalRef<jobject> contentResolver (android.activity.callObjectMethod (JuceAppActivity.getContentResolver));
if (contentResolver == 0)
return {};
auto filename = getStringUsingDataColumn ("_display_name", env, uri, contentResolver);
// Fallback to "_data" column
if (filename.isEmpty())
{
auto path = getStringUsingDataColumn ("_data", env, uri, contentResolver);
filename = path.fromLastOccurrenceOf ("/", false, true);
}
return filename;
}
private:
//==============================================================================
static String getCursorDataColumn (const URL& url, const String& selection = {},
@@ -345,6 +367,42 @@ private:
return LocalRef<jobject> (getEnv()->CallStaticObjectMethod (AndroidUri, AndroidUri.parse,
javaString (url.toString (true)).get()));
}
//==============================================================================
static String getStringUsingDataColumn (const String& columnNameToUse, JNIEnv* env,
const LocalRef<jobject>& uri,
const LocalRef<jobject>& contentResolver)
{
LocalRef<jstring> columnName (javaString (columnNameToUse));
LocalRef<jobjectArray> projection (env->NewObjectArray (1, JavaString, columnName.get()));
LocalRef<jobject> cursor (env->CallObjectMethod (contentResolver.get(), ContentResolver.query,
uri.get(), projection.get(), nullptr,
nullptr, nullptr));
if (cursor == 0)
return {};
String fileName;
if (env->CallBooleanMethod (cursor.get(), AndroidCursor.moveToFirst) != 0)
{
auto columnIndex = env->CallIntMethod (cursor.get(), AndroidCursor.getColumnIndex, columnName.get());
if (columnIndex >= 0)
{
LocalRef<jstring> value ((jstring) env->CallObjectMethod (cursor.get(), AndroidCursor.getString, columnIndex));
if (value)
fileName = juceString (value.get());
}
}
env->CallVoidMethod (cursor.get(), AndroidCursor.close);
return fileName;
}
};
//==============================================================================


+ 8
- 0
modules/juce_core/native/juce_android_Network.cpp View File

@@ -94,6 +94,14 @@ File URL::getLocalFile() const
return fileFromFileSchemeURL (*this);
}
String URL::getFileName() const
{
if (getScheme() == "content")
return AndroidContentUriResolver::getFileNameFromContentUri (*this);
return toString (false).fromLastOccurrenceOf ("/", false, true);
}
//==============================================================================
class WebInputStream::Pimpl
{


+ 5
- 0
modules/juce_core/network/juce_URL.cpp View File

@@ -359,6 +359,11 @@ File URL::getLocalFile() const
{
return fileFromFileSchemeURL (*this);
}
String URL::getFileName() const
{
return toString (false).fromLastOccurrenceOf ("/", false, true);
}
#endif
File URL::fileFromFileSchemeURL (const URL& fileURL)


+ 9
- 0
modules/juce_core/network/juce_URL.h View File

@@ -111,6 +111,15 @@ public:
*/
File getLocalFile() const;
/** Returns the file name. For all but Android's content:// scheme, it will
simply return the last segment of the URL.
E.g. for "http://www.xyz.com/foo/bar.txt", this will return "bar.txt".
For Android's content:// scheme, it will attempt to resolve the filename
located under the URL.
*/
String getFileName() const;
/** Attempts to read a port number from the URL.
@returns the port number, or 0 if none is explicitly specified.
*/


Loading…
Cancel
Save