parent
a2271e5d9b
commit
cbd818e378
@ -0,0 +1,158 @@
|
||||
package com.example.health;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.core.app.ActivityCompat;
|
||||
import androidx.core.content.ContextCompat;
|
||||
import androidx.core.content.FileProvider;
|
||||
import android.Manifest;
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Message;
|
||||
import android.provider.MediaStore;
|
||||
import android.text.TextUtils;
|
||||
import android.view.View;
|
||||
import android.widget.EditText;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.Toast;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
public class network extends AppCompatActivity {
|
||||
protected static final int CHANGE_UI = 1;
|
||||
protected static final int ERROR = 2;
|
||||
private static final int REQUEST_CODE_TAKE = 1;
|
||||
private Uri imageUri;
|
||||
private ImageView ivPic;
|
||||
|
||||
@SuppressLint("HandlerLeak")
|
||||
private Handler handler = new Handler(){
|
||||
public void handleMessage(android.os.Message msg){
|
||||
if (msg.what == CHANGE_UI){
|
||||
Bitmap bitmap = (Bitmap) msg.obj;
|
||||
ivPic.setImageBitmap(bitmap);
|
||||
}
|
||||
else if (msg.what == ERROR){
|
||||
Toast.makeText(network.this,"显示图片错误",Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_network);
|
||||
ivPic = (ImageView) findViewById(R.id.iv_pic);
|
||||
}
|
||||
|
||||
public void click(View view){
|
||||
new Thread(){
|
||||
private HttpURLConnection conn;
|
||||
private Bitmap bitmap;
|
||||
public void run(){
|
||||
try{
|
||||
URL url = new URL("https://picb2.photophoto.cn/40/293/40293462_1.jpg");
|
||||
conn = (HttpURLConnection) url.openConnection();
|
||||
conn.setRequestMethod("GET");
|
||||
conn.setConnectTimeout(5000);
|
||||
int code = conn.getResponseCode();
|
||||
if(code == 200){
|
||||
InputStream is = conn.getInputStream();
|
||||
bitmap = BitmapFactory.decodeStream(is);
|
||||
Message msg = new Message();
|
||||
msg.what = CHANGE_UI;
|
||||
msg.obj = bitmap;
|
||||
handler.sendMessage(msg);
|
||||
}
|
||||
else{
|
||||
Message msg = new Message();
|
||||
msg.what = ERROR;
|
||||
handler.sendMessage(msg);
|
||||
}
|
||||
|
||||
}
|
||||
catch(Exception e){
|
||||
e.printStackTrace();
|
||||
Message msg = new Message();
|
||||
msg.what = ERROR;
|
||||
handler.sendMessage(msg);
|
||||
}
|
||||
conn.disconnect();
|
||||
}
|
||||
}.start();
|
||||
}
|
||||
|
||||
public void takephoto(View view) {
|
||||
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
|
||||
// 真正的执行去拍照
|
||||
doTake();
|
||||
} else {
|
||||
// 去申请权限
|
||||
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 1);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
||||
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
||||
if (requestCode == 1) {
|
||||
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||
doTake();
|
||||
} else {
|
||||
Toast.makeText(this, "你没有获得摄像头权限~", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void doTake() {
|
||||
File imageTemp = new File(getExternalCacheDir(), "imageOut.jpeg");
|
||||
if (imageTemp.exists()) {
|
||||
imageTemp.delete();
|
||||
}
|
||||
try {
|
||||
imageTemp.createNewFile();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
if (Build.VERSION.SDK_INT > 24) {
|
||||
// contentProvider
|
||||
imageUri = FileProvider.getUriForFile(this, "com.example.health.fileprovider", imageTemp);
|
||||
} else {
|
||||
imageUri = Uri.fromFile(imageTemp);
|
||||
}
|
||||
Intent intent = new Intent();
|
||||
intent.setAction("android.media.action.IMAGE_CAPTURE");
|
||||
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
|
||||
startActivityForResult(intent, REQUEST_CODE_TAKE);
|
||||
|
||||
}
|
||||
@Override
|
||||
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
if (requestCode == REQUEST_CODE_TAKE) {
|
||||
if (resultCode == RESULT_OK) {
|
||||
// 获取拍摄的照片
|
||||
try {
|
||||
InputStream inputStream = getContentResolver().openInputStream(imageUri);
|
||||
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
|
||||
ivPic.setImageBitmap(bitmap);
|
||||
} catch (FileNotFoundException e) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue