0

I'm trying to get this resolve for days but no luck yet. Please help me kindly. I need to write a function to accepts URLs as parameter and returns for ImageView. Could someone kindly show me how to modify this code to do that? it gives me red line under imgURL.setImageBitmap(directory.getString(TAG_IMAGE));

public class ViewProfileActivity extends Activity {

    ImageView imgURL;
    TextView txtName;

    String eid; 

    private ProgressDialog pDialog;

    JSONParser jsonParser = new JSONParser();

    private static final String url_veiw_directory = "http://website.com/app/include/view_directory.php";

    private static final String TAG_SUCCESS = "success";
    private static final String TAG_DIRECTORY = "directory";
    private static final String TAG_ID = "eid";
    private static final String TAG_IMAGE = "image";
    private static final String TAG_NAME = "name";  

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_directory);

        Intent i = getIntent();

        eid = i.getStringExtra(TAG_ID);     


        new GetDirectoryDetails().execute();

    }   

    class GetDirectoryDetails extends AsyncTask<String, String, String> {

        protected String doInBackground(String... params) {

            runOnUiThread(new Runnable() {
                public void run() {

                    int success;
                    try {

                        List<NameValuePair> params = new ArrayList<NameValuePair>();
                        params.add(new BasicNameValuePair("id", eid));

                        JSONObject json = jsonParser.makeHttpRequest(url_veiw_directory, "GET", params);

                        Log.d("Single Directory Details", json.toString());

                        success = json.getInt(TAG_SUCCESS);
                        if (success == 1) {                     

                            JSONArray directoryObj = json.getJSONArray(TAG_DIRECTORY);  
                            JSONObject directory = directoryObj.getJSONObject(0);

                            imgURL = (ImageView) findViewById(R.id.image);
                            txtName = (TextView) findViewById(R.id.name);                       


                            imgURL.setImageBitmap(directory.getString(TAG_IMAGE));
                            txtName.setText(directory.getString(TAG_NAME));                         


                        }else{

                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });

            return null;
        }

        protected void onPostExecute(String file_url) {
            pDialog.dismiss();
        }

    }
}
Ahmad
  • 69,608
  • 17
  • 111
  • 137
etrademom
  • 221
  • 4
  • 12

2 Answers2

3

http://developer.android.com/reference/android/widget/ImageView.html#setImageBitmap%28android.graphics.Bitmap%29

There is no setImageBitmap()-method in ImageView, that accepts a string as a parameter. You have to supply a Bitmap.

Instead, you could use the BitmapFactory to create your bitmap, like this:

setImageBitmap(BitmapFactory.decodeFile(directory.getString(TAG_IMAGE)));

EDIT: Sorry, forgot that it was a URL you wanted to decode. See @MarchingHome answer for this.

Florian Minges
  • 586
  • 3
  • 9
  • could you give me some sample kindly? – etrademom Oct 16 '12 at 20:35
  • 1
    `setImageBitmap(BitmapFactory.decodeFile(directory.getString(TAG_IMAGE)));` Look here for more info about BitmapFactory: http://developer.android.com/reference/android/graphics/BitmapFactory.html – Florian Minges Oct 16 '12 at 20:44
2

I think I see the problem. ImageView.setImageBitmap needs a Bitmap object as argument. A String won't do. You should try to load the JSONObject as a Bitmap object.

This could be the answer you are looking for: Loading a Bitmap from URL

Community
  • 1
  • 1
MarchingHome
  • 1,184
  • 9
  • 15