如何在安卓工作室中从MediaStore重命名视频



我正在制作一个视频播放器应用程序,并添加了一个重命名功能,但我无法重命名文件。我尝试了许多不同的方法,但都不起作用。

这是我使用的代码

final Dialog dialog = new Dialog(getContext());
dialog.setContentView(R.layout.rename_layout);
dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
final EditText editText = dialog.findViewById(R.id.rename_edit_text);
Button cancel = dialog.findViewById(R.id.cancel_rename_button);
Button rename_btn = dialog.findViewById(R.id.rename_button);
final File file = new File(new File(path).getAbsolutePath());
editText.setText(file.getName());
editText.requestFocus();
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
rename_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = editText.getText().toString();
boolean renamed = file.renameTo(new File(name));
Log.i("name_i", file.getName());
Toast.makeText(getContext(), String.valueOf(renamed), Toast.LENGTH_SHORT).show();
mAdapter.notifyDataSetChanged();
dialog.dismiss();
}
});
mAdapter.notifyDataSetChanged();
dialog.show();

我正在从一个光标的数组列表中获取文件路径。布尔值返回false值,并且文件名没有更改。

public ArrayList<File> GetVideoFile()
{
mSwipeRefreshLayout.setRefreshing(true);
ContentResolver contentResolver = getContext().getContentResolver();
Uri videoUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
String sortOrder = MediaStore.Video.Media.DATE_ADDED + " ASC";
Cursor cursor = contentResolver.query(videoUri, null, null, null, sortOrder);
try {
if (cursor != null && cursor.moveToFirst()) {
int videoPath = cursor.getColumnIndex(MediaStore.Video.Media.DATA);
do {
String path = cursor.getString(videoPath);
File file = new File(path);
fileArrayList.add(file);
} while (cursor.moveToNext());
}
} catch (Exception e) {
Log.i("CursorHandleException", e.getMessage());
mSwipeRefreshLayout.setRefreshing(false);
}
Collections.reverse(fileArrayList);
mSwipeRefreshLayout.setRefreshing(false);
return fileArrayList;
}

所以我明白了如何做到这一点,我使用的是这个代码

final Dialog dialog = new Dialog(getContext());
dialog.setContentView(R.layout.rename_layout);
dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
final EditText editText = dialog.findViewById(R.id.rename_edit_text);
Button cancel = dialog.findViewById(R.id.cancel_rename_button);
Button rename_btn = dialog.findViewById(R.id.rename_button);
final File file = new File(path);
String nameText = file.getName();
nameText = nameText.substring(0,nameText.lastIndexOf("."));
editText.setText(nameText);
editText.requestFocus();
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
rename_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String onlyPath = file.getParentFile().getAbsolutePath();
String ext = file.getAbsolutePath();
ext = ext.substring(ext.lastIndexOf("."));
String newPath = onlyPath+"/"+editText.getText().toString()+ext;
File newFile = new File(newPath);
boolean rename = file.renameTo(newFile);
if(rename)
{
ContentResolver resolver = getActivity().getApplicationContext().getContentResolver();
resolver.delete(
MediaStore.Files.getContentUri("external")
, MediaStore.MediaColumns.DATA + "=?", new String[] { file.getAbsolutePath() });
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(newFile));
getActivity().getApplicationContext().sendBroadcast(intent);
Toast.makeText(getContext(), "SuccessFull!", Toast.LENGTH_SHORT).show();
}else
{
Toast.makeText(getContext(), "Oops! rename failed", Toast.LENGTH_SHORT).show();
}
fileArrayList.clear();
GetVideoFile();
mAdapter.notifyDataSetChanged();
mAdapter.notifyItemChanged(adapterPosition);
dialog.dismiss();
}
});
mAdapter.notifyDataSetChanged();
dialog.show();

接受的答案已过时。这是我用过的Intent.ACTION_MEDIA_SCANNER_SCAN_FILE未经修饰。呼叫者应迁移到将项目直接插入MediaStore中,在每次突变后将自动扫描这些项目。

String extension = videoFile.getAbsolutePath(); extension = extension.substring(extension.lastIndexOf("."));
ContentValues values = new ContentValues(2); values.put(MediaStore.Video.Media.TITLE, title);
values.put(MediaStore.Video.Media.DISPLAY_NAME, title + extension );
context.getContentResolver().update(MediaStore.Video.Media. EXTERNAL_CONTENT_URI, values,
MediaStore.MediaColumns.DATA + "=?", new String[]{videoFile. getAbsolutePath()});
if (videoOptionListener != null) { videoOptionListener.onEdit();
} dialogRenameVideo.dismiss(); });

最新更新