安卓10.0 拍照更换头像失败解决办法:
build.gradle里的: targetSdkVersion 28(降到28)
implementation 'com.zxy.android:tiny:0.1.0'
import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.FileProvider;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.*.*.R;
import com.*.*.activity.FirstActivity;
import com.*.*.app.AppUtils;
import com.*.*.app.BaseActivity;
import com.jcd.sideslipback.SideslipBackHelper;
import com.zxy.tiny.Tiny;
import com.zxy.tiny.callback.FileWithBitmapCallback;
import java.io.File;
import java.io.IOException;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.jpush.android.api.JPushInterface;
public class AboutActivity extends BaseActivity {
@BindView(R.id.back_aboutshiwen)
RelativeLayout backAboutshiwen;
@BindView(R.id.versionName_aboutshiwen)
TextView versionName;
@BindView(R.id.Appname_aboutshiwen)
TextView AppName;
@BindView(R.id.toscore_aboutshiwen)
LinearLayout toscore;
@BindView(R.id.copyrightinformation_aboutshiwen)
LinearLayout copyrightinformation;
@BindView(R.id.serviceagreement_aboutshiwen)
LinearLayout serviceagreement;
@BindView(R.id.img_aboutshiwen)
ImageView img_aboutshiwen;
private String registrationId;
//调取系统摄像头的请求码
private static final int MY_ADD_CASE_CALL_PHONE = 6;
//打开相册的请求码
private static final int MY_ADD_CASE_CALL_PHONE2 = 7;
@Override
protected int layout() {
return R.layout.activity_about;
}
@Override
protected void initView() {
ButterKnife.bind(this);
}
@Override
protected void initData() {
}
/**
* 返回
*/
@OnClick(R.id.back_aboutshiwen)
public void onViewClicked() {
Intent intent = new Intent(AboutActivity.this, FirstActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
}
/**
* 点击事件
* @param view
*/
@OnClick({R.id.toscore_aboutshiwen, R.id.copyrightinformation_aboutshiwen, R.id.serviceagreement_aboutshiwen})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.toscore_aboutshiwen:
android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.pop_layout, null);//获取自定义布局
builder.setView(layout);
final android.support.v7.app.AlertDialog dlg = builder.create();
Window window = dlg.getWindow();
window.setGravity(Gravity.BOTTOM);
window.setWindowAnimations(R.style.PopAnimation);
//设置点击外围消散
dlg.setCanceledOnTouchOutside(true);
dlg.show();
WindowManager m = getWindowManager();
Display d = m.getDefaultDisplay(); //为获取屏幕宽、高
WindowManager.LayoutParams p = dlg.getWindow().getAttributes(); //获取对话框当前的参数值
p.width = d.getWidth(); //宽度设置为屏幕
window.setBackgroundDrawable(new ColorDrawable(0));
TextView xj_pop = layout.findViewById(R.id.xj_pop);
TextView xc_pop = layout.findViewById(R.id.xc_pop);
TextView cancel_pop = layout.findViewById(R.id.cancel_pop);
/**
* 取消按钮
*/
cancel_pop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dlg.dismiss();
}
});
/**
* 相机的按钮
*/
xj_pop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//"点击了照相";
// 6.0之后动态申请权限 摄像头调取权限,SD卡写入权限
if (ContextCompat.checkSelfPermission(AboutActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED
|| ContextCompat.checkSelfPermission(AboutActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(AboutActivity.this,
new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE},
MY_ADD_CASE_CALL_PHONE);
} else {
try {
//有权限,去打开摄像头
takePhoto();
} catch (IOException e) {
e.printStackTrace();
}
}
dlg.dismiss();
}
});
/**
* 相册机的按钮
*/
xc_pop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//"点击了相册");
// 6.0之后动态申请权限 SD卡写入权限
if (ContextCompat.checkSelfPermission(AboutActivity.this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(AboutActivity.this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
MY_ADD_CASE_CALL_PHONE2);
} else {
choosePhoto();
}
dlg.dismiss();
}
});
break;
case R.id.copyrightinformation_aboutshiwen:
break;
case R.id.serviceagreement_aboutshiwen:
break;
}
}
/**
* 打开相机
*/
private void takePhoto() throws IOException {
Intent intent = new Intent();
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
// 获取文件
File file = createFileIfNeed("UserIcon.png");
//拍照后原图回存入此路径下
Uri uri;
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) {
uri = Uri.fromFile(file);
} else {
/**
* 7.0 调用系统相机拍照不再允许使用Uri方式,应该替换为FileProvider
* 并且这样可以解决MIUI系统上拍照返回size为0的情况
*/
uri = FileProvider.getUriForFile(this, "${applicationId}.fileprovider", file);
}
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(intent, 1);
}
// 在sd卡中创建一保存图片(原图和缩略图共用的)文件夹
private File createFileIfNeed(String fileName) throws IOException {
String fileA = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath() + "/nbinpic";
File fileJA = new File(fileA);
if (!fileJA.exists()) {
fileJA.mkdirs();
}
File file = new File(fileA, fileName);
if (!file.exists()) {
file.createNewFile();
}
return file;
}
/**
* 打开相册
*/
private void choosePhoto() {
//这是打开系统默认的相册(就是你系统怎么分类,就怎么显示,首先展示分类列表)
Intent picture = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(picture, 2);
}
/**
* startActivityForResult执行后的回调方法,接收返回的图片
* @param requestCode
* @param resultCode
* @param data
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode != Activity.RESULT_CANCELED) {
String state = Environment.getExternalStorageState();
if (!state.equals(Environment.MEDIA_MOUNTED)) return;
// 把原图显示到界面上
Tiny.FileCompressOptions options = new Tiny.FileCompressOptions();
Tiny.getInstance().source(readpic()).asFile().withOptions(options).compress(new FileWithBitmapCallback() {
@Override
public void callback(boolean isSuccess, Bitmap bitmap, String outfile, Throwable t) {
saveImageToServer(bitmap, outfile);//显示图片到imgView上
}
});
} else if (requestCode == 2 && resultCode == Activity.RESULT_OK
&& null != data) {
try {
Uri selectedImage = data.getData();//获取路径
Tiny.FileCompressOptions options = new Tiny.FileCompressOptions();
Tiny.getInstance().source(selectedImage).asFile().withOptions(options).compress(new FileWithBitmapCallback() {
@Override
public void callback(boolean isSuccess, Bitmap bitmap, String outfile, Throwable t) {
saveImageToServer(bitmap, outfile);
}
});
} catch (Exception e) {
//"上传失败");
}
}
}
/**
* 从保存原图的地址读取图片
*/
private String readpic() {
String filePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath() + "/nbinpic/" + "UserIcon.png";
return filePath;
}
//TODO: 这里就可以将图片文件 file 上传到服务器,上传成功后可以将bitmap设置给你对应的图片展示
private void saveImageToServer(final Bitmap bitmap, String outfile) {
File file = new File(outfile);
img_aboutshiwen.setImageBitmap(bitmap);
}
/**
* 解绑
*/
@Override
protected void onDestroy() {
super.onDestroy();
ButterKnife.bind(this).unbind();
}
}
清单文件里:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.fileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>