我有以下代码,其中我得到了id,名称和图像路径。
我想在图像视图中显示图像路径。
public class AllProductsActivity extends ListActivity {
String pid;
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> productsList;
// url to get all products list
private static String url_all_products = "example.com/files/get_all_products.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "products";
private static final String TAG_PID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_PRICE = "price";
private static final String TAG_IMAGE1 = "image1";
// products JSONArray
JSONArray products = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.all_products);
// Hashmap for ListView
productsList = new ArrayList<HashMap<String, String>>();
// getting product details from intent
Intent i = getIntent();
// getting product id (pid) from intent
pid = i.getStringExtra(TAG_PID);
// Loading products in Background Thread
new LoadAllProducts().execute();
// Get listview
ListView lv = getListView();
// on seleting single product
// launching Edit Product Screen
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String pid = ((TextView) view.findViewById(R.id.pid)).getText()
.toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(),
EditProductActivity.class);
// sending pid to next activity
in.putExtra(TAG_PID, pid);
// starting new activity and expecting some response back
startActivityForResult(in, 100);
}
});
}
// Response from Edit Product Activity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// if result code 100
if (resultCode == 100) {
// if result code 100 is received
// means user edited/deleted product
// reload this screen again
Intent intent = getIntent();
finish();
startActivity(intent);
}
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(AllProductsActivity.this);
pDialog.setMessage("Loading products. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("id", pid));
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_products, "GET",
params);
// Check your log cat for JSON reponse
Log.d("All Products: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
products = json.getJSONArray(TAG_PRODUCTS);
// looping through All Products
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_PID);
String name = c.getString(TAG_NAME);
String price = c.getString(TAG_PRICE);
String image1 = c.getString(TAG_IMAGE1);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_PID, id);
map.put(TAG_NAME, name);
map.put(TAG_PRICE, price);
map.put(TAG_IMAGE1, image1);
// adding HashList to ArrayList
productsList.add(map);
}
} else {
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
AllProductsActivity.this, productsList,
R.layout.list_row, new String[] { TAG_PID,
TAG_NAME, TAG_PRICE, TAG_IMAGE1 },
new int[] { R.id.pid, R.id.name, R.id.price,
R.id.thumbnail });
// updating Listview
setListAdapter(adapter);
}
});
}
}
}
我已经在列表适配器中传递了image1
路径,并且已经在.xml文件中获取了imageview。
但我没有在他们身上获得蚂蚁图像。
list_row.xml containg image view:
<ImageView
android:id="@+id/thumbnail"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_marginRight="8dp" />
如何在imageview中显示图像?
答案 0 :(得分:2)
答案 1 :(得分:1)
Picasso在另一个帖子中下载图片并为您管理:
这非常简单,这是一个例子:
Picasso.with(context)
.load(url)
.placeholder(R.drawable.placeholder)
.resize(imgWidth, imgHeight)
.centerCrop()
.into(image);
答案 2 :(得分:1)
您需要执行以下步骤:
1)您需要实现一个自定义适配器,它将根据该行上相应的URL延迟加载图像来填充图像
2)实现一个LazyLoader,可以在完成后下载图像并刷新imageview
3)调用自定义适配器的getView方法时调用LazyLoading
4)在OnPostExecute中调用自定义适配器,而不是Listadapter
5)或者为了使LazyLoading部分更容易,您使用Universal Image Loader
进行编码答案 3 :(得分:0)
下载picasso jar文件并首先添加android build application jar:
现在只需从json获取你的网址
if(response.login_data.avtar!= null&amp;&amp; response.login_data.avtar.length()&gt; 0){
String photo=response.login_data.avtar.toString();
infoLog("avatar"+photo);
String url=App.WEB_PHOTO_URL+photo;
infoLog("avatar url"+url);
Picasso.with(HealthProfile.this).load(url).into(img);
}
else {
Picasso.with(HealthProfile.this).load(R.drawable.user).into(img);
}
答案 4 :(得分:0)
ImageView imageView=(ImageView)findViewById(R.id.animage);
Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
if (imageUri != null) {
String filePath = null;
filePath = imageUri.getPath();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
Bitmap bitmap = BitmapFactory.decodeFile(filePath,options);
imageView.setImageBitmap(bitmap);
}
答案 5 :(得分:0)
您只需使用 Picasso lib,这是一款功能强大的Android图像下载和缓存库。
它提供了一种超级干净的语法,因此您可以使用它而不会产生混淆。
例如,在您的适配器中,您尝试这样:
首先,如果您使用 SimpleAdapter ,则应该知道 ViewBinder 。详情见http://developer.android.com/reference/android/widget/SimpleAdapter.ViewBinder.html
然后,在您自己的ViewBinder的 setViewValue 中,只需添加以下代码:
Picasso.with(context).load(img_path).into(imgView)
或者,如果您自定义自己的适配器,请在 getView 功能中添加替代代码。
最后,它会在您的图像视图中显示图像。
关于毕加索的更多细节:http://square.github.io/picasso/
通用图片加载器是您的另一种选择,它也拥有众多用户。它更具可定制性。请参阅:https://github.com/nostra13/Android-Universal-Image-Loader
答案 6 :(得分:0)
太晚了但也许它可以帮助别人。
如果您可以在项目中使用某个图片加载库,只需使用此自定义图片视图以允许从xml设置网址。
由于您可以从xml设置url,因此可以直接将链接变量绑定到imageview。
创建自定义图像视图。
class MyImageView : ImageView {
var url: String = ""
set(value){
field = value
Glide.with(this).load(value).into(this)
}
constructor(context: Context) : super(context)
constructor(context: Context, attrs : AttributeSet) : super(context,attrs){
val array = context.obtainStyledAttributes(attrs, R.styleable.MyImageView)
val url = array.getString(R.styleable.MyImageView_url)
if(url!=null){
Glide.with(this).load(url).into(this)
}
array.recycle()
}
constructor(context: Context, attrs: AttributeSet , defStyleAttr : Int) : super(context, attrs, defStyleAttr)
}
在attr.xml中添加
<declare-styleable name="MyImageView">
<attr name="url" format="string" />
</declare-styleable>
在xml
中使用它<package.path.MyImageView
android:id="@+id/image_company_logo"
android:scaleType="fitCenter"
android:layout_width="match_parent"
android:layout_height="80dp"
android:contentDescription="logo"
app:url="@{invoice.invoiceLogoLink}"/> //for data binding purpose
详细了解here
答案 7 :(得分:-1)
您需要创建自定义适配器,为行布局充气并从布局中检索ImageView。然后,您可以通过编程方式设置ImageView,如:
File imgFile = new File(yourImagePath);
if(imgFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
yourImageView.setImageBitmap(myBitmap);
}
创建自己的适配器的教程:http://www.vogella.com/tutorials/AndroidListView/article.html