Renaming selected image from gallery
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
add a comment |
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
if images are in same folder how would you handle renaming?
– Manohar Reddy
Nov 15 '18 at 4:38
add a comment |
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
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
android imageview file-rename
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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());
add a comment |
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()));
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 setgetExternalStorageDirectory
.
– 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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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());
add a comment |
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());
add a comment |
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());
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());
answered Nov 15 '18 at 4:50
Dhaval SolankiDhaval Solanki
2,72211324
2,72211324
add a comment |
add a comment |
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()));
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 setgetExternalStorageDirectory
.
– 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
add a comment |
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()));
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 setgetExternalStorageDirectory
.
– 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
add a comment |
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()));
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()));
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 setgetExternalStorageDirectory
.
– 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
add a comment |
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 setgetExternalStorageDirectory
.
– 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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
if images are in same folder how would you handle renaming?
– Manohar Reddy
Nov 15 '18 at 4:38