2

I am getting the news information from web. So i can easily get the header and subject of news but the problem is how to get the image from json?

Here is my code:

public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {  
                        convertView = mInflater.inflate(R.layout.haber_yapisi, null); // gets all of the views from adaptor_content and implements their id
                        holder = new ViewHolder();
                        holder.baslik = (TextView) convertView.findViewById(R.id.baslik);
                        holder.haber = (TextView) convertView.findViewById(R.id.haber);
                        holder.resim = (ImageView) convertView.findViewById(R.id.resim);
                        holder.aaa = (TextView) convertView.findViewById(R.id.aaa);
                        holder.bas=(TextView) findViewById(R.id.head_ana);
                        holder.bas.setText("Ana Sayfa");
                        convertView.setTag(holder);   
                      } else {
                        holder = (ViewHolder) convertView.getTag();
                      }

            HttpClient httpclient = new DefaultHttpClient();
            HttpGet httpget = new HttpGet("http://www.dha.com.tr/mobil/anasayfa.asp");
            HttpResponse response;

            try {
                response = httpclient.execute(httpget);
                HttpEntity entity = response.getEntity();
                if (entity != null) {

                 try {
                     InputStream instream = entity.getContent();
                     String line = "";
                     BufferedReader reader = new BufferedReader(new InputStreamReader(instream));
                     StringBuilder json = new StringBuilder();

                    while ((line = reader.readLine()) != null) {
                           json.append(line + "\n");
                     }

                  StringBuilder stringBuilder = new StringBuilder();

                  JSONArray siteler = new JSONArray(json.toString());


                       JSONObject ss = siteler.getJSONObject(position);
                       holder.baslik.setText(ss.getString("strsubject"));
                       holder.haber.setText( ss.getString("strbody") );
                           holder.resim.setImageBitmap("WHAT SHOULD I DO FOR GETTİNG IMAGE")

                  holder.aaa.setText(stringBuilder.toString());

                   instream.close();

                } catch (Exception e) {
                           holder.aaa.setText("Error is: " + e);
                }
             }

           } catch (Exception e) {
                holder.aaa.setText("Error is : " + e);
           }


                        return convertView; 
            }
    }

PLESE HELP!

igde_
  • 261
  • 3
  • 6
  • 15

5 Answers5

4

If your json contains image url you can use

String imageBaseDirectory = "http://www.dha.com.tr/newpics/news/";
String imageName = "230620111119295717933.jpg";//get image name from json parsing;
    imageview.setImageURI(Uri.parse(imageBaseDirectory+imageName));

But I can see that the url that you are referring donot contain image url only it contains name of image

Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243
  • this is the image url http://www.dha.com.tr/newpics/news/ so when i write this code imageview.setImageURI(Uri.parse('http://www.dha.com.tr/newpics/news/")); it does not work? – igde_ Jun 23 '11 at 10:25
  • 1
    as http://www.dha.com.tr/newpics/news/ is not absolute path of the image file. here name of image is missing you have to get that name from json parsing and append that filename to the directory name then pass to the usettmageURI method. see my answer now – Sunil Kumar Sahoo Jun 23 '11 at 10:39
  • thanks for information.. i tried it it is a good solution but know the problem is no image appears? but code does not give a error – igde_ Jun 23 '11 at 11:29
  • try with the hardcoded file name that i have given by using String imageBaseDirectory = "http://www.dha.com.tr/newpics/news/"; String imageName = "230620111119295717933.jpg"; imageview.setImageURI(Uri.parse(imageBaseDirectory+imageName)); – Sunil Kumar Sahoo Jun 23 '11 at 11:39
  • i tried it exactly in this way but it doesnt show me the image! – igde_ Jun 23 '11 at 11:49
  • ImageView a= (ImageView) findViewById(R.id.imageView1); String imageBaseDirectory = "www.dha.com.tr/newpics/news/"; String imageName = "230620111356175716857.jpg";//get image name from json parsing; a.setImageURI(Uri.parse(imageBaseDirectory+imageName)); – igde_ Jun 23 '11 at 12:08
  • i use this code but i got error like this 09-28 11:57:25.299: W/InputManagerService(61): Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@40543118 (uid=10013 pid=143) – Prashant09 Sep 28 '12 at 06:30
0

How to load an image from a json? Use of picasso, it is easy:

String imageUrl = imgBaseUrl + ss.getString("foto"); // this is the image url
    Picasso.with(this.getActivity()).load(imageUrl).into(holder.resim); // holder.resim is the imageview
Saeed
  • 3,294
  • 5
  • 35
  • 52
ziniestro
  • 686
  • 1
  • 11
  • 24
0

do you have image url in json ?? if so , then create another http connection for this url , get inputstream , then create bitmap from this stream . else clarify here what kind of info you have for image in your json ??

Shailendra Singh Rajawat
  • 8,172
  • 3
  • 35
  • 40
  • all these informations are in one JsonObject and the name of photo is "foto2" so if this was a text i should do holder.resim.settext(ss.getString("foto")); but it is a image so what can i do – igde_ Jun 23 '11 at 09:50
0

Your JSON contains the filename. Assuming you know the path of the images, form the url and do as Shailendra suggested, example:

URL url = new URL(imgBaseUrl + ss.getString("foto"));
URLConnection connection = url.openConnection();
FlushedInputStream fis = new FlushedInputStream(connection.getInputStream());
ByteArrayBuffer baf = new ByteArrayBuffer(100);
int current = 0;  
while((current = fis.read()) != -1){  
    baf.append((byte)current);  
}
fis.close();
holder.resim.setImageBitmap(BitmapFactory.decodeByteArray(baf, 0, baf.length()));

Be sure to use FlushedInputStream as seen at http://code.google.com/p/android/issues/detail?id=6066

static class FlushedInputStream extends FilterInputStream {
    public FlushedInputStream(InputStream inputStream) {
    super(inputStream);
    }

    @Override
    public long skip(long n) throws IOException {
        long totalBytesSkipped = 0L;
        while (totalBytesSkipped < n) {
            long bytesSkipped = in.skip(n - totalBytesSkipped);
            if (bytesSkipped == 0L) {
                  int bite = read();
                  if (bite < 0) {
                      break;  // we reached EOF
                  } else {
                      bytesSkipped = 1; // we read one byte
                  }
           }
           totalBytesSkipped += bytesSkipped;
        }
        return totalBytesSkipped;
    }
}
vonmangle
  • 113
  • 1
  • 8
0

Most of the answer is in this other post here:

How to load an imageView by URL in Android

1) create the url and pass it to the method in the link I provided

String myPhoto = "foto2";
String url = "http://mysite.com/images/" + myPhoto + ".png";
Community
  • 1
  • 1
nickfox
  • 2,835
  • 1
  • 26
  • 31