怎么能实现这个? [英] How can implement this ?

查看:71
本文介绍了怎么能实现这个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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();
		}
		
	}
}





我尝试过:



这是从数据库中检索图像的代码。此代码适用于单个图像。我想从网上获取多张图片。并且想要在gridview中显示这个图像。



i搜索了很多教程,但它们没有帮助bcz它从内部drawable文件夹中提取。





i成功地影响了图像路径,但我不知道如何使用此代码获取多个图像。帮助我



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

推荐答案

由于代码工作正常,我建议您稍微修改一下代码并使用下载的图像资源列表。例如,您目前正在使用此...

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);
}



这将允许您返回一些稍后可以使用的Bitmap对象。这样你就可以做同样的事情,但是在一个循环中; 这正是所需要的,除非你有更好的选择


这篇关于怎么能实现这个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆