Android 对话框与相机,相册调用
2020-02-29 本文已影响0人
亡灵丶晓尘
import android.*;
import android.annotation.*;
import android.content.*;
import android.content.pm.*;
import android.database.*;
import android.graphics.*;
import android.net.*;
import android.os.*;
import android.provider.*;
import android.support.v4.app.*;
import android.support.v4.content.*;
import android.support.v7.app.*;
import android.view.*;
import android.widget.*;
import com.mycompany.myapp.*;
import android.util.*;
import java.io.*;
import android.support.annotation.*;
public class Grzxmain extends AppCompatActivity
{
ImageView toux;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.grzx);
toux = findViewById(R.id.imageview_toux);
toux.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
AlertDialog.Builder builder = new AlertDialog.Builder(Grzxmain.this);
builder.setTitle("选择头像方式");
builder.setMessage("请选择。");
builder.setPositiveButton("相机", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which)
{
xiangji();
}
});
builder.setNegativeButton("相册", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which)
{
if (ContextCompat.checkSelfPermission(Grzxmain.this, Manifest.permission
.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(Grzxmain.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
else
{
//执行启动相册的方法
openAlbum();
}
}
});
builder.show();
}
});
}
//相机
static final int REQUEST_IMAGE_CAPTURE = 1;
private void xiangji()
{
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null)
{
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
//获取权限的结果
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
{
if (requestCode == 1)
{
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) openAlbum();
else Toast.makeText(Grzxmain.this, "你拒绝了", Toast.LENGTH_SHORT).show();
}
}
//启动相册的方法
private void openAlbum()
{
Intent intent = new Intent("android.intent.action.GET_CONTENT");
intent.setType("image/*");
startActivityForResult(intent, 2);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data)
{
if (requestCode == 2)
{
//判断安卓版本
if (resultCode == RESULT_OK && data != null)
{
if (Build.VERSION.SDK_INT >= 19)
handImage(data);
else handImageLow(data);
}
}
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK)
{
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
toux.setImageBitmap(imageBitmap);
}
}
//安卓版本大于4.4的处理方法
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
private void handImage(Intent data)
{
String path =null;
Uri uri = data.getData();
//根据不同的uri进行不同的解析
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;
path = 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));
path = getImagePath(contentUri, null);
}
}
else if ("content".equalsIgnoreCase(uri.getScheme()))
{
path = getImagePath(uri, null);
}
else if ("file".equalsIgnoreCase(uri.getScheme()))
{
path = uri.getPath();
}
//展示图片
displayImage(path);
}
//安卓小于4.4的处理方法
private void handImageLow(Intent data)
{
Uri uri = data.getData();
String path = getImagePath(uri, null);
displayImage(path);
}
//content类型的uri获取图片路径的方法
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);
toux.setImageBitmap(bitmap);
}
else
{
Toast.makeText(this, "fail to set image", Toast.LENGTH_SHORT).show();
}
}
}