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

问题描述:

我的数据来自数据库wampserver使用php。我可以在activity_news中显示我的图像,并且需要在另一个活动中显示相同的图像。我需要在另一项活动中同时显示图像和文字。怎么做。如何将图像从一个活动发送到另一个android中的活动?

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); 



} 

}

+0

为什么不把图像保存到SD卡? http://*.com/questions/13577664/pass-a-bitmap-image-from-one-activity-to-another –

+0

改为传递网址。 – petey

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

我想你是发送你的byteArray使用密钥KEY_THUMB_URL即“ImagePath”,并尝试使用密钥“图片”接收它,所以尝试通过使用相同的密钥,即1键在两个地方“ImagePath”或“图片” ,它会起作用。