自定义个 Android 的视频选择相册demo
时间: 2023-11-21 07:04:35 浏览: 36
好的,下面是一个简单的 Android 视频选择相册的 demo:
首先,在 AndroidManifest.xml 中添加以下权限:
```xml
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
```
接下来,在布局文件中添加一个用于显示选择的视频的 ImageView 和一个按钮用于打开相册:
```xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/selected_video"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:adjustViewBounds="true"
android:src="@drawable/ic_launcher_background" />
<Button
android:id="@+id/select_video_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="选择视频" />
</LinearLayout>
```
接下来,在 Activity 中添加以下代码:
```java
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_PICK_VIDEO = 1;
private ImageView mSelectedVideo;
private Button mSelectVideoButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSelectedVideo = findViewById(R.id.selected_video);
mSelectVideoButton = findViewById(R.id.select_video_button);
mSelectVideoButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, REQUEST_PICK_VIDEO);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_PICK_VIDEO && resultCode == RESULT_OK && data != null) {
Uri selectedVideo = data.getData();
mSelectedVideo.setImageURI(selectedVideo);
}
super.onActivityResult(requestCode, resultCode, data);
}
}
```
在这个代码中,我们在 onCreate 方法中获取到 ImageView 和 Button,然后为 Button 添加了一个点击事件,在点击事件中打开相册。当用户选择视频后,onActivityResult 方法会被调用,我们可以在这个方法中获取到选择的视频的 Uri,并将其设置到 ImageView 中。
这就是一个简单的 Android 视频选择相册的 demo,你可以根据自己的需求进行修改和扩展。