Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
SDK
/
exoplayer
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
61e4f923
authored
Aug 08, 2022
by
tonihei
Browse files
Options
_('Browse Files')
Download
Plain Diff
Merge pull request #109 from tzugen:patch-1
PiperOrigin-RevId: 464045351
parents
534740fd
83602358
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
62 additions
and
14 deletions
RELEASENOTES.md
libraries/session/src/androidTest/java/androidx/media3/session/SimpleBitmapLoaderTest.java
libraries/session/src/main/java/androidx/media3/session/SimpleBitmapLoader.java
RELEASENOTES.md
View file @
61e4f923
...
...
@@ -36,6 +36,8 @@
small icon (
[
#104
](
https://github.com/androidx/media/issues/104
)
).
*
Ensure commands sent before
`MediaController.release()`
are not dropped
(
[
#99
](
https://github.com/androidx/media/issues/99
)
).
*
`SimpleBitmapLoader`
can load bitmap from
`file://`
URIs
(
[
#108
](
https://github.com/androidx/media/issues/108
)
).
*
RTSP:
*
Add H263 fragmented packet handling
(
[
#119
](
https://github.com/androidx/media/pull/119
)
).
...
...
libraries/session/src/androidTest/java/androidx/media3/session/SimpleBitmapLoaderTest.java
View file @
61e4f923
...
...
@@ -26,14 +26,19 @@ import androidx.test.core.app.ApplicationProvider;
import
androidx.test.ext.junit.runners.AndroidJUnit4
;
import
com.google.common.util.concurrent.ListenableFuture
;
import
com.google.common.util.concurrent.MoreExecutors
;
import
java.io.File
;
import
java.io.IOException
;
import
java.net.MalformedURLException
;
import
java.nio.file.Files
;
import
java.nio.file.Paths
;
import
java.util.concurrent.ExecutionException
;
import
okhttp3.mockwebserver.MockResponse
;
import
okhttp3.mockwebserver.MockWebServer
;
import
okio.Buffer
;
import
org.junit.Rule
;
import
org.junit.Test
;
import
org.junit.function.ThrowingRunnable
;
import
org.junit.rules.TemporaryFolder
;
import
org.junit.runner.RunWith
;
/**
...
...
@@ -47,6 +52,8 @@ public class SimpleBitmapLoaderTest {
private
static
final
String
TEST_IMAGE_PATH
=
"media/jpeg/non-motion-photo-shortened.jpg"
;
@Rule
public
final
TemporaryFolder
tempFolder
=
new
TemporaryFolder
();
@Test
public
void
loadData
()
throws
Exception
{
SimpleBitmapLoader
bitmapLoader
=
...
...
@@ -70,11 +77,13 @@ public class SimpleBitmapLoaderTest {
ListenableFuture
<
Bitmap
>
future
=
bitmapLoader
.
decodeBitmap
(
new
byte
[
0
]);
assertException
(
future:
:
get
,
IllegalArgumentException
.
class
,
/* messagePart= */
"Could not decode bitmap"
);
future:
:
get
,
IllegalArgumentException
.
class
,
/* messagePart= */
"Could not decode image data"
);
}
@Test
public
void
loadUri_loadsImage
()
throws
Exception
{
public
void
load
_http
Uri_loadsImage
()
throws
Exception
{
SimpleBitmapLoader
bitmapLoader
=
new
SimpleBitmapLoader
(
MoreExecutors
.
newDirectExecutorService
());
MockWebServer
mockWebServer
=
new
MockWebServer
();
...
...
@@ -93,7 +102,7 @@ public class SimpleBitmapLoaderTest {
}
@Test
public
void
load
Uri_s
erverError_throwsException
()
{
public
void
load
_httpUriAndS
erverError_throwsException
()
{
SimpleBitmapLoader
bitmapLoader
=
new
SimpleBitmapLoader
(
MoreExecutors
.
newDirectExecutorService
());
MockWebServer
mockWebServer
=
new
MockWebServer
();
...
...
@@ -106,7 +115,36 @@ public class SimpleBitmapLoaderTest {
}
@Test
public
void
loadUri_nonHttpUri_throwsException
()
{
public
void
load_fileUri_loadsImage
()
throws
Exception
{
byte
[]
imageData
=
TestUtil
.
getByteArray
(
ApplicationProvider
.
getApplicationContext
(),
TEST_IMAGE_PATH
);
File
file
=
tempFolder
.
newFile
();
Files
.
write
(
Paths
.
get
(
file
.
getAbsolutePath
()),
imageData
);
Uri
uri
=
Uri
.
fromFile
(
file
);
SimpleBitmapLoader
bitmapLoader
=
new
SimpleBitmapLoader
(
MoreExecutors
.
newDirectExecutorService
());
Bitmap
bitmap
=
bitmapLoader
.
loadBitmap
(
uri
).
get
();
assertThat
(
bitmap
.
sameAs
(
BitmapFactory
.
decodeByteArray
(
imageData
,
/* offset= */
0
,
imageData
.
length
)))
.
isTrue
();
}
@Test
public
void
fileUriWithFileNotExisting
()
throws
Exception
{
SimpleBitmapLoader
bitmapLoader
=
new
SimpleBitmapLoader
(
MoreExecutors
.
newDirectExecutorService
());
assertException
(
()
->
bitmapLoader
.
loadBitmap
(
Uri
.
parse
(
"file:///not_valid/path/image.bmp"
)).
get
(),
IllegalArgumentException
.
class
,
/* messagePart= */
"Could not read image from file"
);
}
@Test
public
void
load_unhandledUriScheme_throwsException
()
{
SimpleBitmapLoader
bitmapLoader
=
new
SimpleBitmapLoader
(
MoreExecutors
.
newDirectExecutorService
());
...
...
@@ -115,10 +153,6 @@ public class SimpleBitmapLoaderTest {
MalformedURLException
.
class
,
/* messagePart= */
"no protocol"
);
assertException
(
()
->
bitmapLoader
.
loadBitmap
(
Uri
.
parse
(
"file://local/path"
)).
get
(),
UnsupportedOperationException
.
class
,
/* messagePart= */
"Unsupported scheme"
);
assertException
(
()
->
bitmapLoader
.
loadBitmap
(
Uri
.
parse
(
"asset://asset/path"
)).
get
(),
MalformedURLException
.
class
,
/* messagePart= */
"unknown protocol"
);
...
...
libraries/session/src/main/java/androidx/media3/session/SimpleBitmapLoader.java
View file @
61e4f923
...
...
@@ -15,6 +15,7 @@
*/
package
androidx
.
media3
.
session
;
import
static
androidx
.
media3
.
common
.
util
.
Assertions
.
checkArgument
;
import
static
androidx
.
media3
.
common
.
util
.
Assertions
.
checkStateNotNull
;
import
android.graphics.Bitmap
;
...
...
@@ -38,19 +39,21 @@ import java.util.concurrent.Executors;
/**
* A simple bitmap loader that delegates all tasks to an executor and supports fetching images from
*
HTTP/HTTPS endpoint
s.
*
URIs with {@code file}, {@code http} and {@code https} scheme
s.
*
* <p>Loading tasks are delegated to an {@link ExecutorService} (or {@link
* ListeningExecutorService}) defined during construction. If no executor service is defined, all
* tasks are delegated to a single-thread executor service that is shared between instances of this
* class.
*
* <p>
The supported URI scheme is only HTTP/HTTPS and this class reads a resource only when the
*
endpoint responds with an
{@code HTTP 200} after sending the HTTP request.
* <p>
For HTTP(S) transfers, this class reads a resource only when the endpoint responds with an
* {@code HTTP 200} after sending the HTTP request.
*/
@UnstableApi
public
final
class
SimpleBitmapLoader
implements
BitmapLoader
{
private
static
final
String
FILE_URI_EXCEPTION_MESSAGE
=
"Could not read image from file"
;
private
static
final
Supplier
<
ListeningExecutorService
>
DEFAULT_EXECUTOR_SERVICE
=
Suppliers
.
memoize
(
()
->
MoreExecutors
.
listeningDecorator
(
Executors
.
newSingleThreadExecutor
()));
...
...
@@ -82,13 +85,22 @@ public final class SimpleBitmapLoader implements BitmapLoader {
private
static
Bitmap
decode
(
byte
[]
data
)
{
@Nullable
Bitmap
bitmap
=
BitmapFactory
.
decodeByteArray
(
data
,
/* offset= */
0
,
data
.
length
);
if
(
bitmap
==
null
)
{
throw
new
IllegalArgumentException
(
"Could not decode bitmap"
);
}
checkArgument
(
bitmap
!=
null
,
"Could not decode image data"
);
return
bitmap
;
}
private
static
Bitmap
load
(
Uri
uri
)
throws
IOException
{
if
(
"file"
.
equals
(
uri
.
getScheme
()))
{
@Nullable
String
path
=
uri
.
getPath
();
if
(
path
==
null
)
{
throw
new
IllegalArgumentException
(
FILE_URI_EXCEPTION_MESSAGE
);
}
@Nullable
Bitmap
bitmap
=
BitmapFactory
.
decodeFile
(
path
);
if
(
bitmap
==
null
)
{
throw
new
IllegalArgumentException
(
FILE_URI_EXCEPTION_MESSAGE
);
}
return
bitmap
;
}
URLConnection
connection
=
new
URL
(
uri
.
toString
()).
openConnection
();
if
(!(
connection
instanceof
HttpURLConnection
))
{
throw
new
UnsupportedOperationException
(
"Unsupported scheme: "
+
uri
.
getScheme
());
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment