Android – Cómo obtener el nombre de archivo seleccionado del documento
Estoy lanzando la intención de seleccionar documnets usando código siguiente.
private void showFileChooser() { Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("*/*"); intent.addCategory(Intent.CATEGORY_OPENABLE); try { startActivityForResult( Intent.createChooser(intent, "Select a File to Upload"), 1); } catch (android.content.ActivityNotFoundException ex) { // Potentially direct the user to the Market with a Dialog Toast.makeText(this, "Please install a File Manager.", Toast.LENGTH_SHORT).show(); } }
En los resultados onActivity cuando estoy tratando de obtener la ruta del archivo que está dando algún otro número en el lugar del nombre de archivo.
- ¿Cuál es la forma más rápida de acceder a los archivos?
- Cómo obtener datos de MediaStore.File y MediaStore.File.FileColumn
- ¿Cómo dejo que un usuario suba archivos al almacenamiento interno?
- Obtener la extensión de archivo del archivo elegido de una intención
- Cómo restaurar el archivo vcf sin utilizar la aplicación externa?
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case 1: if (resultCode == RESULT_OK) { // Get the Uri of the selected file Uri uri = data.getData(); File myFile = new File(uri.toString()); String path = myFile.getAbsolutePath(); } break; } super.onActivityResult(requestCode, resultCode, data); }
Ese valor de la trayectoria que estoy consiguiendo como esto. "Content: //com.android.providers.downloads.documents/document/1433" Pero quiero nombre de archivo real como doc1.pdf etc .. ¿Cómo conseguirlo?
- PhoneGap FileTransfer.download espera una ruta diferente que FileSystem proporciona
- Lectura de un archivo de caché previamente escrito por la misma aplicación
- Creación y eliminación de un archivo en la carpeta de activos automáticamente
- Escribir a las preferencias compartidas es demasiado lento?
- Guardar bitmap en la función de archivo
- Copia los archivos de una carpeta de la tarjeta SD en otra carpeta de la tarjeta SD
- Agregar imagen a twitter compartir intención android
- Cómo copiar archivo de imagen de la Galería a otra carpeta mediante programación en Android
Cuando obtienes un contenido: // uri, necesitarás consultar un resolvedor de contenido y luego tomar el nombre para mostrar.
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case 1: if (resultCode == RESULT_OK) { // Get the Uri of the selected file Uri uri = data.getData(); String uriString = uri.toString(); File myFile = new File(uriString); String path = myFile.getAbsolutePath(); String displayName = null; if (uriString.startsWith("content://")) { Cursor cursor = null; try { cursor = getActivity().getContentResolver().query(uri, null, null, null, null); if (cursor != null && cursor.moveToFirst()) { displayName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)); } } finally { cursor.close(); } } else if (uriString.startsWith("file://")) { displayName = myFile.getName(); } } break; } super.onActivityResult(requestCode, resultCode, data); }
Usted puede intentar esto, m Espero que le ayude u:
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); Log.i("inside", "onActivityResult"); if(requestCode == FILE_MANAGER_REQUEST_CODE) { // Check if the user actually selected an image: if(resultCode == Activity.RESULT_OK) { // This gets the URI of the image the user selected: Uri selectedFileURI = data.getData(); File file = new File(getRealPathFromURI(selectedFileURI)); // Create a new Intent to send to the next Activity: } } } private String getRealPathFromURI(Uri contentURI) { String result; Cursor cursor = getContentResolver().query(contentURI, null, null, null, null); if (cursor == null) { // Source is Dropbox or other similar local file path result = contentURI.getPath(); } else { cursor.moveToFirst(); int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); result = cursor.getString(idx); cursor.close(); } return result; }
- Cancelar la transición de retorno compartida en Android Lollipop
- Establecimiento de una etiqueta en una llamada de Retrofit para cancelar por etiqueta más tarde