-1

my data is coming from database wampserver using php. I can display my image in activity_news and the same image i need to display in another activity. I need to display image and text both together in another activity. how to do so.

News.java

private String URL = "";
// XML node keys
static final String KEY_NEWS = "news"; // parent node
// static final String KEY_ID = "id";
static final String KEY_TITLE = "Title";
// static final String KEY_SUBTITLE = "SubTitle";
static final String KEY_Details = "Details";
static final String KEY_THUMB_URL = "ImagePath";

ListView list;
LazyAdapterNews adapter;

JSONArray news = null;
ServiceHandler sh;
String jsonStr;
ArrayList<HashMap<String, String>> newList;

public News() {

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.activity_news, container,
            false);

    URL = getResources().getString(R.string.server_url) + "/new_fetch.php";
    newList = new ArrayList<HashMap<String, String>>();
    ImageView img = (ImageView) rootView.findViewById(R.id.imageView1);
    list = (ListView) rootView.findViewById(R.id.listView1);

    sh = new ServiceHandler();

    new getData(this).execute();

    list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
            byte[] byteArray = stream.toByteArray();

            Intent i = new Intent(getActivity(), Prac.class);

            String name = ((TextView) view.findViewById(R.id.Title))
                    .getText().toString();
            String details = ((TextView) view.findViewById(R.id.Details))
                    .getText().toString();





            i.putExtra(KEY_TITLE, name);
            i.putExtra(KEY_Details, details);
            i.putExtra(KEY_THUMB_URL,byteArray);

            startActivity(i);

        }
    });

    return rootView;
}

private class getData extends AsyncTask<Void, Void, Void> {

    private News activity;

    public getData(News news) {
        this.activity = news;
    }

    @Override
    protected void onPostExecute(Void result) {
        if (jsonStr != null) {
            Log.i("jsonstr", jsonStr);
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);
                news = jsonObj.getJSONArray(KEY_NEWS);
                for (int i = 0; i < news.length(); i++) {
                    JSONObject c = news.getJSONObject(i);
                    String Title = c.getString(KEY_TITLE);
                    String Details = c.getString(KEY_Details);
                    String path = c.getString(KEY_THUMB_URL);

                    HashMap<String, String> newss = new HashMap<String, String>();

                    newss.put(KEY_TITLE, Title);
                    newss.put(KEY_Details, Details);
                    newss.put(KEY_THUMB_URL, path);

                    newList.add(newss);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            Log.e("ServiceHandler", "Couldn't get any data from the url");
        }

        // Getting adapter by passing xml data ArrayList
        adapter = new LazyAdapterNews(getActivity(), newList);
        list.setAdapter(adapter);

        super.onPostExecute(result);
    }

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(Void... params) {
        jsonStr = sh.makeServiceCall(URL, ServiceHandler.GET);
        Log.d("Response: ", "> " + jsonStr);
        return null;
    }

}

}

Prac.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_prac);
    String title = "";
    String details = "";
    Bundle extras = getIntent().getExtras();
    byte[] byteArray = extras.getByteArray("picture");

    Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);

    Intent intent = getIntent();
    if (null != intent) {
        title = intent.getStringExtra(KEY_TITLE);
        details = intent.getStringExtra(KEY_Details);
        Bundle b = getIntent().getExtras();
        String picturePath = b.getString("picture");
        //Bitmap bmp;



    }

    TextView headlineTxt = (TextView) findViewById(R.id.headlines);
    headlineTxt.setText(title);


    TextView descriptionTxt = (TextView) findViewById(R.id.description);
    descriptionTxt.setText(details);

    ImageView img = (ImageView)findViewById(R.id.imgdetails);
    img.setImageBitmap(bmp);



}

}

2 Answers2

0

You should not send any images from/to activities. As @petey and @Yadav were saying, you eiter pass the url to the activity and load the image again or save the image on the device (storage) and pass the file path to the acitivity and load the image again (from that file path)

0

I think you are sending your byteArray using Key KEY_THUMB_URL i.e. "ImagePath" and trying to receive it using key "picture", So try by using same key i.e. 1 key at both places either "ImagePath" or "picture",It will work.

Nitin Mali
  • 226
  • 1
  • 7