Skip to content

Commit

Permalink
hzuapps#7 hzuapps#254 第七次实验
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed May 24, 2019
1 parent 565317b commit c5f8fd9
Show file tree
Hide file tree
Showing 4 changed files with 236 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package edu.hzuapps.androidlabs.soft1714080902234;

import android.content.DialogInterface;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -22,8 +21,10 @@ protected void onCreate(Bundle savedInstanceState) {
button3.setOnClickListener(this);
Button button4 = findViewById(R.id.button4);
button4.setOnClickListener(this);

Button button5 = findViewById(R.id.button5);
button5.setOnClickListener(this);
}

@Override
public void onClick(View v) {
switch (v.getId()) {
Expand All @@ -43,8 +44,19 @@ public void onClick(View v) {
Intent intent4 = new Intent(v.getContext(), Download.class);
startActivity(intent4);
break;
case R.id.button5:
Intent intent5 = new Intent(v.getContext(), Takephoto.class);
startActivity(intent5);
break;
default:
break;
}
}




}



Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
package edu.hzuapps.androidlabs.soft1714080902234;

import android.Manifest;
import android.annotation.TargetApi;
import android.content.ContentUris;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

public class Takephoto extends AppCompatActivity {

private static final String TAG = "Takephoto";

private static final int TAKE_PHOTO = 1;

private static final int CHOOSE_PHOTO = 2;

private ImageView picture;

private Uri imageUri;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_takephoto);
final Button takePhoto = (Button) findViewById(R.id.take_photo);
Button chooseFromAlbum = (Button) findViewById(R.id.chosse_from_album);
picture = (ImageView) findViewById(R.id.picture);
takePhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//创建 File 对象,用于存储拍照后的图片
File outputImage = new File(getExternalCacheDir(), "output_image.jpg");
try {
if (outputImage.exists()) {
outputImage.delete();
}
outputImage.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
if (Build.VERSION.SDK_INT >= 24) {
imageUri = FileProvider.getUriForFile(Takephoto.this, "edu.hzuapps.androidlabs.soft1714080902234.fileprovider", outputImage);
} else {
imageUri = Uri.fromFile(outputImage);
}
//启动相机程序

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

startActivityForResult(intent, TAKE_PHOTO);
}
});
chooseFromAlbum.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ContextCompat.checkSelfPermission(Takephoto.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(Takephoto.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
} else {
openAlbum();
}
}
});
}

private void openAlbum() {
Intent intent = new Intent("android.intent.action.GET_CONTENT");
intent.setType("image/*");
startActivityForResult(intent, CHOOSE_PHOTO); //打开相册
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case 1:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
openAlbum();
} else {
Toast.makeText(this, "You denied the peimission", Toast.LENGTH_SHORT).show();
}
break;
default:
}
}

@Override
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case TAKE_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) {
//4.4 及以上系统石永红这个方法处理图片
handleImageOnKitKat(data);
} else {
//4.4 以下系统使用这个该方法处理图片
handleImageBeforeKitKat(data);
}
}
default:
break;
}
}

@TargetApi(19)
private void handleImageOnKitKat(Intent data) {
String imagePath = null;
Uri uri = data.getData();
if (DocumentsContract.isDocumentUri(this, uri)) {
String docId = DocumentsContract.getDocumentId(uri);
//如果是 document 类型的 Uri,那就取出 document id 进行处理
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 ("file".equalsIgnoreCase(uri.getScheme())) {
//如果是 content 类型的 Uri ,则使用普通方式处理
imagePath = getImagePath(uri, null);
} else if ("file".equalsIgnoreCase(uri.getScheme())) {
//如果是 file 类型的 Uri ,直接获取图片路径即可
imagePath = uri.getPath();
}
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;
//通过 Uri 和 selection 来获取真实的图片路径
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();
}
}

}
5 changes: 5 additions & 0 deletions students/soft1714080902234/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@
android:layout_height="wrap_content"
android:text="下载" />

<Button
android:id="@+id/button5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="相机" />
</LinearLayout>
25 changes: 25 additions & 0 deletions students/soft1714080902234/main/res/layout/activity_takephoto.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

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

<Button
android:id="@+id/chosse_from_album"
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_vertical" />

</LinearLayout>

0 comments on commit c5f8fd9

Please sign in to comment.