2016-12-12 17 views
0

イメージファイルのサイズは約16MBです。この画像をimageViewに読み込み、そのマーカーを追加した後にズームしたいと思います。私はサブサンプリングスケール画像ビューでこれを試しました。私は以下のリンクhttps://github.com/davemorrissey/subsampling-scale-image-viewに従っています。Android - イメージビューでバイト単位でイメージを読み込みます。

重要な点は、URLから画像を読み込むことです。上記のライブラリはそれをサポートしていません。だから私はその画像をダウンロードし、そのローカルファイルからロードした後にSDカードに保存しました。技術的には動作しています。

問題:

今の問題は、それが初めてのダウンロードのための時間のあまりを取っています。また、2度目でもほぼ1分かかります。

私の考え:

この問題のため、私はバイトで画像バイトをロードしてみてください。画像が100bytesをダウンロードすると、次にimageViewにURLから画像の次の部分をダウンロードすることが示されます。そんなことをすることは可能ですか?誰かがこの謎を解くために私を助けることができる

URL url = new URL(url_); 

       URLConnection conection = url.openConnection(); 
       conection.connect(); 
       // getting file length 
       int lenghtOfFile = conection.getContentLength(); 

       // input stream to read file - with 8k buffer 
       InputStream input = new BufferedInputStream(url.openStream(), 8192); 

       // Output stream to write file 

       OutputStream output = new FileOutputStream(root+"/"+ fileName); 
       byte data[] = new byte[1024]; 

       long total = 0; 
       while ((count = input.read(data)) != -1) { 
        total += count; 

        // writing data to file 
        output.write(data, 0, count); 

       } 

       // flushing output 
       output.flush(); 

       // closing streams 
       output.close(); 
       input.close(); 
       runOnUiThread(new Runnable() { 
        @Override 
        public void run() { 
         image.setImage(ImageSource.uri(root+"/"+ fileName)); 
        } 
       }); 

は現在、私は次のコードのようなイメージをロードするのですか?

注:このライブラリ以外の可能性がある場合は、を追加してください。

+0

APIから来ていますか?私は画像を意味します – Vadivel

+0

はいそれはAPIからです – Amsheer

+0

画像をtimthumbまたはmthumb形式に変換するようにPHP開発者に依頼してください – Vadivel

答えて

0

これを試したことはありませんが、この機能が動作しているかどうかを確認できます。

URLからデータをバイト配列形式で取得します。

data = getImageStream(url); //should call in async Task.. 

バイト配列をimageViewに変換して設定します。

Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); 
image.setImageBitmap(bitmap) 

これは、パフォーマンスの向上に役立ちます。

public byte[] getImageStream(String url){  
ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
InputStream is = null; 
try { 
    is = url.openStream(); 
    byte[] byteChunk = new byte[4096]; // Or whatever size you want to read in at a time. 
    int n; 

    while ((n = is.read(byteChunk)) > 0) { 
    baos.write(byteChunk, 0, n); 
    } 
} 
catch (IOException e) { 
    System.err.printf ("Failed while reading bytes from %s: %s", url.toExternalForm(), e.getMessage()); 
    e.printStackTrace(); 
    // Perform any other exception handling that's appropriate. 
} 
finally { 
    if (is != null) { is.close(); } 
} 
return baos.toByteArray(); 
} 
+0

私のコードとあなたのコードとの違いは何ですか? – Amsheer

関連する問題