Harsh Vardhan
Harsh Vardhan

Reputation: 39

How to rename a Video from MediaStore in android studio

I am making a video player app and i am adding a rename function but i can't rename the files i have tried many different methods but they are not working.

Here is the code i am using

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();

I am getting the file path from an array list from a cursor. The boolean is returning false value and the files name is not changing.

 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;
}

Upvotes: 1

Views: 2460

Answers (2)

ahmadu suleiman
ahmadu suleiman

Reputation: 71

The accepted answer is outdated. This is what I used Intent.ACTION_MEDIA_SCANNER_SCAN_FILE is deperecated Callers should migrate to inserting items directly into MediaStore, where they will be automatically scanned after each mutation.

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(); });

Upvotes: 0

Harsh Vardhan
Harsh Vardhan

Reputation: 39

So i understood how to do it i am using this code

 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();

Upvotes: 2

Related Questions