Skip to content

Commit

Permalink
hzuapps#7 hzuapps#464 第7次实验
Browse files Browse the repository at this point in the history
  • Loading branch information
ashuo2010 committed May 4, 2019
1 parent 1ff0a3b commit c981492
Show file tree
Hide file tree
Showing 5 changed files with 197 additions and 116 deletions.
24 changes: 0 additions & 24 deletions students/com1714080901133/app/src/main/AndroidManifest.xml

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,79 +1,192 @@
package edu.hzuapps.androidlabs.com1714080901133;

import android.annotation.TargetApi;
import android.content.ContentUris;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.os.StrictMode;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class Com1714080901133Activity extends AppCompatActivity {

private String imageUrl = "http://img.mp.itc.cn/upload/20170628/b0dabda375494e078340073efc4156c4_th.jpg";
private ImageView img;
private Handler handle = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 0:
Bitmap bmp=(Bitmap)msg.obj;
img.setImageBitmap(bmp);
break;
}
};
};
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

public class Com1714080901133Activity extends AppCompatActivity {

public static final int TAKE_PHOTO = 1;
public static final int CROP_PHOTO = 2;
public static final int CHOOSE_PHOTO = 3;
private Button takephoto;
private Button choosephoto;
private ImageView picture;
private Uri imageUri;

@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
builder.detectFileUriExposure();
setContentView(R.layout.com_1714080901133_activity);
takephoto = (Button) findViewById(R.id.take_photo);
picture = (ImageView) findViewById(R.id.picture);
choosephoto = (Button) findViewById(R.id.choose_photo);

takephoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

File outputImage = new File(Environment.getExternalStorageDirectory(),
"output_image.jpg");

ImageView jmp = findViewById(R.id.hzu);
{
try {
if (outputImage.exists()) {
outputImage.delete();
}
outputImage.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}

imageUri = Uri.fromFile(outputImage);

Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);

startActivityForResult(intent, TAKE_PHOTO);
}
});

img = (ImageView) findViewById(R.id.img);
img.setOnClickListener(new View.OnClickListener() {
choosephoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {

@Override
public void run() {
Bitmap bmp = getURLimage(imageUrl);
Message msg = new Message();
msg.what = 0;
msg.obj = bmp;
handle.sendMessage(msg);
}
}).start();
Intent intent = new Intent("android.intent.action.GET_CONTENT");
intent.setType("image/*");

startActivityForResult(intent, CHOOSE_PHOTO);
}
});
}}

private Bitmap getURLimage(String imageUrl) {
Bitmap bmp = null;
try {
URL myurl = new URL(imageUrl);
HttpURLConnection conn = (HttpURLConnection) myurl.openConnection();
conn.setConnectTimeout(6000);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.connect();
InputStream is = conn.getInputStream();
bmp = BitmapFactory.decodeStream(is);
is.close();
} catch (Exception e) {
e.printStackTrace();
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {

case TAKE_PHOTO:
if (resultCode == RESULT_OK) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(imageUri, "image/*");
intent.putExtra("scale", true);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);

startActivityForResult(intent, CROP_PHOTO);
}
break;

case CROP_PHOTO:
if (resultCode == RESULT_OK) {
try {
Bitmap bitmap = BitmapFactory.decodeStream(
getContentResolver().openInputStream(imageUri));
picture.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
break;

case CHOOSE_PHOTO:
if (resultCode == RESULT_OK) {
if (Build.VERSION.SDK_INT >= 19) {

handleImageOnKitKat(data);
} else {

handleImageBeforeKitKat(data);
}
}
break;
default:
break;
}
return bmp;
}


@TargetApi(19)
private void handleImageOnKitKat(Intent data) {
String imagePath = null;

Uri uri = data.getData();

if (DocumentsContract.isDocumentUri(this, uri)) {
String docId = DocumentsContract.getDocumentId(uri);

if ("com.android.providers.media.documents".equals(
uri.getAuthority())) {
String id = docId.split(":")[1];
String selection = MediaStore.Images.Media._ID + "=" + id;

imagePath = getImagePath(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection);
} else if ("com.android.providers.downloads.documents".equals(
uri.getAuthority())) {

Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"),
Long.valueOf(docId));
imagePath = getImagePath(contentUri, null);
}
} else if ("content".equalsIgnoreCase(uri.getScheme())) {

imagePath = getImagePath(uri, null);
}

displayImage(imagePath);
}


}
private void handleImageBeforeKitKat(Intent data) {
Uri uri = data.getData();
String imagePath = getImagePath(uri, null);
displayImage(imagePath);
}


private String getImagePath(Uri uri, String selection) {
String path = null;
Cursor cursor = getContentResolver().query(uri, null, selection, null, null);
if (cursor != null) {

if (cursor.moveToFirst()) {
path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
}

cursor.close();
}
return path;
}


private void displayImage(String imagePath) {
if (imagePath != null) {

Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
picture.setImageBitmap(bitmap);
} else {
Toast.makeText(this, "failed to get image", Toast.LENGTH_SHORT).show();
}
}
}
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,43 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="edu.hzuapps.androidlabs.com1714080901133.Com1714080901133Activity">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="运动场地浏览,点击图片跳转下一张"
android:textSize="30dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="@+id/hzu" />

<ImageView
android:id="@+id/hzu"
android:layout_width="match_parent"
android:layout_height="384dp"
android:contentDescription="ture"
android:drawablePadding="1dp"
android:src="@drawable/hzxy"
android:layout_marginBottom="48dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<ImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_below="@+id/textView" />

</RelativeLayout>




tools:context=".Com1714080901133Activity">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="368dp"
android:layout_height="551dp"
android:orientation="vertical"
tools:layout_editor_absoluteY="8dp"
tools:layout_editor_absoluteX="8dp">

<Button
android:id="@+id/take_photo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="拍照" />

<Button
android:id="@+id/choose_photo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="选择照片" />

<ImageView
android:id="@+id/picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
</LinearLayout>

</android.support.constraint.ConstraintLayout>

0 comments on commit c981492

Please sign in to comment.