Skip to content Skip to sidebar Skip to footer

How To Get Directory And File Name Of An Image Selected From Gallery

I need to use this function, java.io.File.File(File dir, String name) public File (File dir, String name) Added in API level 1 Constructs a new file using the specified direc

Solution 1:

The EXTRA_ALLOW_MULTIPLE option is set on the intent through the Intent.putExtra() method:

intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);

Your code above should look like this:

Intenti=newIntent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE );
//intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);

Note: the EXTRA_ALLOW_MULTIPLE option is only available in Android API 18 and higher.

@OverrideprotectedvoidonActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_LOAD_IMAGE  && resultCode == RESULT_OK && null != data) {
    UriselectedImage= data.getData();
    String[] filePathColumn = { MediaStore.Images.Media.DATA };

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

    intcolumnIndex= cursor.getColumnIndex(filePathColumn[0]);
    StringpicturePath= cursor.getString(columnIndex);
    casted_image = newFile(picturePath);
    cursor.close();
    // String picturePath contains the path of selected Image
  }
}

Solution 2:

You can do like this.

FileselectedFile= Environment.getExternalStorageDirectory();
FileparentDir= selectedFile.getParentFile();

casted_image = newFile(parentDir, "attachment.jpg");

or if you want to pass selected file's name instead of specific filename. use following line.

casted_image = new File(parentDir, selectedFile.getName());

you can also check whether selected file is directory or a ordinary file, by using selectedFile.isFile(); & selectedFile.isDirectory();

Post a Comment for "How To Get Directory And File Name Of An Image Selected From Gallery"