如何将图像从一个活动发送到android中的另一个活动?

时间:2015-04-06 17:03:19

标签: php android android-intent

我的数据来自使用php的数据库wampserver。我可以在activity_news中显示我的图像,并且我需要在另一个活动中显示相同的图像。我需要在另一个活动中一起显示图像和文本。怎么做

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 个答案:

答案 0 :(得分:0)

您不应向活动发送任何图像。正如@petey和@Yadav所说,您将url传递给活动并再次加载图像或将图像保存在设备(存储)上并将文件路径传递给活动并再次加载图像(从该文件路径) )

答案 1 :(得分:0)

我认为您正在使用Key KEY_THUMB_URL发送您的byteArray,即“ImagePath”并尝试使用关键的“图片”接收它,因此请尝试使用相同的密钥,即“ImagePath”或“picture”两个地方的1个密钥,它会工作的。