怎么能实现这个?

编程入门 行业动态 更新时间:2024-10-10 07:32:28
本文介绍了怎么能实现这个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

package com.example.imagefromfolder; import java.io.InputStream; import java.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 = "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 = "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("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对象。这样你就可以做同样的事情,但是在一个循环中; 这正是所需要的,除非你有更好的选择。

更多推荐

怎么能实现这个?

本文发布于:2023-11-29 02:33:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1644954.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!