Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rename an image retrieved from phone gallery and save it with new name

I am getting image from gallery through this code:

accepted The other answers explained how to send the intent, but they didn't explain well how to handle the response. Here's some sample code on how to do that:

protected void onActivityResult(int requestCode, int resultCode, 
       Intent imageReturnedIntent) {
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

    switch(requestCode) { 
    case REQ_CODE_PICK_IMAGE:
        if(resultCode == RESULT_OK){  
            Uri selectedImage = imageReturnedIntent.getData();
            String[] filePathColumn = {MediaStore.Images.Media.DATA};

            Cursor cursor = getContentResolver().query(
                               selectedImage, filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String filePath = cursor.getString(columnIndex);
            cursor.close();


            Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
        }
    }
}

Now, I want to get the file name from filepath, rename it and save it again. Is this possible? If yes, how?

like image 361
Jolly Nature Avatar asked Oct 04 '13 18:10

Jolly Nature


1 Answers

Its always possible!

//get file
File photo = new File(filePath);

//file name
String fileName = photo.getName();

//resave file with new name
File newFile = new File("new file name");
photo.renameTo(newFile);
like image 173
ilovepjs Avatar answered Sep 28 '22 11:09

ilovepjs