WebからローカルのAndroid携帯に画像をロードしています。ファイルに書き込むためのコードは、次のとおりです。BufferedInputStreamをファイルに変換する
BufferedInputStream bisMBImage=null;
InputStream isImage = null;
URL urlImage = null;
URLConnection urlImageCon = null;
try
{
urlImage = new URL(imageURL); //you can write here any link
urlImageCon = urlImage.openConnection();
isImage = urlImageCon.getInputStream();
bisMBImage = new BufferedInputStream(isImage);
int dotPos = imageURL.lastIndexOf(".");
if (dotPos > 0)
{
imageExt = imageURL.substring(dotPos,imageURL.length());
}
imageFileName = PATH + "t1" + imageExt;
File file = new File(imageFileName);
if (file.exists())
{
file.delete();
Log.d("FD",imageFileName + " deleted");
}
ByteArrayBuffer baf = new ByteArrayBuffer(255);
Log.d("IMAGEWRITE", "Start to write image to Disk");
int current = 0;
try
{
while ((current = bisMBImage.read()) != -1)
{
baf.append((byte) current);
}
FileOutputStream fos = new FileOutputStream(file);
fos.write(baf.toByteArray());
fos.close();
Log.d("IMAGEWRITE", "Image write to Disk done");
}
catch (IOException e)
{
e.printStackTrace();
}
isImage.close();
}
catch (IOException e)
{
Log.d("DownloadImage", "Error: " + e);
}
finally
{
isImage = null;
urlImageCon = null;
urlImage = null;
}
何らかの理由で、ファイルへの書き込み全体に1分かかります。私はこれを最適化する方法はありますか?
大きな画像ですか? – Otra
オトラが尋ねるものに似ています - イメージはどれくらい大きいですか?また、あなたはこの質問に 'android-emulator'タグを入れています。エミュレータは特定のものについては遅くなるかもしれませんが、私はそれを使用しないので、ファイルのダウンロード/書き込みが含まれているかわかりません。実際のデバイスでこれを試して、どれくらい時間がかかるか見てみましたか? – Squonk
これはメインのUIスレッドまたは別のスレッドで実行されていますか? – WindsurferOak