-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1569 from ZedCxs/master
- Loading branch information
Showing
13 changed files
with
459 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
208 changes: 208 additions & 0 deletions
208
...va/edu/hzuapps/androidlabs/com1714080901123/Com1714080901123ActivityAmiiboSharePhoto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,208 @@ | ||
package edu.hzuapps.androidlabs.com1714080901123; | ||
|
||
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.support.v7.widget.Toolbar; | ||
import android.util.Log; | ||
import android.view.View; | ||
import android.widget.Button; | ||
import android.widget.ImageView; | ||
import android.widget.TextView; | ||
import android.widget.Toast; | ||
|
||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
|
||
public class Com1714080901123ActivityAmiiboSharePhoto extends AppCompatActivity { | ||
|
||
Com1714080901123ClassDrawableID drawableID = new Com1714080901123ClassDrawableID(); | ||
int i = 0; | ||
|
||
public static final int TAKE_PHOTO = 1; | ||
public static final int CHOOSE_PHOTO =2; | ||
protected ImageView imageViewPhoto; | ||
protected Uri imageUri; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_com1714080901123_amiibo_share_photo); | ||
getSupportActionBar().hide(); | ||
|
||
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar_amiibo_share_photo); //为toolbar设置返回键有效 | ||
toolbar.setNavigationOnClickListener(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View view) { | ||
finish(); | ||
} | ||
}); | ||
|
||
//设置toolbar标题 | ||
Intent intent = this.getIntent(); | ||
i = intent.getIntExtra("imgButton", 0); | ||
TextView textView = (TextView)findViewById(R.id.textView_amiibo_share_bar); | ||
textView.setText(drawableID.getAmiiboName(i)); | ||
|
||
Button buttonTakePhoto = (Button)findViewById(R.id.button_amiibo_share_photo_camera); | ||
Button buttonAlbum = (Button)findViewById(R.id.button_amiibo_share_photo_album); | ||
imageViewPhoto = (ImageView)findViewById(R.id.imageView_amiibo_share_photo); | ||
|
||
buttonTakePhoto.setOnClickListener(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View view) { | ||
//创建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) { | ||
//如果Android版本高于7.0 //从7.0开始,直接使用本地真实的uri被认为是不安全的 | ||
imageUri = FileProvider.getUriForFile(Com1714080901123ActivityAmiiboSharePhoto.this, | ||
"com.example.cameraalbumtest.fileprovider", outputImage); | ||
} else { | ||
imageUri = Uri.fromFile(outputImage); | ||
} | ||
//启动相机程序 | ||
Intent intent1 = new Intent("android.media.action.IMAGE_CAPTURE"); | ||
intent1.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); | ||
startActivityForResult(intent1, TAKE_PHOTO); | ||
} | ||
}); | ||
|
||
buttonAlbum.setOnClickListener(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View view) { | ||
//如果没有读写SD卡的权限,则询问是否授予权限 | ||
if (ContextCompat.checkSelfPermission(Com1714080901123ActivityAmiiboSharePhoto.this, | ||
Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { | ||
ActivityCompat.requestPermissions(Com1714080901123ActivityAmiiboSharePhoto.this, | ||
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1); | ||
} else { | ||
openAlbum(); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
@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)); | ||
imageViewPhoto.setImageBitmap(bitmap); | ||
//Log.d("TakePhoto","success"); //test | ||
} catch (FileNotFoundException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
break; | ||
case CHOOSE_PHOTO: //访问相册的响应 | ||
if (resultCode == RESULT_OK) { | ||
//判断手机系统版本号 | ||
if (Build.VERSION.SDK_INT >= 19) { | ||
//4.4及以上 //从4.4开始,选取相册中的图片不再返回图片真实的uri了,而是一个封装过的uri,因此需要特别解析它 | ||
handleImageOnKitKat(data); | ||
} else { | ||
handleImageBeforeKitKat(data); | ||
} | ||
} | ||
} | ||
} | ||
|
||
//打开相册 | ||
protected void openAlbum() { | ||
Intent intent = new Intent("android.intent.action.GET_CONTENT"); | ||
intent.setType("image/*"); | ||
startActivityForResult(intent, CHOOSE_PHOTO); | ||
} | ||
|
||
//响应ActivityCompat.requestPermissions | ||
@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, "你没有权限访问相册", Toast.LENGTH_SHORT).show(); | ||
} | ||
break; | ||
} | ||
} | ||
|
||
@TargetApi(19) | ||
private void handleImageOnKitKat(Intent data) { | ||
String imagePath = null; | ||
Uri uri = data. getData(); | ||
if (DocumentsContract.isDocumentUri(this, uri)) { | ||
//如果是document类型的Uri,则通过document id处理 | ||
String docId = DocumentsContract.getDocumentId(uri); | ||
if ("com.android.providers.media.documents".equals(uri.getAuthority())) { | ||
//如果uri的authority是media格式,document id还需要在进行一次解析,要通过字符串分割的方式取出后半部分才能得到真正的数字id | ||
String id = docId.split(":")[1]; //解析出 media格式的id | ||
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())) { | ||
//如果是 content 类型的 uri,则使用普通方式处理 | ||
imagePath = getImagePath(uri, null); | ||
} else if ("file".equalsIgnoreCase(uri.getScheme())) { | ||
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.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); | ||
imageViewPhoto.setImageBitmap(bitmap); | ||
} else { | ||
Toast.makeText(this, "获取图片失败", Toast.LENGTH_SHORT).show(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.