5

こんにちはみんな、私はID番号に従ってサーバーから画像を読み込もうとしていますので、Webサービスから取得するのが一番良い方法です。 ?WebサービスからAndroidアプリケーションに画像を取得する方法

package com.crispsys; 

import java.io.IOException; 
import java.net.MalformedURLException; 
import java.net.URL; 

import android.app.Activity; 
import android.app.ProgressDialog; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.Bundle; 
import android.os.Handler; 
import android.view.View; 
import android.widget.Button; 
import android.widget.ImageView; 
import android.widget.TextView; 
import android.widget.Toast; 

public class imageviewer extends Activity { 
    /** Called when the activity is first created. */ 

    Bitmap image; 
    private TextView ordernumber; 
    ImageView imageView; 
    private Button butt; 
    String Str="image",ordno; 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.imageviewerr); 

     this.butt = (Button) this.findViewById(R.id.getbutt); 
      this.imageView =(ImageView) findViewById(R.id.blodig); 
      this.ordernumber=(TextView)this.findViewById(R.id.ordernotext); 

      final Bundle getdata = getIntent().getExtras(); 

      if (getdata != null)  
      { 
       ordno=getdata.getString("orono"); 
       ordernumber.setText(ordno); 
      } 


     this.butt.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) { 
       // TODO Auto-generated method stub 


       Toast.makeText(getApplicationContext(),"loading" , Toast.LENGTH_LONG).show(); 

         URL url = null; 


         try 
         { 

          url = new URL("http://www.cyberstudents.in/aaa/car.jpg"); 

         } 
         catch (MalformedURLException e) 
         { 
          e.printStackTrace(); 
         } 
         try { 
          image = BitmapFactory.decodeStream(url.openStream()); 

         } 
         catch (IOException e) 
         { 
          e.printStackTrace(); 
         } 

       imageView.setImageBitmap(image); 
      } 
     }); 

    } 
} 

XMLファイル:このコーディングしてみました

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:background="@drawable/cr_isp" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TableRow 
     android:id="@+id/tableRow1" 
     android:layout_width="match_parent" 
     android:gravity="center" 
     android:paddingTop="20dp" 
     android:layout_height="wrap_content" > 

     <TextView 
      android:id="@+id/ordernotext" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textColor="#8B8682" 
      android:text="" /> 


    </TableRow> 

    <TableRow 
     android:id="@+id/tableRow1" 
     android:layout_width="match_parent" 
     android:gravity="center" 
     android:paddingTop="20dp" 
     android:layout_height="wrap_content" > 

     <Button 
      android:id="@+id/getbutt" 
      android:layout_width="210dp" 
      android:layout_height="40dp" 
      android:background="@drawable/bcbut" 
      android:textColor="#8B8682" 
      android:text="Get Block Dig" /> 

    </TableRow> 

    <TableRow 
     android:id="@+id/tableRow2" 
     android:layout_width="match_parent" 
     android:gravity="center" 
     android:paddingTop="20dp" 
     android:layout_height="wrap_content" > 

     <ImageView 
      android:id="@+id/blodig" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/ic_launcher" /> 

    </TableRow> 

</LinearLayout> 
  • が、私はそれが作品をdoesntの大きなサイズの画像のために行くとき、私は小さいサイズの画像を扱うことができ、それは同じまま、私はカント同じ画面を表示しているエラーを見つける
+0

あなたの質問を説明してください。あなたはXMLを解析していますか? –

+0

'Universal Image Loader in Android'を検索しようとしました –

+0

Krishna Suthar actualli上記のコードをチェックすると、タイプURLとbase64変換文字列で試してみましたが、小さなサイズの画像を読み込めます。 – user1389233

答えて

1

このID番号でWebサービスにGetリクエストを行い、サービスが必要なイメージで応答します。

2

これは、サーバーからの応答、つまり画像を提供している形式によって異なります。 ベース64文字列またはURLの形式にすることができます。

base64文字列の場合は、Javaのbase64クラスを使用してエンコードします。urlの場合は、URLからビットマップにイメージを変換してビットマップを使用してイメージを設定するだけです。 次のようにhttpGetを使用してサーバーにget要求を行う必要があります。

public static String connect(String url) throws IOException { 
    HttpClient httpclient = new DefaultHttpClient(); 
    HttpGet httpget = new HttpGet(url); 
    HttpResponse response; 
    try { 
     response = httpclient.execute(httpget); 
     HttpEntity entity = response.getEntity(); 
     if (entity != null) { 
      InputStream instream = entity.getContent(); 
      result = convertStreamToString(instream); 
      instream.close(); 
     } 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return result; 
} 


private static String convertStreamToString(InputStream input) { 
    /* 
    * To convert the InputStream to String we use the 
    * BufferedReader.readLine() method. We iterate until the BufferedReader 
    * return null which means there's no more data to read. Each line will 
    * appended to a StringBuilder and returned as String. 
    */ 
    BufferedReader reader = new BufferedReader(new InputStreamReader(input)); 
    StringBuilder buffer = new StringBuilder(); 
    String line = null; 
    try { 
     while ((line = reader.readLine()) != null) { 
      buffer.append(line + "\n"); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } finally { 
     try { 
      input.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
    return buffer.toString(); 
} 
+0

qは文字列ではなく文字列です。 – Tina

10

非同期イメージローダーを使用すると、イメージを非同期で読み込むことができます。以下のコードを確認してください。

private class FetchImageTask extends AsyncTask<String, Integer, Bitmap> { 
    @Override 
    protected Bitmap doInBackground(String... arg0) { 
     Bitmap b = null; 
     try { 
      b = BitmapFactory.decodeStream((InputStream) new URL(arg0[0]).getContent()); 
     } 
     catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } 
     catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return b; 
    } 
} 

このコードを実装するには。

new FetchImageTask() { 
     @Override 
     protected void onPostExecute(Bitmap result) { 
      if (result != null) { 
       image.setImageBitmap(result); 
      } 
     } 
    }.execute("IMAGE_URL"); 
関連する問題