From 6f31555cf8235b6eb68f6806995db8bebe6e606d Mon Sep 17 00:00:00 2001 From: YLiuLiu <851942035@qq.com> Date: Thu, 16 Jun 2016 01:15:26 +0800 Subject: [PATCH 1/3] =?UTF-8?q?#93=20#94=20#95=20#76=20=E5=AE=9E=E9=AA=8C?= =?UTF-8?q?=E5=B7=B2=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Com1314080901239Activity.java | 141 +++++++++++++---- .../Com1314080901239GetJson.java | 144 ++++++++++++++++++ 2 files changed, 255 insertions(+), 30 deletions(-) create mode 100644 app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901239/Com1314080901239GetJson.java diff --git a/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901239/Com1314080901239Activity.java b/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901239/Com1314080901239Activity.java index 2780b839..ca8bee90 100644 --- a/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901239/Com1314080901239Activity.java +++ b/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901239/Com1314080901239Activity.java @@ -3,46 +3,127 @@ import java.io.File; import android.app.Activity; +import android.content.ContentResolver; import android.content.Intent; +import android.database.Cursor; +import android.graphics.Bitmap; +import android.graphics.drawable.BitmapDrawable; +import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; import android.view.View; +import android.widget.ImageView; import android.widget.Toast; - public class Com1314080901239Activity extends Activity { + + private final String IMAGE_TYPE = "image/*"; + private final int PICK_PHOTO = 1; // 选择照片 + private final int TAKE_PHOTO = 0; // 拍照 + private final int CUT_PHOTO = 2;// 裁剪图片 private Uri fileUri; + private ImageView iv; + private String path; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_com1314080901239); + iv = (ImageView) findViewById(R.id.iv); + } + + // 拍照 + public void takePhoto(View v) { + + // 启动一个意图。这里就是调用google给我们提供的,手机自带的拍照应用 + Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); + // 设置存储路径 + intent.putExtra(MediaStore.EXTRA_OUTPUT, + // 设置图片存储路径及名字 + Uri.fromFile(new File("sdcard/nuna.jpg"))); + + startActivityForResult(intent, TAKE_PHOTO); + + } + + public void pickPhoto(View v) { + // 调用系统提供的相册功能 + Intent intent = new Intent(Intent.ACTION_GET_CONTENT); + intent.setType(IMAGE_TYPE); + startActivityForResult(intent, PICK_PHOTO); + + } + + public void cutPhoto(View v) { + Cutphoto(); + } - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_com1314080901239); - } - // 点击事件 - public void takePhoto(View v) { - // create Intent to take a picture and return control to the calling - // application - // 启动一个意图。这里就是调用google给我们提供的,手机自带的拍照应用 - Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); - // 设置存储路径 - intent.putExtra(MediaStore.EXTRA_OUTPUT, - Uri.fromFile(new File("sdcard/nuna.jpg"))); // set the image - // file name - - // start the image capture Intent - startActivityForResult(intent, 0); - - } - - @Override - protected void onActivityResult(int requestCode, int resultCode, Intent data) { - // TODO Auto-generated method stub - super.onActivityResult(requestCode, resultCode, data); - if (requestCode == 0) { - Toast.makeText(getApplicationContext(), "拍照成功", 0).show(); - } - } + public void getJson(View v){ + Intent intent=new Intent(Com1314080901239Activity.this,Com1314080901239GetJson.class); + startActivity(intent); + } + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + // TODO Auto-generated method stub + super.onActivityResult(requestCode, resultCode, data); + Bitmap bm = null; + ContentResolver resolver = getContentResolver(); + if (requestCode == TAKE_PHOTO) { + Toast.makeText(getApplicationContext(), "拍照成功", 0).show(); + } else if (requestCode == PICK_PHOTO) { + Uri originalUri = data.getData(); + try { + bm = MediaStore.Images.Media.getBitmap(resolver, originalUri); + iv.setImageBitmap(bm); + String[] proj = { MediaStore.Images.Media.DATA }; + Cursor cursor = managedQuery(originalUri, proj, null, null, + null); + int column_index = cursor + .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); + + cursor.moveToFirst(); + // 最后根据索引值获取图片路径 + path = cursor.getString(column_index); + + + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } else if (requestCode == CUT_PHOTO) { + if (resultCode == RESULT_OK) { + savePhoto(data); + } + } + } + + // 裁剪图片的方法 + public void Cutphoto() { + Intent intent = new Intent("com.android.camera.action.CROP"); + //intent.setDataAndType(uri, IMAGE_TYPE); + intent.putExtra("crop", "true"); + intent.putExtra("aspectX", 1); + intent.putExtra("aspectY", 1); + intent.putExtra("outoutX", 150); + intent.putExtra("outputY", 150); + intent.putExtra("return-data", true); + startActivityForResult(intent, CUT_PHOTO); + + } + + // 保存裁剪后的图片 + public void savePhoto(Intent intent) { + Bundle extras = intent.getExtras(); + if (extras != null) { + Bitmap photo = extras.getParcelable("data"); + Drawable drawable = new BitmapDrawable(photo); + iv.setBackgroundDrawable(drawable); + } + } + + } diff --git a/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901239/Com1314080901239GetJson.java b/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901239/Com1314080901239GetJson.java new file mode 100644 index 00000000..6f08ec46 --- /dev/null +++ b/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901239/Com1314080901239GetJson.java @@ -0,0 +1,144 @@ +package com.example.test; + +import java.io.ByteArrayOutputStream; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.URL; + +import org.json.JSONObject; + + +import android.os.Bundle; +import android.os.Handler; +import android.os.Message; +import android.app.Activity; +import android.view.Menu; +import android.view.View; +import android.widget.TextView; +import android.widget.Toast; + +public class Com1314080901239GetJson extends Activity { + + private final int SUCCESS = 1; + private final int FAILURE = 0; + private final int ERRORCODE = 2; + private TextView tv; + + private Handler handler=new Handler(){ + public void handleMessage(Message msg){ + switch (msg.what) { + case SUCCESS: + String result=getJson(msg.obj.toString()); + //主线程更新UI + tv.setText(result); + Toast.makeText(Com1314080901239GetJson.this, "获取数据成功", Toast.LENGTH_SHORT) + .show(); + break; + case FAILURE: + Toast.makeText(Com1314080901239GetJson.this, "获取数据失败", Toast.LENGTH_SHORT) + .show(); + case ERRORCODE: + Toast.makeText(Com1314080901239GetJson.this, "获取的CODE码不为200!", + Toast.LENGTH_SHORT).show(); + default: + break; + } + } + }; + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_com1314080901239_get_json); + tv=(TextView) findViewById(R.id.tv); + } + + public String getJson(String string){ + JSONObject jObject= null; + try { + jObject =new JSONObject(string); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + System.out.println("创建JsonObject失败"); + } + //解析JSON数据 + String city=jObject.optString("city"); + String city_en=jObject.optString("city_en"); + String date_y=jObject.optString("date_y"); + String week=jObject.optString("week"); + int cityid=jObject.optInt("cityid"); + String temp1=jObject.optString("temp1"); + System.out.println(temp1+""+date_y+""+city_en+""+cityid+""+week+""+city); + String weatherResult="城市:"+city+"\n城市英文名:"+city_en+"\n日期:"+date_y+"\n星期:"+week+"\n城市编号:"+cityid+"\n温度:"+temp1; + return weatherResult; + } + + public void click(View v){ + //开子线程获取网络请求 + new Thread(){ + int code=0; + public void run() { + // TODO Auto-generated method stub + try { + + String path="https://raw.githubusercontent.com/hzuapps/android-labs/master/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901239/city.json"; + URL url=new URL(path); + HttpURLConnection conn=(HttpURLConnection) url.openConnection(); + //设置GET请求 + conn.setRequestMethod("GET"); + //设置请求超时时间 + conn.setConnectTimeout(5000); + code=conn.getResponseCode(); + System.out.println(code); + //200表示获取请求成功 + if(code==200){ + System.out.println("获取数据成功"); + InputStream is=conn.getInputStream(); + String result = readMyInputStream(is); + + //handle Message + Message msg=new Message(); + msg.obj= result; + msg.what=SUCCESS; + handler.sendMessage(msg); + } else { + Message msg = new Message(); + msg.what = ERRORCODE; + handler.sendMessage(msg); + } + } catch (Exception e) { + // TODO Auto-generated catch block + System.out.println("获取数据失败"); + e.printStackTrace(); + Message msg = new Message(); + msg.what = FAILURE; + handler.sendMessage(msg); + } + + + super.run(); + } + }.start(); + } + + //以流的形式读取数据 + public String readMyInputStream(InputStream is){ + byte[] result; + try { + ByteArrayOutputStream baos=new ByteArrayOutputStream(); + byte[] buffer=new byte[1024]; + int len; + while((len = is.read(buffer))!= -1){ + baos.write(buffer,0,len); + } + is.close(); + baos.close(); + result =baos.toByteArray(); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + String errorString="获取数据失败"; + return errorString; + } + return new String(result); + } +} From 47cd8076bc0cb68b26430a201ee32df5c7d05645 Mon Sep 17 00:00:00 2001 From: YLiuLiu <851942035@qq.com> Date: Thu, 16 Jun 2016 14:51:05 +0800 Subject: [PATCH 2/3] =?UTF-8?q?Revert=20"#93=20#94=20#95=20#76=20=E5=AE=9E?= =?UTF-8?q?=E9=AA=8C=E5=B7=B2=E5=AE=8C=E6=88=90"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 6f31555cf8235b6eb68f6806995db8bebe6e606d. --- .../Com1314080901239Activity.java | 141 ++++------------- .../Com1314080901239GetJson.java | 144 ------------------ 2 files changed, 30 insertions(+), 255 deletions(-) delete mode 100644 app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901239/Com1314080901239GetJson.java diff --git a/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901239/Com1314080901239Activity.java b/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901239/Com1314080901239Activity.java index ca8bee90..2780b839 100644 --- a/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901239/Com1314080901239Activity.java +++ b/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901239/Com1314080901239Activity.java @@ -3,127 +3,46 @@ import java.io.File; import android.app.Activity; -import android.content.ContentResolver; import android.content.Intent; -import android.database.Cursor; -import android.graphics.Bitmap; -import android.graphics.drawable.BitmapDrawable; -import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; import android.view.View; -import android.widget.ImageView; import android.widget.Toast; -public class Com1314080901239Activity extends Activity { - private final String IMAGE_TYPE = "image/*"; - private final int PICK_PHOTO = 1; // 选择照片 - private final int TAKE_PHOTO = 0; // 拍照 - private final int CUT_PHOTO = 2;// 裁剪图片 +public class Com1314080901239Activity extends Activity { private Uri fileUri; - private ImageView iv; - private String path; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_com1314080901239); - iv = (ImageView) findViewById(R.id.iv); - } - - // 拍照 - public void takePhoto(View v) { - - // 启动一个意图。这里就是调用google给我们提供的,手机自带的拍照应用 - Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); - // 设置存储路径 - intent.putExtra(MediaStore.EXTRA_OUTPUT, - // 设置图片存储路径及名字 - Uri.fromFile(new File("sdcard/nuna.jpg"))); - - startActivityForResult(intent, TAKE_PHOTO); - - } - - public void pickPhoto(View v) { - // 调用系统提供的相册功能 - Intent intent = new Intent(Intent.ACTION_GET_CONTENT); - intent.setType(IMAGE_TYPE); - startActivityForResult(intent, PICK_PHOTO); - - } - - public void cutPhoto(View v) { - Cutphoto(); - } - public void getJson(View v){ - Intent intent=new Intent(Com1314080901239Activity.this,Com1314080901239GetJson.class); - startActivity(intent); - } + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_com1314080901239); + } + // 点击事件 + public void takePhoto(View v) { + // create Intent to take a picture and return control to the calling + // application + // 启动一个意图。这里就是调用google给我们提供的,手机自带的拍照应用 + Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); + // 设置存储路径 + intent.putExtra(MediaStore.EXTRA_OUTPUT, + Uri.fromFile(new File("sdcard/nuna.jpg"))); // set the image + // file name + + // start the image capture Intent + startActivityForResult(intent, 0); + + } + + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + // TODO Auto-generated method stub + super.onActivityResult(requestCode, resultCode, data); + if (requestCode == 0) { + Toast.makeText(getApplicationContext(), "拍照成功", 0).show(); + } + } - @Override - protected void onActivityResult(int requestCode, int resultCode, Intent data) { - // TODO Auto-generated method stub - super.onActivityResult(requestCode, resultCode, data); - Bitmap bm = null; - ContentResolver resolver = getContentResolver(); - if (requestCode == TAKE_PHOTO) { - Toast.makeText(getApplicationContext(), "拍照成功", 0).show(); - } else if (requestCode == PICK_PHOTO) { - Uri originalUri = data.getData(); - try { - bm = MediaStore.Images.Media.getBitmap(resolver, originalUri); - iv.setImageBitmap(bm); - String[] proj = { MediaStore.Images.Media.DATA }; - Cursor cursor = managedQuery(originalUri, proj, null, null, - null); - int column_index = cursor - .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); - - cursor.moveToFirst(); - // 最后根据索引值获取图片路径 - path = cursor.getString(column_index); - - - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - } else if (requestCode == CUT_PHOTO) { - if (resultCode == RESULT_OK) { - savePhoto(data); - } - } - } - - // 裁剪图片的方法 - public void Cutphoto() { - Intent intent = new Intent("com.android.camera.action.CROP"); - //intent.setDataAndType(uri, IMAGE_TYPE); - intent.putExtra("crop", "true"); - intent.putExtra("aspectX", 1); - intent.putExtra("aspectY", 1); - intent.putExtra("outoutX", 150); - intent.putExtra("outputY", 150); - intent.putExtra("return-data", true); - startActivityForResult(intent, CUT_PHOTO); - - } - - // 保存裁剪后的图片 - public void savePhoto(Intent intent) { - Bundle extras = intent.getExtras(); - if (extras != null) { - Bitmap photo = extras.getParcelable("data"); - Drawable drawable = new BitmapDrawable(photo); - iv.setBackgroundDrawable(drawable); - } - } - - } diff --git a/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901239/Com1314080901239GetJson.java b/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901239/Com1314080901239GetJson.java deleted file mode 100644 index 6f08ec46..00000000 --- a/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901239/Com1314080901239GetJson.java +++ /dev/null @@ -1,144 +0,0 @@ -package com.example.test; - -import java.io.ByteArrayOutputStream; -import java.io.InputStream; -import java.net.HttpURLConnection; -import java.net.URL; - -import org.json.JSONObject; - - -import android.os.Bundle; -import android.os.Handler; -import android.os.Message; -import android.app.Activity; -import android.view.Menu; -import android.view.View; -import android.widget.TextView; -import android.widget.Toast; - -public class Com1314080901239GetJson extends Activity { - - private final int SUCCESS = 1; - private final int FAILURE = 0; - private final int ERRORCODE = 2; - private TextView tv; - - private Handler handler=new Handler(){ - public void handleMessage(Message msg){ - switch (msg.what) { - case SUCCESS: - String result=getJson(msg.obj.toString()); - //主线程更新UI - tv.setText(result); - Toast.makeText(Com1314080901239GetJson.this, "获取数据成功", Toast.LENGTH_SHORT) - .show(); - break; - case FAILURE: - Toast.makeText(Com1314080901239GetJson.this, "获取数据失败", Toast.LENGTH_SHORT) - .show(); - case ERRORCODE: - Toast.makeText(Com1314080901239GetJson.this, "获取的CODE码不为200!", - Toast.LENGTH_SHORT).show(); - default: - break; - } - } - }; - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_com1314080901239_get_json); - tv=(TextView) findViewById(R.id.tv); - } - - public String getJson(String string){ - JSONObject jObject= null; - try { - jObject =new JSONObject(string); - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - System.out.println("创建JsonObject失败"); - } - //解析JSON数据 - String city=jObject.optString("city"); - String city_en=jObject.optString("city_en"); - String date_y=jObject.optString("date_y"); - String week=jObject.optString("week"); - int cityid=jObject.optInt("cityid"); - String temp1=jObject.optString("temp1"); - System.out.println(temp1+""+date_y+""+city_en+""+cityid+""+week+""+city); - String weatherResult="城市:"+city+"\n城市英文名:"+city_en+"\n日期:"+date_y+"\n星期:"+week+"\n城市编号:"+cityid+"\n温度:"+temp1; - return weatherResult; - } - - public void click(View v){ - //开子线程获取网络请求 - new Thread(){ - int code=0; - public void run() { - // TODO Auto-generated method stub - try { - - String path="https://raw.githubusercontent.com/hzuapps/android-labs/master/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901239/city.json"; - URL url=new URL(path); - HttpURLConnection conn=(HttpURLConnection) url.openConnection(); - //设置GET请求 - conn.setRequestMethod("GET"); - //设置请求超时时间 - conn.setConnectTimeout(5000); - code=conn.getResponseCode(); - System.out.println(code); - //200表示获取请求成功 - if(code==200){ - System.out.println("获取数据成功"); - InputStream is=conn.getInputStream(); - String result = readMyInputStream(is); - - //handle Message - Message msg=new Message(); - msg.obj= result; - msg.what=SUCCESS; - handler.sendMessage(msg); - } else { - Message msg = new Message(); - msg.what = ERRORCODE; - handler.sendMessage(msg); - } - } catch (Exception e) { - // TODO Auto-generated catch block - System.out.println("获取数据失败"); - e.printStackTrace(); - Message msg = new Message(); - msg.what = FAILURE; - handler.sendMessage(msg); - } - - - super.run(); - } - }.start(); - } - - //以流的形式读取数据 - public String readMyInputStream(InputStream is){ - byte[] result; - try { - ByteArrayOutputStream baos=new ByteArrayOutputStream(); - byte[] buffer=new byte[1024]; - int len; - while((len = is.read(buffer))!= -1){ - baos.write(buffer,0,len); - } - is.close(); - baos.close(); - result =baos.toByteArray(); - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - String errorString="获取数据失败"; - return errorString; - } - return new String(result); - } -} From 55d68078b93b75b7817af3dc8d6601f56c6f547e Mon Sep 17 00:00:00 2001 From: YLiuLiu <851942035@qq.com> Date: Thu, 16 Jun 2016 15:00:49 +0800 Subject: [PATCH 3/3] =?UTF-8?q?#93=20#94=20#95=20#76=20=E5=AE=9E=E9=AA=8C?= =?UTF-8?q?=E4=B8=83=E3=80=81=E5=85=AB=E3=80=81=E4=B9=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Com1314080901239Activity.java | 141 +++++++++++++---- .../Com1314080901239GetJson.java | 144 ++++++++++++++++++ .../res/layout/activity_com1314080901239.xml | 21 ++- .../activity_com1314080901239_get_json.xml | 25 +++ 4 files changed, 296 insertions(+), 35 deletions(-) create mode 100644 app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901239/Com1314080901239GetJson.java create mode 100644 app/src/main/res/layout/activity_com1314080901239_get_json.xml diff --git a/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901239/Com1314080901239Activity.java b/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901239/Com1314080901239Activity.java index 2780b839..ca8bee90 100644 --- a/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901239/Com1314080901239Activity.java +++ b/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901239/Com1314080901239Activity.java @@ -3,46 +3,127 @@ import java.io.File; import android.app.Activity; +import android.content.ContentResolver; import android.content.Intent; +import android.database.Cursor; +import android.graphics.Bitmap; +import android.graphics.drawable.BitmapDrawable; +import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; import android.view.View; +import android.widget.ImageView; import android.widget.Toast; - public class Com1314080901239Activity extends Activity { + + private final String IMAGE_TYPE = "image/*"; + private final int PICK_PHOTO = 1; // 选择照片 + private final int TAKE_PHOTO = 0; // 拍照 + private final int CUT_PHOTO = 2;// 裁剪图片 private Uri fileUri; + private ImageView iv; + private String path; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_com1314080901239); + iv = (ImageView) findViewById(R.id.iv); + } + + // 拍照 + public void takePhoto(View v) { + + // 启动一个意图。这里就是调用google给我们提供的,手机自带的拍照应用 + Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); + // 设置存储路径 + intent.putExtra(MediaStore.EXTRA_OUTPUT, + // 设置图片存储路径及名字 + Uri.fromFile(new File("sdcard/nuna.jpg"))); + + startActivityForResult(intent, TAKE_PHOTO); + + } + + public void pickPhoto(View v) { + // 调用系统提供的相册功能 + Intent intent = new Intent(Intent.ACTION_GET_CONTENT); + intent.setType(IMAGE_TYPE); + startActivityForResult(intent, PICK_PHOTO); + + } + + public void cutPhoto(View v) { + Cutphoto(); + } - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_com1314080901239); - } - // 点击事件 - public void takePhoto(View v) { - // create Intent to take a picture and return control to the calling - // application - // 启动一个意图。这里就是调用google给我们提供的,手机自带的拍照应用 - Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); - // 设置存储路径 - intent.putExtra(MediaStore.EXTRA_OUTPUT, - Uri.fromFile(new File("sdcard/nuna.jpg"))); // set the image - // file name - - // start the image capture Intent - startActivityForResult(intent, 0); - - } - - @Override - protected void onActivityResult(int requestCode, int resultCode, Intent data) { - // TODO Auto-generated method stub - super.onActivityResult(requestCode, resultCode, data); - if (requestCode == 0) { - Toast.makeText(getApplicationContext(), "拍照成功", 0).show(); - } - } + public void getJson(View v){ + Intent intent=new Intent(Com1314080901239Activity.this,Com1314080901239GetJson.class); + startActivity(intent); + } + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + // TODO Auto-generated method stub + super.onActivityResult(requestCode, resultCode, data); + Bitmap bm = null; + ContentResolver resolver = getContentResolver(); + if (requestCode == TAKE_PHOTO) { + Toast.makeText(getApplicationContext(), "拍照成功", 0).show(); + } else if (requestCode == PICK_PHOTO) { + Uri originalUri = data.getData(); + try { + bm = MediaStore.Images.Media.getBitmap(resolver, originalUri); + iv.setImageBitmap(bm); + String[] proj = { MediaStore.Images.Media.DATA }; + Cursor cursor = managedQuery(originalUri, proj, null, null, + null); + int column_index = cursor + .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); + + cursor.moveToFirst(); + // 最后根据索引值获取图片路径 + path = cursor.getString(column_index); + + + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } else if (requestCode == CUT_PHOTO) { + if (resultCode == RESULT_OK) { + savePhoto(data); + } + } + } + + // 裁剪图片的方法 + public void Cutphoto() { + Intent intent = new Intent("com.android.camera.action.CROP"); + //intent.setDataAndType(uri, IMAGE_TYPE); + intent.putExtra("crop", "true"); + intent.putExtra("aspectX", 1); + intent.putExtra("aspectY", 1); + intent.putExtra("outoutX", 150); + intent.putExtra("outputY", 150); + intent.putExtra("return-data", true); + startActivityForResult(intent, CUT_PHOTO); + + } + + // 保存裁剪后的图片 + public void savePhoto(Intent intent) { + Bundle extras = intent.getExtras(); + if (extras != null) { + Bitmap photo = extras.getParcelable("data"); + Drawable drawable = new BitmapDrawable(photo); + iv.setBackgroundDrawable(drawable); + } + } + + } diff --git a/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901239/Com1314080901239GetJson.java b/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901239/Com1314080901239GetJson.java new file mode 100644 index 00000000..6f08ec46 --- /dev/null +++ b/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901239/Com1314080901239GetJson.java @@ -0,0 +1,144 @@ +package com.example.test; + +import java.io.ByteArrayOutputStream; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.URL; + +import org.json.JSONObject; + + +import android.os.Bundle; +import android.os.Handler; +import android.os.Message; +import android.app.Activity; +import android.view.Menu; +import android.view.View; +import android.widget.TextView; +import android.widget.Toast; + +public class Com1314080901239GetJson extends Activity { + + private final int SUCCESS = 1; + private final int FAILURE = 0; + private final int ERRORCODE = 2; + private TextView tv; + + private Handler handler=new Handler(){ + public void handleMessage(Message msg){ + switch (msg.what) { + case SUCCESS: + String result=getJson(msg.obj.toString()); + //主线程更新UI + tv.setText(result); + Toast.makeText(Com1314080901239GetJson.this, "获取数据成功", Toast.LENGTH_SHORT) + .show(); + break; + case FAILURE: + Toast.makeText(Com1314080901239GetJson.this, "获取数据失败", Toast.LENGTH_SHORT) + .show(); + case ERRORCODE: + Toast.makeText(Com1314080901239GetJson.this, "获取的CODE码不为200!", + Toast.LENGTH_SHORT).show(); + default: + break; + } + } + }; + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_com1314080901239_get_json); + tv=(TextView) findViewById(R.id.tv); + } + + public String getJson(String string){ + JSONObject jObject= null; + try { + jObject =new JSONObject(string); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + System.out.println("创建JsonObject失败"); + } + //解析JSON数据 + String city=jObject.optString("city"); + String city_en=jObject.optString("city_en"); + String date_y=jObject.optString("date_y"); + String week=jObject.optString("week"); + int cityid=jObject.optInt("cityid"); + String temp1=jObject.optString("temp1"); + System.out.println(temp1+""+date_y+""+city_en+""+cityid+""+week+""+city); + String weatherResult="城市:"+city+"\n城市英文名:"+city_en+"\n日期:"+date_y+"\n星期:"+week+"\n城市编号:"+cityid+"\n温度:"+temp1; + return weatherResult; + } + + public void click(View v){ + //开子线程获取网络请求 + new Thread(){ + int code=0; + public void run() { + // TODO Auto-generated method stub + try { + + String path="https://raw.githubusercontent.com/hzuapps/android-labs/master/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901239/city.json"; + URL url=new URL(path); + HttpURLConnection conn=(HttpURLConnection) url.openConnection(); + //设置GET请求 + conn.setRequestMethod("GET"); + //设置请求超时时间 + conn.setConnectTimeout(5000); + code=conn.getResponseCode(); + System.out.println(code); + //200表示获取请求成功 + if(code==200){ + System.out.println("获取数据成功"); + InputStream is=conn.getInputStream(); + String result = readMyInputStream(is); + + //handle Message + Message msg=new Message(); + msg.obj= result; + msg.what=SUCCESS; + handler.sendMessage(msg); + } else { + Message msg = new Message(); + msg.what = ERRORCODE; + handler.sendMessage(msg); + } + } catch (Exception e) { + // TODO Auto-generated catch block + System.out.println("获取数据失败"); + e.printStackTrace(); + Message msg = new Message(); + msg.what = FAILURE; + handler.sendMessage(msg); + } + + + super.run(); + } + }.start(); + } + + //以流的形式读取数据 + public String readMyInputStream(InputStream is){ + byte[] result; + try { + ByteArrayOutputStream baos=new ByteArrayOutputStream(); + byte[] buffer=new byte[1024]; + int len; + while((len = is.read(buffer))!= -1){ + baos.write(buffer,0,len); + } + is.close(); + baos.close(); + result =baos.toByteArray(); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + String errorString="获取数据失败"; + return errorString; + } + return new String(result); + } +} diff --git a/app/src/main/res/layout/activity_com1314080901239.xml b/app/src/main/res/layout/activity_com1314080901239.xml index 7bf778e8..d515ccf5 100644 --- a/app/src/main/res/layout/activity_com1314080901239.xml +++ b/app/src/main/res/layout/activity_com1314080901239.xml @@ -7,14 +7,17 @@ > @@ -24,26 +27,34 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="閫夊浘" - + android:onClick="pickPhoto" />