Renaming selected image from gallery










0















I'm trying to make an app where user selects an Image from gallery and can rename it to desired name,
The code to get the Image from gallery is



Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, RESULT_LOAD_IMG);

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
super.onActivityResult(requestCode, resultCode, data);

if (resultCode == RESULT_OK)
try
upload.setVisibility(View.INVISIBLE);
Image.setVisibility(View.VISIBLE);
final Uri imageUri = data.getData();
final InputStream imageStream = getContentResolver().openInputStream(imageUri);
final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);

Image.setImageBitmap(selectedImage);
Log.i("FileName is", fileName);
catch (FileNotFoundException e)
e.printStackTrace();
Toast.makeText(getApplicationContext(),"Error Loading Image",Toast.LENGTH_LONG).show();

else
Toast.makeText(getApplicationContext(),"Please select an image",Toast.LENGTH_LONG).show();




I've seen some posts which use from and to to rename Images but I'm not able to understand how it works and how to set a desired name(which will be entered by user via an editText).



Any help would be appreciated










share|improve this question
























  • if images are in same folder how would you handle renaming?

    – Manohar Reddy
    Nov 15 '18 at 4:38















0















I'm trying to make an app where user selects an Image from gallery and can rename it to desired name,
The code to get the Image from gallery is



Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, RESULT_LOAD_IMG);

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
super.onActivityResult(requestCode, resultCode, data);

if (resultCode == RESULT_OK)
try
upload.setVisibility(View.INVISIBLE);
Image.setVisibility(View.VISIBLE);
final Uri imageUri = data.getData();
final InputStream imageStream = getContentResolver().openInputStream(imageUri);
final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);

Image.setImageBitmap(selectedImage);
Log.i("FileName is", fileName);
catch (FileNotFoundException e)
e.printStackTrace();
Toast.makeText(getApplicationContext(),"Error Loading Image",Toast.LENGTH_LONG).show();

else
Toast.makeText(getApplicationContext(),"Please select an image",Toast.LENGTH_LONG).show();




I've seen some posts which use from and to to rename Images but I'm not able to understand how it works and how to set a desired name(which will be entered by user via an editText).



Any help would be appreciated










share|improve this question
























  • if images are in same folder how would you handle renaming?

    – Manohar Reddy
    Nov 15 '18 at 4:38













0












0








0








I'm trying to make an app where user selects an Image from gallery and can rename it to desired name,
The code to get the Image from gallery is



Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, RESULT_LOAD_IMG);

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
super.onActivityResult(requestCode, resultCode, data);

if (resultCode == RESULT_OK)
try
upload.setVisibility(View.INVISIBLE);
Image.setVisibility(View.VISIBLE);
final Uri imageUri = data.getData();
final InputStream imageStream = getContentResolver().openInputStream(imageUri);
final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);

Image.setImageBitmap(selectedImage);
Log.i("FileName is", fileName);
catch (FileNotFoundException e)
e.printStackTrace();
Toast.makeText(getApplicationContext(),"Error Loading Image",Toast.LENGTH_LONG).show();

else
Toast.makeText(getApplicationContext(),"Please select an image",Toast.LENGTH_LONG).show();




I've seen some posts which use from and to to rename Images but I'm not able to understand how it works and how to set a desired name(which will be entered by user via an editText).



Any help would be appreciated










share|improve this question
















I'm trying to make an app where user selects an Image from gallery and can rename it to desired name,
The code to get the Image from gallery is



Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, RESULT_LOAD_IMG);

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
super.onActivityResult(requestCode, resultCode, data);

if (resultCode == RESULT_OK)
try
upload.setVisibility(View.INVISIBLE);
Image.setVisibility(View.VISIBLE);
final Uri imageUri = data.getData();
final InputStream imageStream = getContentResolver().openInputStream(imageUri);
final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);

Image.setImageBitmap(selectedImage);
Log.i("FileName is", fileName);
catch (FileNotFoundException e)
e.printStackTrace();
Toast.makeText(getApplicationContext(),"Error Loading Image",Toast.LENGTH_LONG).show();

else
Toast.makeText(getApplicationContext(),"Please select an image",Toast.LENGTH_LONG).show();




I've seen some posts which use from and to to rename Images but I'm not able to understand how it works and how to set a desired name(which will be entered by user via an editText).



Any help would be appreciated







android imageview file-rename






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 4:40









Manohar Reddy

6,21933561




6,21933561










asked Nov 15 '18 at 4:30









jp singhjp singh

549




549












  • if images are in same folder how would you handle renaming?

    – Manohar Reddy
    Nov 15 '18 at 4:38

















  • if images are in same folder how would you handle renaming?

    – Manohar Reddy
    Nov 15 '18 at 4:38
















if images are in same folder how would you handle renaming?

– Manohar Reddy
Nov 15 '18 at 4:38





if images are in same folder how would you handle renaming?

– Manohar Reddy
Nov 15 '18 at 4:38












2 Answers
2






active

oldest

votes


















0














Here is the code which may help you



 File sdcard = Environment.getExternalStorageDirectory();
File from = new File(sdcard, "from.txt");
File to = new File(sdcard, "to.txt");
if (updateFileName(context, from, to))
Log.d("File Rename", "Successfully renamed");
else Log.d("File Rename", "Rename filed");

public static boolean updateFileName(Context mContext, File from, File to)
if (from.renameTo(to))
removeMedia(mContext, from);
updateMediaInGallery(mContext, to);
return true;
else
return false;



public static void updateMediaInGallery(Context c, File f)
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(f));
c.sendBroadcast(intent);


private static void removeMedia(Context c, File f)
ContentResolver resolver = c.getContentResolver();
resolver.delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, MediaStore.Images.Media.DATA + "=?", new Stringf.getAbsolutePath());






share|improve this answer






























    0














    Here you can find your relevant solution of @Davewebb and you are confused in from to



    File sdcard = Environment.getExternalStorageDirectory();
    File from = new File(sdcard,"from.txt");
    File to = new File(sdcard,"to.txt");
    from.renameTo(to);


    from.txt is your old name and new name will be here is to.txt



    for internal Storage, you can read a file like this and will do the same above process



    private String readFileFromInternalStorage()
    ImageView imageView = (ImageView) findViewById(R.id.image);
    ContextWrapper cw = new ContextWrapper(context);

    //path to /data/data/yourapp/app_data/dirName
    File directory = cw.getDir("dirName", Context.MODE_PRIVATE);
    File mypath=new File(directory,"imagename.jpg");

    ImageView imageView = (ImageView) findViewById(R.id.image);
    imageView.setImageDrawable(Drawable.createFromPath(mypath.toString()));






    share|improve this answer




















    • 1





      Thanks alot, however What if the user selects the file from internal storage? Would it work tge same way?

      – jp singh
      Nov 15 '18 at 4:40











    • no, I think you don't need to set getExternalStorageDirectory.

      – farhana
      Nov 15 '18 at 4:54











    • Then instead of 'sdcard' what we have to pass in? 'imageUri'?

      – jp singh
      Nov 15 '18 at 4:56











    • @jpsingh see updated code

      – farhana
      Nov 15 '18 at 4:59










    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312462%2frenaming-selected-image-from-gallery%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Here is the code which may help you



     File sdcard = Environment.getExternalStorageDirectory();
    File from = new File(sdcard, "from.txt");
    File to = new File(sdcard, "to.txt");
    if (updateFileName(context, from, to))
    Log.d("File Rename", "Successfully renamed");
    else Log.d("File Rename", "Rename filed");

    public static boolean updateFileName(Context mContext, File from, File to)
    if (from.renameTo(to))
    removeMedia(mContext, from);
    updateMediaInGallery(mContext, to);
    return true;
    else
    return false;



    public static void updateMediaInGallery(Context c, File f)
    Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    intent.setData(Uri.fromFile(f));
    c.sendBroadcast(intent);


    private static void removeMedia(Context c, File f)
    ContentResolver resolver = c.getContentResolver();
    resolver.delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, MediaStore.Images.Media.DATA + "=?", new Stringf.getAbsolutePath());






    share|improve this answer



























      0














      Here is the code which may help you



       File sdcard = Environment.getExternalStorageDirectory();
      File from = new File(sdcard, "from.txt");
      File to = new File(sdcard, "to.txt");
      if (updateFileName(context, from, to))
      Log.d("File Rename", "Successfully renamed");
      else Log.d("File Rename", "Rename filed");

      public static boolean updateFileName(Context mContext, File from, File to)
      if (from.renameTo(to))
      removeMedia(mContext, from);
      updateMediaInGallery(mContext, to);
      return true;
      else
      return false;



      public static void updateMediaInGallery(Context c, File f)
      Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
      intent.setData(Uri.fromFile(f));
      c.sendBroadcast(intent);


      private static void removeMedia(Context c, File f)
      ContentResolver resolver = c.getContentResolver();
      resolver.delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, MediaStore.Images.Media.DATA + "=?", new Stringf.getAbsolutePath());






      share|improve this answer

























        0












        0








        0







        Here is the code which may help you



         File sdcard = Environment.getExternalStorageDirectory();
        File from = new File(sdcard, "from.txt");
        File to = new File(sdcard, "to.txt");
        if (updateFileName(context, from, to))
        Log.d("File Rename", "Successfully renamed");
        else Log.d("File Rename", "Rename filed");

        public static boolean updateFileName(Context mContext, File from, File to)
        if (from.renameTo(to))
        removeMedia(mContext, from);
        updateMediaInGallery(mContext, to);
        return true;
        else
        return false;



        public static void updateMediaInGallery(Context c, File f)
        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        intent.setData(Uri.fromFile(f));
        c.sendBroadcast(intent);


        private static void removeMedia(Context c, File f)
        ContentResolver resolver = c.getContentResolver();
        resolver.delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, MediaStore.Images.Media.DATA + "=?", new Stringf.getAbsolutePath());






        share|improve this answer













        Here is the code which may help you



         File sdcard = Environment.getExternalStorageDirectory();
        File from = new File(sdcard, "from.txt");
        File to = new File(sdcard, "to.txt");
        if (updateFileName(context, from, to))
        Log.d("File Rename", "Successfully renamed");
        else Log.d("File Rename", "Rename filed");

        public static boolean updateFileName(Context mContext, File from, File to)
        if (from.renameTo(to))
        removeMedia(mContext, from);
        updateMediaInGallery(mContext, to);
        return true;
        else
        return false;



        public static void updateMediaInGallery(Context c, File f)
        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        intent.setData(Uri.fromFile(f));
        c.sendBroadcast(intent);


        private static void removeMedia(Context c, File f)
        ContentResolver resolver = c.getContentResolver();
        resolver.delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, MediaStore.Images.Media.DATA + "=?", new Stringf.getAbsolutePath());







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 4:50









        Dhaval SolankiDhaval Solanki

        2,72211324




        2,72211324























            0














            Here you can find your relevant solution of @Davewebb and you are confused in from to



            File sdcard = Environment.getExternalStorageDirectory();
            File from = new File(sdcard,"from.txt");
            File to = new File(sdcard,"to.txt");
            from.renameTo(to);


            from.txt is your old name and new name will be here is to.txt



            for internal Storage, you can read a file like this and will do the same above process



            private String readFileFromInternalStorage()
            ImageView imageView = (ImageView) findViewById(R.id.image);
            ContextWrapper cw = new ContextWrapper(context);

            //path to /data/data/yourapp/app_data/dirName
            File directory = cw.getDir("dirName", Context.MODE_PRIVATE);
            File mypath=new File(directory,"imagename.jpg");

            ImageView imageView = (ImageView) findViewById(R.id.image);
            imageView.setImageDrawable(Drawable.createFromPath(mypath.toString()));






            share|improve this answer




















            • 1





              Thanks alot, however What if the user selects the file from internal storage? Would it work tge same way?

              – jp singh
              Nov 15 '18 at 4:40











            • no, I think you don't need to set getExternalStorageDirectory.

              – farhana
              Nov 15 '18 at 4:54











            • Then instead of 'sdcard' what we have to pass in? 'imageUri'?

              – jp singh
              Nov 15 '18 at 4:56











            • @jpsingh see updated code

              – farhana
              Nov 15 '18 at 4:59















            0














            Here you can find your relevant solution of @Davewebb and you are confused in from to



            File sdcard = Environment.getExternalStorageDirectory();
            File from = new File(sdcard,"from.txt");
            File to = new File(sdcard,"to.txt");
            from.renameTo(to);


            from.txt is your old name and new name will be here is to.txt



            for internal Storage, you can read a file like this and will do the same above process



            private String readFileFromInternalStorage()
            ImageView imageView = (ImageView) findViewById(R.id.image);
            ContextWrapper cw = new ContextWrapper(context);

            //path to /data/data/yourapp/app_data/dirName
            File directory = cw.getDir("dirName", Context.MODE_PRIVATE);
            File mypath=new File(directory,"imagename.jpg");

            ImageView imageView = (ImageView) findViewById(R.id.image);
            imageView.setImageDrawable(Drawable.createFromPath(mypath.toString()));






            share|improve this answer




















            • 1





              Thanks alot, however What if the user selects the file from internal storage? Would it work tge same way?

              – jp singh
              Nov 15 '18 at 4:40











            • no, I think you don't need to set getExternalStorageDirectory.

              – farhana
              Nov 15 '18 at 4:54











            • Then instead of 'sdcard' what we have to pass in? 'imageUri'?

              – jp singh
              Nov 15 '18 at 4:56











            • @jpsingh see updated code

              – farhana
              Nov 15 '18 at 4:59













            0












            0








            0







            Here you can find your relevant solution of @Davewebb and you are confused in from to



            File sdcard = Environment.getExternalStorageDirectory();
            File from = new File(sdcard,"from.txt");
            File to = new File(sdcard,"to.txt");
            from.renameTo(to);


            from.txt is your old name and new name will be here is to.txt



            for internal Storage, you can read a file like this and will do the same above process



            private String readFileFromInternalStorage()
            ImageView imageView = (ImageView) findViewById(R.id.image);
            ContextWrapper cw = new ContextWrapper(context);

            //path to /data/data/yourapp/app_data/dirName
            File directory = cw.getDir("dirName", Context.MODE_PRIVATE);
            File mypath=new File(directory,"imagename.jpg");

            ImageView imageView = (ImageView) findViewById(R.id.image);
            imageView.setImageDrawable(Drawable.createFromPath(mypath.toString()));






            share|improve this answer















            Here you can find your relevant solution of @Davewebb and you are confused in from to



            File sdcard = Environment.getExternalStorageDirectory();
            File from = new File(sdcard,"from.txt");
            File to = new File(sdcard,"to.txt");
            from.renameTo(to);


            from.txt is your old name and new name will be here is to.txt



            for internal Storage, you can read a file like this and will do the same above process



            private String readFileFromInternalStorage()
            ImageView imageView = (ImageView) findViewById(R.id.image);
            ContextWrapper cw = new ContextWrapper(context);

            //path to /data/data/yourapp/app_data/dirName
            File directory = cw.getDir("dirName", Context.MODE_PRIVATE);
            File mypath=new File(directory,"imagename.jpg");

            ImageView imageView = (ImageView) findViewById(R.id.image);
            imageView.setImageDrawable(Drawable.createFromPath(mypath.toString()));







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 15 '18 at 4:59

























            answered Nov 15 '18 at 4:36









            farhanafarhana

            2,51242134




            2,51242134







            • 1





              Thanks alot, however What if the user selects the file from internal storage? Would it work tge same way?

              – jp singh
              Nov 15 '18 at 4:40











            • no, I think you don't need to set getExternalStorageDirectory.

              – farhana
              Nov 15 '18 at 4:54











            • Then instead of 'sdcard' what we have to pass in? 'imageUri'?

              – jp singh
              Nov 15 '18 at 4:56











            • @jpsingh see updated code

              – farhana
              Nov 15 '18 at 4:59












            • 1





              Thanks alot, however What if the user selects the file from internal storage? Would it work tge same way?

              – jp singh
              Nov 15 '18 at 4:40











            • no, I think you don't need to set getExternalStorageDirectory.

              – farhana
              Nov 15 '18 at 4:54











            • Then instead of 'sdcard' what we have to pass in? 'imageUri'?

              – jp singh
              Nov 15 '18 at 4:56











            • @jpsingh see updated code

              – farhana
              Nov 15 '18 at 4:59







            1




            1





            Thanks alot, however What if the user selects the file from internal storage? Would it work tge same way?

            – jp singh
            Nov 15 '18 at 4:40





            Thanks alot, however What if the user selects the file from internal storage? Would it work tge same way?

            – jp singh
            Nov 15 '18 at 4:40













            no, I think you don't need to set getExternalStorageDirectory.

            – farhana
            Nov 15 '18 at 4:54





            no, I think you don't need to set getExternalStorageDirectory.

            – farhana
            Nov 15 '18 at 4:54













            Then instead of 'sdcard' what we have to pass in? 'imageUri'?

            – jp singh
            Nov 15 '18 at 4:56





            Then instead of 'sdcard' what we have to pass in? 'imageUri'?

            – jp singh
            Nov 15 '18 at 4:56













            @jpsingh see updated code

            – farhana
            Nov 15 '18 at 4:59





            @jpsingh see updated code

            – farhana
            Nov 15 '18 at 4:59

















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312462%2frenaming-selected-image-from-gallery%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            ReactJS Fetched API data displays live - need Data displayed static

            Evgeni Malkin