我正在做一个通过JSON
获取图片网址的演示,并在获得ULR
我下载图片后将其显示在GridView
中。为了存储下载的图像,我正在使用Bitmap
数组并将其传递给ImageAdapter
。现在我想在ImageView
上的下一个活动中显示这些图像,因为我在第二个活动中使用了以下代码。
// get intent data
Intent i = getIntent();
// Selected image id
int position = i.getExtras().getInt("id");
ImageAdapter imageAdapter = new ImageAdapter(this);
ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
imageView.setBackgroundDrawable(imageAdapter.bm[position]);
但它给我的错误如The method setImageDrawable(Drawable) in the type ImageView is not applicable for the arguments (Bitmap)
。请告诉我如何将图像从Bitmap
数组设置为ImageView
..
ImageAdapter类:
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public Bitmap bm[]=new Bitmap[4];
ImageView imageView;
// Constructor
public ImageAdapter(Context c, Bitmap[] bm){
mContext = c;
this.bm = bm;
}
public ImageAdapter(Context c) {
mContext = c;
}
@Override
public int getCount() {
return bm.length;
}
@Override
public Object getItem(int position) {
return bm[position];
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
imageView = new ImageView(mContext);
imageView.setImageBitmap(bm[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
return imageView;
}
}
主类:
public class AndroidGridLayoutActivity extends Activity {
public static final Bitmap bm[]=new Bitmap[4];
public static ImageAdapter img;
public static String[] imageurls;
public static GridView gridView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_layout);
Log.d("LOG", "1");
new GetImageUrls().execute();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.d("LOG", "7");
gridView = (GridView) findViewById(R.id.grid_view);
// Instance of ImageAdapter Class
Log.d("LOG", "8");
img = new ImageAdapter(getApplicationContext(), bm);
Log.d("LOG", "9");
gridView.setAdapter(img);
Log.d("LOG", "10");
/**
* On Click event for Single Gridview Item
* */
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Log.d("LOG", "11");
// Sending image id to FullScreenActivity
Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
Log.d("LOG", "12");
// passing array index
i.putExtra("id", position);
startActivity(i);
}
});
}
public class GetImageUrls extends AsyncTask<Void, Void, Void>
{
Context context;
private ProgressDialog pDialog;
// URL to get JSON
private static final String url= "http://xxx.x.xxx/demo/test.json";
private static final String MAIN = "mainCategory";
private static final String IMAGE = "mcatimage";
// JSONArray
JSONArray loginjsonarray=null;
//result from url
protected void onPreExecute() {
Log.d("LOG", "2");
// Showing progress dialog
pDialog = new ProgressDialog(getApplicationContext());
pDialog.setMessage("Loading...");
pDialog.setCancelable(false);
//pDialog.show();
Log.d("LOG", "3");
}
protected Void doInBackground(Void... arg) {
Log.d("LOG", "4");
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
Log.d("LOG", "5");
// Making a request to url and getting response
String jsonstr = sh.makeServiceCall(url, ServiceHandler.POST, null);
Log.d("Response: ", ">"+jsonstr);
if(jsonstr!=null)
{
try {
JSONObject jsonObj =new JSONObject(jsonstr);
loginjsonarray=jsonObj.getJSONArray(MAIN);
imageurls=new String[loginjsonarray.length()];
for(int i=0;i<loginjsonarray.length();i++){
JSONObject l=loginjsonarray.getJSONObject(i);
imageurls[i]=l.getString(IMAGE);
Log.d("imageurls: ", imageurls[i]);
}
for(int i=0;i<imageurls.length;i++)
{
bm[i]=DownloadImage(imageurls[i]);
Log.d("LOG", bm[i].toString());
}
} catch (JSONException e) {
e.printStackTrace();
}
}else{
Toast.makeText(context,"Check your Internet Connection",Toast.LENGTH_SHORT).show();
}
return null;
}
public Bitmap DownloadImage(String STRURL) {
Bitmap bitmap = null;
InputStream in = null;
try {
int response = -1;
URL url = new URL(STRURL);
Log.d("DownloadImage: ", url.toString());
URLConnection conn = url.openConnection();
if (!(conn instanceof HttpURLConnection))
throw new IOException("Not an HTTP connection");
try{
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
response = httpConn.getResponseCode();
Log.d("DownloadImage response: ", Integer.toString(response));
if (response == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
Log.d("DownloadImage: ", in.toString());
}
} catch (Exception ex) {
throw new IOException("Error connecting");
}
bitmap = BitmapFactory.decodeStream(in);
Log.d("DownloadImage Bitmap: ", bitmap.toString());
in.close();
}catch (IOException e1) {
e1.printStackTrace();
}
Log.d("LOG", "6");
return bitmap;
}
protected void onPostExecute(Integer result) {
// Dismiss the progress dialog
if(pDialog.isShowing()){
pDialog.dismiss();
}
}
}
}
FullActivity类:
public class FullImageActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.full_image);
// get intent data
Intent i = getIntent();
// Selected image id
int position = i.getExtras().getInt("id");
Log.d("Position", Integer.toString(position));
ImageAdapter imageAdapter = new ImageAdapter(this);
ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
imageView.setImageBitmap(imageAdapter.bm[position]);
}
}
答案 0 :(得分:3)
使用setImageBitmap ..
这么简单imageView.setImageBitmap(imageAdapter.bm[position]);
答案 1 :(得分:1)
您应该使用 setImageBitmap(Bitmap bm)方法,该方法将位图设置为此ImageView的内容。
所以你的代码将是,
imageView.setImageBitmap(imageAdapter.bm[position]);
答案 2 :(得分:1)
imageView.setImageBitmap(imageAdapter.bm[position]);
答案 3 :(得分:0)
在这里,我向您提供我的应用程序代码,该应用程序可以使用带有int位置的可绘制资源的库,根据您的需要进行修改,应该使用您的代码。
我的画廊意图是:
Linking.openURL('https://www.google.com/maps/search/?api=1&query=centurylink+field');
在主要活动中,我收到意图并将其设置在imageview上:
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int selectedId = imgs[position];
Intent intent = new Intent(Gallery3DActivity.this,MainActivity.class);
intent.putExtra("imageId", selectedId);
startActivity(intent);
}
你的代码与我的类似,但你从url到bitmap得到它,我从drawable资源得到它,在活动java上为名称和位置设置了长id列表,我选择了我想设置的那个作为头像,他们将意图传递给主要活动,并将包含可绘制缓存的意图名称定义到imageview,在按钮onclick后转换为位图,位图转换为字符串,字符串转换为图像以恢复启动主活动上的imageview视图,以及我将字符串保存到共享首选项,即使在关闭应用程序后再次获取imageview视图。
祝你好运