Php 在android中,如何将图像从一个活动发送到另一个活动?
我的数据来自使用php的数据库wampserver。我可以在“活动\新闻”中显示我的图像,也可以在其他活动中显示我需要的图像。我需要在另一个活动中同时显示图像和文本。如何做到这一点 javaPhp 在android中,如何将图像从一个活动发送到另一个活动?,php,android,android-intent,Php,Android,Android Intent,我的数据来自使用php的数据库wampserver。我可以在“活动\新闻”中显示我的图像,也可以在其他活动中显示我需要的图像。我需要在另一个活动中同时显示图像和文本。如何做到这一点 java private String URL = ""; // XML node keys static final String KEY_NEWS = "news"; // parent node // static final String KEY_ID = "id"; static final String
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;
}
}
}您不应向活动发送任何图像。正如@petey和@Yadav所说的,您可以将url传递给活动并再次加载图像,或者将图像保存在设备(存储器)上,并将文件路径传递给活动并再次加载图像(从该文件路径)我想您是在使用Key\u THUMB\u url发送byteArray,即“ImagePath”,并尝试使用Key接收它“图片”,因此尝试使用相同的键,即在“ImagePath”或“picture”两个位置都使用1键,它会起作用。为什么不将图像保存到sd卡中?请传递url。
@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);
}