问题
I'm doing one demo of getting Image URLs through JSON and after getting the ULR I'm downloading that images and display them into the GridView. For storing downloaded images I'm using Bitmap array and passing it to ImageAdapter. And now I want to display that images on next activity on ImageView for that I'm using following code in second activity..
// 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]);
but It giving me error like The method setImageDrawable(Drawable) in the type ImageView is not applicable for the arguments (Bitmap). Please tell me how can I set the Image from Bitmap array to ImageView..
ImageAdapter Class:
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;
}
}
Main Class:
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 Class:
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]);
}
}
回答1:
Its so simple way use setImageBitmap..
imageView.setImageBitmap(imageAdapter.bm[position]);
回答2:
You should use , setImageBitmap (Bitmap bm) method which Sets a Bitmap as the content of this ImageView.
So your code will be,
imageView.setImageBitmap(imageAdapter.bm[position]);
Docs.
回答3:
imageView.setImageBitmap(imageAdapter.bm[position]);
回答4:
here i give you my code of my app that works with a gallery from drawable resources with int position, modify it for your needs, should work with your code.
My intent of gallery is:
@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);
}
In the main activity i receive the intent and set it on the imageview:
Intent avatar = getIntent();
int imageId = avatar.getIntExtra("imageId", -0);
// -0 would be a default value
// -1 will crash with a missing resource
if (getIntent().getExtras() != null) {
Avatar.setImageResource(imageId);
savedAvatar.setImageResource(imageId);
} else {
Avatar.setImageResource(R.mipmap.ic_launcher);
}
Your code is similiar as mine, but you get it from url them to bitmap, me i get it from drawable resources, with long id list set on the activity java for name and position, i chose the one i want to set as avatar them pass it inside intent to main activity and define the intent name that contains the drawable cache to an imageview, after in button onclick i convert to bitmap, bitmap to string, string to image to restore imageview view on start main activity, and i save the string to sharedpreferences, getting the imageview view back again even after close the app.
Good luck
来源:https://stackoverflow.com/questions/22217690/how-to-set-imageview-using-bitmap-array