一、图片
public class PhotoActivity extends AppCompatActivity {
private static final int OPEN_CAMERA = 1;
private static final int OPEN_ALBUM = 2;
private ImageView imageView;
private Uri imageUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_photo);
Button getPhoto = (Button) findViewById(R.id.photo_get);
Button openAlbum = (Button) findViewById(R.id.photo_from);
imageView = (ImageView) findViewById(R.id.photo_show);
getPhoto.setOnClickListener(
view -> {
//新建文件
File file = new File(getExternalCacheDir(), "photo.jpg");
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
if (Build.VERSION.SDK_INT >= 24) {
imageUri = FileProvider.getUriForFile(this, "com.example.media.provider", file);
}
//打开相机
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, OPEN_CAMERA);
}
);
openAlbum.setOnClickListener(
view -> {
//判断权限
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
} else {
/**
* 打开相册
* 1、ACTION_PICK获取的是相册中的图片,返回的Uri格式:content://
*/
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(intent, OPEN_ALBUM);
}
}
);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
//拍照
case OPEN_CAMERA:
if (resultCode == RESULT_OK) {
try {
Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
imageView.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
break;
//从相册选择
case OPEN_ALBUM:
if (resultCode == RESULT_OK) {
Uri uri = data.getData();
try {
ParcelFileDescriptor parcelFileDescriptor = getContentResolver().openFileDescriptor(uri, "r");
FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
Bitmap bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor);
imageView.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
break;
}
}
}
//下面是我用之前写的,基本上完成了功能,但是读取download下图片会失败,这里记录一下
/**
*if (Build.VERSION.SDK_INT>=19){
* String imagePath=null;
* //获得封装过的Uri
* Uri uri=data.getData();
* //document类型
* if (DocumentsContract.isDocumentUri(this,uri)){
* String docId=DocumentsContract.getDocumentId(uri);
* if ("com.android.providers.media.documents".equals(uri.getAuthority())) {
* //获得id
* 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 contentUir = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(docId));
* imagePath = getImagePath(contentUir, null);
* }
* } else if ("content".equalsIgnoreCase(uri.getScheme())) {
* imagePath = getImagePath(uri, null);
* } else if ("file".equalsIgnoreCase(uri.getScheme())) {
* imagePath = uri.getPath();
* }
* //需要在application添加android:requestLegacyExternalStorage="true"属性
* Bitmap bitmap=BitmapFactory.decodeFile(imagePath);
* imageView.setImageBitmap(bitmap);
* }
*
*
* //通过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;
* }
*/
二、音频
public class MusicActivity extends AppCompatActivity {
private MediaPlayer mediaPlayer=new MediaPlayer();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_music);
Button start=(Button)findViewById(R.id.music_start);
Button pause=(Button)findViewById(R.id.music_pause);
Button stop=(Button)findViewById(R.id.music_stop);
//检查权限
if (ContextCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
try {
AssetFileDescriptor assetFileDescriptor=getAssets().openFd("music/yue.mp3");
Log.d("tag", ""+assetFileDescriptor);
if (Build.VERSION.SDK_INT>=24) {
mediaPlayer.setDataSource(assetFileDescriptor);
mediaPlayer.prepare();
}
} catch (IOException e) {
e.printStackTrace();
}
start.setOnClickListener(
view -> {
mediaPlayer.start();
}
);
pause.setOnClickListener(
view -> {
mediaPlayer.pause();
}
);
stop.setOnClickListener(
view -> {
mediaPlayer.stop();
}
);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mediaPlayer!=null){
mediaPlayer.stop();
mediaPlayer.release();
}
}
}
三、视频
public class VideoActivity extends AppCompatActivity {
private VideoView videoView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_vedio);
Button start=(Button)findViewById(R.id.video_start);
Button pause=(Button)findViewById(R.id.video_pause);
Button restart=(Button)findViewById(R.id.video_restart);
videoView=(VideoView)findViewById(R.id.video_view);
Uri uri=Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.yang);
videoView.setVideoURI(uri);
start.setOnClickListener(
view -> {
if (!videoView.isPlaying()){
videoView.start();
}
}
);
pause.setOnClickListener(
view -> {
if (videoView.isPlaying()){
videoView.pause();
}
}
);
restart.setOnClickListener(
view -> {
videoView.resume();
}
);
}
@Override
protected void onDestroy() {
super.onDestroy();
videoView.suspend();
}
}