Skip to content

Commit

Permalink
Merge pull request #1569 from ZedCxs/master
Browse files Browse the repository at this point in the history
#7 #269 提交实验七代码
  • Loading branch information
zengsn authored Apr 22, 2019
2 parents 889480d + f6acb18 commit ae6e055
Show file tree
Hide file tree
Showing 13 changed files with 459 additions and 28 deletions.
21 changes: 18 additions & 3 deletions students/com1714080901123/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/><!-- 兼容老版本:声明访问SD卡权限 -->

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand All @@ -17,7 +19,8 @@
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:usesCleartextTraffic="true">
<activity android:name=".Com1714080901123ActivitySelectPlayersConfig"></activity>
<activity android:name=".Com1714080901123ActivityAmiiboSharePhoto"></activity>
<activity android:name=".Com1714080901123ActivitySelectPlayersConfig" />
<activity
android:name=".Com1714080901123ActivitySelectAbility"
android:label="@string/title_activity_com1714080901123_select_ability"
Expand All @@ -39,8 +42,20 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<uses-library android:name="org.apache.http.legacy" android:required="false" />
<!-- 上一行代码:java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/conn/scheme/SchemeRegistry; -->

<uses-library
android:name="org.apache.http.legacy"
android:required="false" />

<provider
android:authorities="com.example.cameraalbumtest.fileprovider"
android:name="android.support.v4.content.FileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/><!-- 用来指定Uri的共享路径 -->
</provider>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class Com1714080901123ActivityAmiibo extends AppCompatActivity {

Com1714080901123ClassDrawableID drawableID = new Com1714080901123ClassDrawableID();
int i = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -39,39 +43,44 @@ public void onClick(View view) {
TextView textView_Clothing_ability = (TextView)findViewById(R.id.text_amiibo_clothing_ability);
TextView textView_Shoes_ability = (TextView)findViewById(R.id.text_amiibo_shoes_ability);

Button button = (Button)findViewById(R.id.button_amiibo_share_photo);

Intent intent = this.getIntent();
String msg = intent.getStringExtra("imgButton"); //获取前一个Activity传过来的值,以决定应该显示的图片和文字
if(msg.equals("imgButton01")) {
textView.setText(R.string.name_inkling_girl);
imageView.setImageResource(R.drawable.amiibo_inkling_girl);
imageView_Headgear.setImageResource(R.drawable.gear_headgear_squid_hairclip);
textView_Headgear.setText(R.string.gear_headgear_squid_hairclip);
imageView_Clothing.setImageResource(R.drawable.gear_clothing_school_uniform);
textView_Clothing.setText(R.string.gear_clothing_school_uniform);
imageView_Shoes.setImageResource(R.drawable.gear_shoes_school_shoes);
textView_Shoes.setText(R.string.gear_shoes_school_shoes);
i = intent.getIntExtra("imgButton", 0); //获取前一个Activity传过来的值,以决定应该显示的图片和文字

textView.setText(drawableID.getAmiiboName(i));
imageView.setImageResource(drawableID.getAmiibo(i));
imageView_Headgear.setImageResource(drawableID.getHeadGear(i));
textView_Headgear.setText(drawableID.getHeadGearName(i));
imageView_Clothing.setImageResource(drawableID.getClothing(i));
textView_Clothing.setText(drawableID.getClothingName(i));
imageView_Shoes.setImageResource(drawableID.getShoes(i));
textView_Shoes.setText(drawableID.getShoesName(i));

if(i==0) {
imageView_Headgear_ability.setImageResource(R.drawable.ability_swim_speed_up);
imageView_Clothing_ability.setImageResource(R.drawable.ability_ink_recovery_up);
imageView_Shoes_ability.setImageResource(R.drawable.ability_ink_saver_sub);
textView_Headgear_ability.setText(R.string.ability_swim_speed_up);
textView_Clothing_ability.setText(R.string.ability_ink_recovery_up);
textView_Shoes_ability.setText(R.string.ability_ink_saver_sub);
}
else if(msg.equals("imgButton02")) {
textView.setText(R.string.name_inkling_boy);
imageView.setImageResource(R.drawable.amiibo_inkling_boy);
imageView_Headgear.setImageResource(R.drawable.gear_headgear_samurai_helmet);
textView_Headgear.setText(R.string.gear_headgear_samurai_helmet);
imageView_Clothing.setImageResource(R.drawable.gear_clothing_samurai_jacket);
textView_Clothing.setText(R.string.gear_clothing_samurai_jacket);
imageView_Shoes.setImageResource(R.drawable.gear_shoes_samurai_shoes);
textView_Shoes.setText(R.string.gear_shoes_samurai_shoes);
else if(i==1) {
imageView_Headgear_ability.setImageResource(R.drawable.ability_special_charge_up);
imageView_Clothing_ability.setImageResource(R.drawable.ability_quick_super_jump);
imageView_Shoes_ability.setImageResource(R.drawable.ability_special_power_up);
textView_Headgear_ability.setText(R.string.ability_special_charge_up);
textView_Clothing_ability.setText(R.string.ability_quick_super_jump);
textView_Shoes_ability.setText(R.string.ability_special_power_up);
}

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Com1714080901123ActivityAmiibo.this, Com1714080901123ActivityAmiiboSharePhoto.class);
intent.putExtra("amiibo",i);
startActivity(intent);
}
});
}
}
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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,38 @@ public class Com1714080901123ClassDrawableID {
R.string.ability_swim_speed_up,
R.string.ability_quick_super_jump
};
protected static int[] amiibo = new int[]{
R.drawable.amiibo_inkling_girl,
R.drawable.amiibo_inkling_boy
};
protected static int[] amiiboName = new int[]{
R.string.name_inkling_girl,
R.string.name_inkling_boy
};
protected static int[] headGear = new int[]{
R.drawable.gear_headgear_squid_hairclip,
R.drawable.gear_headgear_samurai_helmet
};
protected static int[] headGearName = new int[]{
R.string.gear_headgear_squid_hairclip,
R.string.gear_headgear_samurai_helmet
};
protected static int[] clothing = new int[]{
R.drawable.gear_clothing_school_uniform,
R.drawable.gear_clothing_samurai_jacket
};
protected static int[] clothingName = new int[]{
R.string.gear_clothing_school_uniform,
R.string.gear_clothing_samurai_jacket
};
protected static int[] shoes = new int[]{
R.drawable.gear_shoes_school_shoes,
R.drawable.gear_shoes_samurai_shoes
};
protected static int[] shoesName = new int[]{
R.string.gear_shoes_school_shoes,
R.string.gear_shoes_samurai_shoes
};

public int getWeapon(int i) {
return weapon[i];
Expand All @@ -63,4 +95,28 @@ public int getAbility(int i) {
public int getAbilityText(int i) {
return abilityText[i];
}
public int getAmiibo(int i) {
return amiibo[i];
}
public int getAmiiboName(int i) {
return amiiboName[i];
}
public int getHeadGear(int i) {
return headGear[i];
}
public int getHeadGearName(int i) {
return headGearName[i];
}
public int getClothing(int i) {
return clothing[i];
}
public int getClothingName(int i) {
return clothingName[i];
}
public int getShoes(int i) {
return shoes[i];
}
public int getShoesName(int i) {
return shoesName[i];
}
}
Loading

0 comments on commit ae6e055

Please sign in to comment.