|
|
|
Android - รูปภาพไม่แสดงครับ รบกวนช่วยดูให่หน่อยครับ แก้ไขยังไงดี |
|
|
|
|
|
|
|
Code (Android-Java)
public class SearchTemple extends Activity {
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_temple);
// Permission StrictMode
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
final Button btn1 = (Button) findViewById(R.id.button1);
// Perform action on click
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
SearchData();
}
});
}
public void SearchData()
{
// listView1
final ListView lisView1 = (ListView)findViewById(R.id.listView1);
// editText1
final EditText inputText = (EditText)findViewById(R.id.editText1);
String url = "http://appsofteng.com/student7/Project/templeimage/getjsontemple.php";
// Paste Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("txtKeyword", inputText.getText().toString()));
try {
JSONArray data = new JSONArray(getJSONUrl(url,params));
final ArrayList<HashMap<String, String>> MyArrList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
for(int i = 0; i < data.length(); i++){
JSONObject c = data.getJSONObject(i);
map = new HashMap<String, String>();
map.put("TempleID", c.getString("TempleID"));
map.put("Name", c.getString("Name"));
map.put("Address", c.getString("Address"));
map.put("Lattitude", c.getString("Lattitude"));
map.put("Longtitude", c.getString("Longtitude"));
map.put("Image", c.getString("Image"));
MyArrList.add(map);
}
SimpleAdapter sAdap;
sAdap = new SimpleAdapter(SearchTemple.this, MyArrList, R.layout.column_temple,new String[] {"TempleID", "Name","Address"}, new int[] {R.id.ColID, R.id.ColName,R.id.colAdd});
lisView1.setAdapter(sAdap);
final AlertDialog.Builder viewDetail = new AlertDialog.Builder(this);
final LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
// OnClick Item
lisView1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,int position, long id) {
View layout = inflater.inflate(R.layout.image_temple, (ViewGroup) findViewById(R.id.layout_root));
ImageView image = (ImageView) layout.findViewById(R.id.fullimage);
try
{
image.setImageBitmap(loadBitmap(MyArrList.get(position).get("Image")));
}catch (Exception e){
//When Error
image.setImageResource(android.R.drawable.ic_menu_report_image);
}
String strTempleID = MyArrList.get(position).get("TempleID").toString();
String strName = MyArrList.get(position).get("Name").toString();
String strAddress = MyArrList.get(position).get("Address").toString();
String strLattitude = MyArrList.get(position).get("Lattitude").toString();
String strLongtitude = MyArrList.get(position).get("Longtitude").toString();
viewDetail.setIcon(android.R.drawable.btn_star_big_on);
viewDetail.setTitle("Temple Detail");
viewDetail.setMessage("TempleID : " + strTempleID + "\n"
+ "Name : " + strName + "\n"
+ "Address : " + strAddress + "\n"
+ "Lattitude : " + strLattitude + "\n"
+ "Longtitude : " + strLongtitude);
viewDetail.setView(layout);
viewDetail.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
viewDetail.show();
viewDetail.create();
}
});
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String getJSONUrl(String url,List<NameValuePair> params) {
StringBuilder str = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
try {
httpPost.setEntity(new UrlEncodedFormEntity(params,"UTF-8"));
HttpResponse response = client.execute(httpPost);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) { // Download OK
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
str.append(line);
}
} else {
Log.e("Log", "Failed to download file..");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return str.toString();
}
/***** Get Image Resource from URL (Start) *****/
private static final String TAG = "ERROR";
private static final int IO_BUFFER_SIZE = 4 * 1024;
public static Bitmap loadBitmap(String url) {
Bitmap bitmap = null;
InputStream in = null;
BufferedOutputStream out = null;
try {
in = new BufferedInputStream(new URL(url).openStream(), IO_BUFFER_SIZE);
final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
copy(in, out);
out.flush();
final byte[] data = dataStream.toByteArray();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 1;
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,options);
} catch (IOException e) {
Log.e(TAG, "Could not load Bitmap from: " + url);
} finally {
closeStream(in);
closeStream(out);
}
return bitmap;
}
private static void closeStream(Closeable stream) {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
android.util.Log.e(TAG, "Could not close stream", e);
}
}
}
private static void copy(InputStream in, OutputStream out) throws IOException {
byte[] b = new byte[IO_BUFFER_SIZE];
int read;
while ((read = in.read(b)) != -1) {
out.write(b, 0, read);
}
}
/***** Get Image Resource from URL (End) *****/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
ผมนั่งงมมาหลายวันครับ ทำไมรูปภาพมันถึงไม่ออก รบกวนช่วยดูให้หน่อยครับ
Tag : Mobile, MySQL, Android
|
|
|
|
|
|
Date :
2014-01-08 09:50:51 |
By :
phuwadonlove |
View :
1424 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
มันมี Error น่ะครับ เห็นไหม๊ครับ ลองดูพวก Path ให้ถูกต้องด้วยครับ
|
|
|
|
|
Date :
2014-01-08 10:14:07 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 02
|