Click here to Skip to main content
15,916,842 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Java
package com.example.imagefromfolder;

import java.io.InputStream;
import java.net.URL;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {

	ImageView img;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		img = (ImageView)findViewById(R.id.img);
		new GetImage().execute();	
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	class GetImage extends AsyncTask<Void, Void, Bitmap >
	{
		ProgressDialog pdia;
		@Override
		protected void onPreExecute() {
		// TODO Auto-generated method stub
			super.onPreExecute();
			pdia = new ProgressDialog(MainActivity.this);
			pdia.setCanceledOnTouchOutside(false);
	        pdia.setMessage("Fetching ...!");
	        pdia.show(); 
		}

		@Override
		protected Bitmap doInBackground(Void... params) {
			// TODO Auto-generated method stub
			try {
				
				String link = "http://192.168.0.106/productiveplus/shopowner/Uploads/Diwalli15%off.png" ;
				
				URL url = new URL(link);
				HttpGet httpRequest = null;

				httpRequest = new HttpGet(url.toURI());

				HttpClient httpclient = new DefaultHttpClient();
				HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);

				HttpEntity entity = response.getEntity();
				BufferedHttpEntity b_entity = new BufferedHttpEntity(entity);
				InputStream input = b_entity.getContent();

				Bitmap bitmap = BitmapFactory.decodeStream(input);
				return bitmap;
				
				} 
				catch (Exception e) 
				{
					
				// TODO Auto-generated catch block
				Toast.makeText(MainActivity.this, e.toString(), Toast.LENGTH_LONG).show();
				e.printStackTrace();
				return null;
				}
			
						
		}
	
		@Override
		protected void onPostExecute(Bitmap bitmap) {
			// TODO Auto-generated method stub
			super.onPostExecute(bitmap);
			
				try
				{
					img.setImageBitmap(bitmap);
				}
				catch(Exception ex)
				{
					Toast.makeText(MainActivity.this, "No Image Found", Toast.LENGTH_SHORT).show();
				}
			pdia.dismiss();
		}
		
	}
}


What I have tried:

this is the code for retrieving an image from database. this code works fine for a single image. i wants to fetch multiple images from web. and wants do display this image in gridview.

i searched verious of tutorials but they are not helpful bcz it fetches from internal drawable folder.


i fatched the image path succesfully but i dont have any idea to how to fetch multiple images from using this code. help me
Posted
Updated 13-Apr-16 21:29pm

1 solution

Since the code works all fine, I would recommend that you just alter the code a bit and use a list of the image resources to download. For example, you are currently having this...
String link = "http://192.168.0.106/productiveplus/shopowner/Uploads/Diwalli15%off.png";
URL url = new URL(link);

You can update this to work as an array; list of the String objects. For example, this would do the trick for you:
ArrayList<string> links;
links.add("http://192.168.0.106/productiveplus/shopowner/Uploads/Diwalli15%off.png");
// Add other links

// Run the loop
for (String link : links) {
    // Download the images and then return the Bitmap collection/
    URL url = new URL(link);
}

This would allow you to return a number of Bitmap objects, that you can later use. This way you would do the same thing, but in a loop; that is exactly what is required, unless you have a better option.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900