*这是我的图库活动*
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_share);
//setupBottomNavigationView();
directories = new ArrayList<>();
directorySpinner = (Spinner)findViewById(R.id.spinnerDirectory);
mProgressbar = (LottieAnimationView)findViewById(R.id.mprogressbar);
galleryImage = (ImageView)findViewById(R.id.galleryImageView);
gridView = (GridView)findViewById(R.id.shareGridView);
back = (LottieAnimationView)findViewById(R.id.share_activity_back);
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "onClick: navigating back ");
finish();
}
});
takephoto = (Button)findViewById(R.id.Camera_take_photo);
takephoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (checkPermissions(Permissions.CAMERA_PERMISSION[0])){
Log.d(TAG, "onClick: starting camera");
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent,CAMERA_REQUEST_CODE);
}else {
}
}
});
if (checkPermissionsArray(Permissions.PERMISSIONS)){
}else{
verifyPermissions(Permissions.PERMISSIONS);
}
init();
}
public void verifyPermissions(String[] permissions){
Log.d(TAG, "verifyPermissions: verifying permissions.");
ActivityCompat.requestPermissions(
ShareActivity.this,
permissions,
VERIFY_PERMISSIONS_REQUEST
);
}
/**
* Check an array of permissions
* @param permissions
* @return
*/
public boolean checkPermissionsArray(String[] permissions){
Log.d(TAG, "checkPermissionsArray: checking permissions array.");
for(int i = 0; i< permissions.length; i++){
String check = permissions[i];
if(!checkPermissions(check)){
return false;
}
}
return true;
}
/**
* Check a single permission is it has been verified
* @param permission
* @return
*/
public boolean checkPermissions(String permission){
Log.d(TAG, "checkPermissions: checking permission: " + permission);
int permissionRequest = ActivityCompat.checkSelfPermission(ShareActivity.this, permission);
if(permissionRequest != PackageManager.PERMISSION_GRANTED){
Log.d(TAG, "checkPermissions: \n Permission was not granted for: " + permission);
Toast.makeText(mContext, " Please allow the permission \n for reading your external storage", Toast.LENGTH_LONG).show();
return false;
}
else{
Log.d(TAG, "checkPermissions: \n Permission was granted for: " + permission);
return true;
}
}
private void setupBottomNavigationView(){
Log.d(TAG, "setupBottomNavigationView: setting up bottam navigation view");
BottomNavigationViewEx bottomNavigationViewEx = (BottomNavigationViewEx)findViewById(R.id.bottom_nav_viewbar);
BottomNavigationViewHelper.setupBottomNavigationView(bottomNavigationViewEx);
BottomNavigationViewHelper.enableNAvigation(mContext,bottomNavigationViewEx);
Menu menu = bottomNavigationViewEx.getMenu();
MenuItem menuItem = menu.getItem(ACTIVITY_NUM);
menuItem.setChecked(true);
}
private void init(){
FilePaths filePaths = new FilePaths();
if (FileSearch.getDirectoryPaths(filePaths.PICTURES) != null){
directories = FileSearch.getDirectoryPaths(filePaths.PICTURES);
}
directories.add(filePaths.CAMERA);
directories.add(filePaths.VEDIO);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext,android.R.layout.simple_spinner_item,directories);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
directorySpinner.setAdapter(adapter);
directorySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
setupGridView(directories.get(position));
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
private void setupGridView(String selectedDirectory){
Log.d(TAG, "setupGridView: directory chosen: " + selectedDirectory);
final ArrayList<String> imgURLs = FileSearch.getFilePaths(selectedDirectory);
//set the grid column width
int gridWidth = getResources().getDisplayMetrics().widthPixels;
int imageWidth = gridWidth/NUM_GRID_COLUMNS;
gridView.setColumnWidth(imageWidth);
//use the grid adapter to adapter the images to gridview
GridImageAdapter adapter = new GridImageAdapter(mContext, R.layout.layout_gridview, mAppend, imgURLs);
gridView.setAdapter(adapter);
//set the first image to be displayed when the activity fragment view is inflated
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == CAMERA_REQUEST_CODE){
Log.d(TAG, "onActivityResult: done taking photo");
Log.d(TAG, "onActivityResult: attempting to take it to final share screen ");
Toast.makeText(mContext, "One more step to publish this photo ", Toast.LENGTH_SHORT).show();
}
}
}
这是我的File_search类和File_Path类
1 * File_Search类
public class FileSearch {
/**
* Search a directory and return a list of all **directories** contained inside
* @param directory
* @return
*/
public static ArrayList<String> getDirectoryPaths(String directory){
ArrayList<String> pathArray = new ArrayList<>();
File file = new File(directory);
File[] listfiles = file.listFiles();
for(int i = 0; i < listfiles.length; i++){
if(listfiles[i].isDirectory()){
pathArray.add(listfiles[i].getAbsolutePath());
}
}
return pathArray;
}
/**
* Search a directory and return a list of all **files** contained inside
* @param directory
* @return
*/
public static ArrayList<String> getFilePaths(String directory){
ArrayList<String> pathArray = new ArrayList<>();
File file = new File(directory);
File[] listfiles = file.listFiles();
for(int i = 0; i < listfiles.length; i++){
if(listfiles[i].isFile()){
pathArray.add(listfiles[i].getAbsolutePath());
}
}
return pathArray;
}
}
2 File_Path类
//"storage/emulated/0"
public String ROOT_DIR = Environment.getExternalStorageDirectory().getPath();
public String PICTURES = ROOT_DIR + "/Pictures";
public String CAMERA = ROOT_DIR + "/DCIM/camera";
//public String VEDIO = ROOT_DIR + "/videos";
public String FIREBASE_STORY_STORAGE = "stories/users";
public String FIREBASE_IMAGE_STORAGE = "photos/users/";
}
请在过去6小时内帮助解决这个问题
答案 0 :(得分:0)
如果您知道返回文件的类型,例如jpg,png,mp4等,那么您可以在自定义GridImageAdapter中区分它。在图像项目xml文件中,您可以创建另一个ImageView
,在每个项目的中心放置一个播放图标。如果有图像文件,此图标将被隐藏,但是,如果有视频文件,则可以在自定义适配器中显示此图标。
此外,您希望获得视频的持续时间,因为您可以使用MediaMetadataRetriever
,如下所示:
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
//use one of overloaded setDataSource() functions to set your data source
retriever.setDataSource(context, Uri.fromFile(videoFile));
String time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
long timeInMillisec = Long.parseLong(time );
retriever.release()