我是android的初学者,在我需要将此图像发送到gridview中的另一个actitivty之后,我将图像的url保存在singleton中。 请有人可以帮助我。
答案 0 :(得分:0)
您的示例:Example
答案 1 :(得分:0)
首先通过当前Activity
的{{1}}将您的图片网址发送给另一个Intent
。喜欢 -
activity
从NextActivity接收您的图片网址。喜欢 -
Intent intent = new Intent(currentActivity, nextActivity);
input.putExtra("urlKey", "urlValue");
startActivity(intent);
使用第三方库从web服务器/ url加载图像进行查看。看看Picasso。在Intent intent = getIntent();
String imageUrl = intent.getStringExtra("urlKey");
Picasso
上使用Adapter
。前 -
GridView
答案 2 :(得分:0)
如果您使用像毕加索这样的第三方库,那么您可以通过几行代码实现目标 执行以下步骤:
dependencies {compile'com.squareup.picasso:picasso:2.5.2'}
语法:
Picasso.with(this) .load("YOUR IMAGE URL HERE") .into(imageView);
例如
Picasso.with(this)
.load("https://www.samplecode.co/wp-content/uploads/2015/10/advertise.png")
.into(imageView);
对于gridview,您创建一个适配器并使用该位置加载图像,请参阅此适配器类
import android.app.Activity; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.RatingBar; import android.widget.TextView;
import com.drapp.BaseDrawerActivity; import com.drapp.R; import com.squareup.picasso.Picasso;
import java.util.ArrayList;
公共类GridViewAdapter扩展了RecyclerView.Adapter { 私人活动活动;
Context context;
ArrayList<String> image = new ArrayList<String>();
ArrayList<String> name = new ArrayList<String>();
private static LayoutInflater inflater = null;
public GridViewAdapter(BaseDrawerActivity mainActivity, ArrayList<String> thumbnail, ArrayList<String> dish_name) {
this.activity = mainActivity;
this.context = mainActivity;
image = thumbnail;
name = dish_name;
}
public int getCount() {
//return textnew1.length;
int s = name.size();
return s;
}
public Object getItem(int position) {
return position;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.grid_item_layout, parent, false);
// set the view's size, margins, paddings and layout parameters
ViewHolder vh = new ViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.productnametv.setText(name.get(position));
if (image.get(position).equals("")) {
holder.productimg.setImageResource(R.drawable.heart);
holder.productimg.setVisibility(View.INVISIBLE);
} else {
Picasso.with(context).load(image.get(position)).into(holder.productimg);
}
}
public long getItemId(int position) {
return position;
}
@Override
public int getItemCount() {
return name.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView productnametv;
public ImageView productimg;
public RatingBar rating;
public ViewHolder(View itemView) {
super(itemView);
productnametv = (TextView) itemView.findViewById(R.id.grid_item_label);
productimg = (ImageView) itemView.findViewById(R.id.grid_item_image);
}
}
}
答案 3 :(得分:0)
如果您不想使用第三方库,请尝试此操作
new DownloadImage(imamgeview).execute(userProfileUrl);
创建异步任务
public class DownloadImage extends AsyncTask<String, Void, Bitmap> {
CircleImageView bmImage;
public DownloadImage(ImageView bmImage) {
this.bmImage = (CircleImageView) bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.d("Error", e.getStackTrace().toString());
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
我希望你能在你的案例中发挥作用