圖像Uri應該是一個文件Uri(即file:// absolute_path scheme)到本地文件。
使用壁畫ü可以這樣做
final DataSource<CloseableReference<PooledByteBuffer>> dataSource =
ImagePipelineFactory.getInstance().getImagePipeline().fetchEncodedImage(ImageRequest.fromUri(Constants.DEFAULT_LOGO), this);
DataSubscriber<CloseableReference<PooledByteBuffer>> dataSubscriber =
new BaseDataSubscriber<CloseableReference<PooledByteBuffer>>() {
@Override
protected void onNewResultImpl(
DataSource<CloseableReference<PooledByteBuffer>> dataSource) {
if (!dataSource.isFinished()) {
return;
}
CloseableReference<PooledByteBuffer> imageReference = dataSource.getResult();
if (imageReference != null) {
FileOutputStream fileOutputStream = null;
PooledByteBufferInputStream pooledByteBufferInputStream;
try {
pooledByteBufferInputStream = new PooledByteBufferInputStream(imageReference.get());
// write the inputStream to a FileOutputStream
File tempFile = AndroidUtils.createImageFile();
fileOutputStream = new FileOutputStream(tempFile);
int read = 0;
byte[] bytes = new byte[1024];
while ((read = pooledByteBufferInputStream.read(bytes)) != -1) {
fileOutputStream.write(bytes, 0, read);
}
startTwitterIntent(tempFile);
} catch (MalformedURLException e) {
AnalyticsManager.logError(e);
finish();
} catch (IOException e) {
e.printStackTrace();
} finally {
CloseableReference.closeSafely(imageReference);
imageReference = null;
dataSource.close();
}
}
}
@Override
protected void onFailureImpl
(DataSource<CloseableReference<PooledByteBuffer>> dataSource) {
Throwable t = dataSource.getFailureCause();
AnalyticsManager.logError(t);
}
};
dataSource.subscribe(dataSubscriber, UiThreadImmediateExecutorService.getInstance());
希望它可以幫助