利用GET_CONTENT调用系统图片并裁剪
只是在昨天的例子里面加了一个按钮而已,不过这里的话,图片选取要选小一点的,反正好多图都没法用。
btnChoose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
File photo = new File(Environment.getExternalStorageDirectory(), "tempImage.jpg");
if (photo.exists()) {
photo.delete();
}
try {
photo.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
/*
bug0001:
before:Uri imageUri = Uri.fromFile(photo);
这里应该用全局变量imageUri
*/
imageUri = Uri.fromFile(photo);
//调用系统任务
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
/*
bug0002:
before:intent.putExtra("crop", true);
这里应该用字符串的true
并且选择的图片要小,300、400kb的样子吧
*/
intent.putExtra("crop", "true");
intent.putExtra("scale", true);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
//这里复用了代码,选择完图片直接进入显示图片的逻辑
startActivityForResult(intent, SHOW_PHOTO);
}
});
简单的音乐播放器
播放的是sd卡根目录的winter.mp3
package com.jackie.musicplayer;
import android.media.MediaPlayer;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import java.io.File;
import java.io.IOException;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
MediaPlayer mPlayer = new MediaPlayer();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btnStart).setOnClickListener(this);
findViewById(R.id.btnPause).setOnClickListener(this);
findViewById(R.id.btnStop).setOnClickListener(this);
initMediaPlayer();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnStart:
if (!mPlayer.isPlaying()) {
mPlayer.start();
}
break;
case R.id.btnPause:
if (mPlayer.isPlaying()) {
mPlayer.pause();
}
break;
case R.id.btnStop:
if (mPlayer.isPlaying()) {
//如果用了stop,那么这个MediaPlayer就不能再播放音乐了
mPlayer.reset();
initMediaPlayer();
}
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mPlayer != null) {
//弃用播放器
mPlayer.stop();
//释放资源
mPlayer.release();
}
}
private void initMediaPlayer() {
//初始化MediaPlayer
/*
bug0001:
java.io.FileNotFoundException: /storage/emulated/0/winter.mp3: open failed: EACCES (Permission denied)
不用说了,忘记给权限了,读取SDcard的权限
*/
File file = new File(Environment.getExternalStorageDirectory(), "winter.mp3");
try {
//指定播放源
mPlayer.setDataSource(file.getPath());
//准备播放
mPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
}
}
简单的视频播放器
播放指定路径(sd卡根目录下video.mp4)文件,VideoView既是一个控件,又是一个类似MediaPlayer的类,可以播放视频文件
package com.jackie.videoplayer;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.VideoView;
import java.io.File;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private VideoView mPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btnPlay).setOnClickListener(this);
findViewById(R.id.btnPause).setOnClickListener(this);
findViewById(R.id.btnReplay).setOnClickListener(this);
mPlayer = (VideoView) findViewById(R.id.videoView);
File file = new File(Environment.getExternalStorageDirectory(), "video.mp4");
mPlayer.setVideoPath(file.getPath());
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnPlay:
if (!mPlayer.isPlaying()) {
mPlayer.start();
}
break;
case R.id.btnPause:
if (mPlayer.isPlaying()) {
mPlayer.pause();
}
break;
case R.id.btnReplay:
if (mPlayer.isPlaying()) {
//如果当前视频正在播放,重新播放
mPlayer.resume();
}
break;
}
}
}