Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
YLiuLiu committed Jun 15, 2016
1 parent d4cea90 commit 6f31555
Show file tree
Hide file tree
Showing 2 changed files with 255 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}



}
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 6f31555

Please sign in to comment.