私のWeb APIから画像を取得して、これをrecyclerviewに取り込もうとしています。 Web APIを使用して写真を取得しています - 私はRetrofit 2.0を使用してAPIを使用しています。画像は圧縮されています(サイズは100〜300kbです)。私が直面している問題は、入力ストリームの内容を出力ストリームに書き込むためのwhileループが長くかかることです。このループは、各画像に対して7〜11秒の間にどこかにかかることがわかりました。写真を取得するコードは次のとおりです。OutputStreamへのInputStreamの書き込みが長すぎます
if (postList.size() > 0) {
for (HousePostViewModel model : postList) {
Response<ResponseBody> pictureCall = service.getHousePostImage("Bearer " + sharedPreferences.getString("authorization_token", ""), model.getHousePostID(), currentActivity.getResources().getString(R.string.homenet_client_string)).execute();
if (pictureCall.isSuccessful()) {
try {
InputStream inputStream = null;
FileOutputStream outputStream = null;
File profileFile = new File(currentActivity.getExternalCacheDir() + File.separator + generateRandomString()+"tempImage3.jpg");
inputStream = new BufferedInputStream(pictureCall.body().byteStream());
outputStream = new FileOutputStream(profileFile);
int c;
Log.i("START", "Starting to read the image");
long timeInMS = System.currentTimeMillis();
//This is the loop, where images take long to write to outputstream
while ((c = inputStream.read()) != -1) {
outputStream.write(c);
}
Picture picture = new Picture(profileFile);
pictureList.add(picture);
long finish = System.currentTimeMillis();
long finalTime = finish - timeInMS;
Log.i("END", "Finished Reading file in " +finalTime+" ms");
inputStream.close();
outputStream.close();
} catch (Exception error) {
}
} else {
errorString += pictureCall.errorBody().string();
}
}
}
}
} else {
errorString += postCall.errorBody().string();
}
私は何をしようとしますか?または、APIから画像データを取得する別の方法がありますか?
ありがとうございました。
一度に1バイトではなく、少なくとも1 KBのブロックで読み書きを試みます。 *参考:* ['Files.copy()'](https://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html#copy-java.io.InputStream- java.nio.file.Path-java.nio.file.CopyOption ...-)は、** 8 kb **バッファを使用します。 – Andreas