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

Manohar
  • 22,116
  • 9
  • 108
  • 144
jp singh
  • 324
  • 2
  • 13

2 Answers2

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()));
}
Farhana Naaz Ansari
  • 7,524
  • 26
  • 65
  • 105
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 String[]{f.getAbsolutePath()});
    }
Dhaval Solanki
  • 4,589
  • 1
  • 23
  • 39