2014-06-20 5 views
12

से चयनित फ़ाइल नाम कैसे प्राप्त करें मैं निम्नलिखित कोड का उपयोग करके डॉक्यूनेट का चयन करने के इरादे को लॉन्च कर रहा हूं।एंड्रॉइड - दस्तावेज़

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(); 
    } 
} 

onActivity में परिणाम जब मैं फ़ाइल पथ यह फ़ाइल नाम के स्थान पर किसी अन्य नंबर दे रहा है पाने के लिए कोशिश कर रहा हूँ।

@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); 
} 

वह पथ मूल्य मुझे यह मिल रहा है। "सामग्री: //com.android.providers.downloads.documents/document/1433" लेकिन मुझे वास्तविक फ़ाइल नाम जैसे doc1.pdf आदि चाहिए .. इसे कैसे प्राप्त करें?

+1

क्या आप मिलता है इस्तेमाल किया जा सकता फ़ाइल

private String getRealPathFromUri(Uri uri) { String[] projection = {MediaStore.Images.Media.DATA}; CursorLoader cursorLoader = new CursorLoader(thisActivity, uri, projection, null, null, null); Cursor cursor = cursorLoader.loadInBackground(); int column = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); String result = cursor.getString(column); cursor.close(); return result; } 

और उपरोक्त विधि के uri पथ नाम खोजने के लिए प्रयोग किया जाता है अगर आप myFile.getName() का उपयोग करते हैं? – Opiatefuchs

उत्तर

-1

आप इस कोशिश कर सकते हैं, मी मुझे आशा है कि यह यू में मदद मिलेगी:

@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; 
     } 
26

जब आप एक सामग्री मिल: // uri, यदि आप किसी सामग्री समाधानकर्ता क्वेरी करने के लिए और उसके बाद प्रदर्शन नाम हड़पने की आवश्यकता होगी।

सही पथ प्राप्त करने के लिए समारोह नीचे करने के लिए onActivityResult से अपने संदर्भ और यूआरआई वस्तु दर्रा:

@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); 
} 
+0

धन्यवाद @ cinthiaro आपने अपना दिन –

+3

फ़ाइल पथ कैसे प्राप्त किया? आप केवल फ़ाइल नाम –

+1

@ParthAnjaria प्राप्त कर रहे हैं। क्या आपको अंत में फ़ाइल पथ मिला? यदि हां, क्या आप इसे साझा कर सकते हैं, कृपया? –

0

यहाँ पूर्ण समाधान है

यह /storage/emulated/0/APMC Mahuva/Report20-11-2017.pdf के रूप में पथ (मैं कहाँ का चयन किया है देता है इस Report20-11-2017.pdf फ़ाइल)

String getFilePath(Context cntx, Uri uri) { 
    Cursor cursor = null; 
    try { 
     String[] arr = { MediaStore.Images.Media.DATA }; 
     cursor = cntx.getContentResolver().query(uri, arr, null, null, null); 
     int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
     cursor.moveToFirst(); 
     return cursor.getString(column_index); 
    } finally { 
     if (cursor != null) { 
      cursor.close(); 
     } 
    } 
} 
0

पहले जांच कर लें अनुमति आवेदन के लिए प्रदान किया जाता है .. विधि नीचे रन जाँच करने के लिए प्रयोग किया जाता है टाइम अनुमति '

public void onClick(View v) { 
      //Checks if the permission is Enabled or not... 
      if (ContextCompat.checkSelfPermission(thisActivity, 
        Manifest.permission.READ_EXTERNAL_STORAGE) 
        != PackageManager.PERMISSION_GRANTED) { 
       ActivityCompat.requestPermissions(thisActivity, 
         new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 
         REQUEST_PERMISSION); 
      } else { 
       Intent galleryIntent = new Intent(Intent.ACTION_PICK, 
         android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
       startActivityForResult(galleryIntent, USER_IMG_REQUEST); 
      } 
     } 

और विधि नीचे के रूप में

if (requestCode == USER_IMG_REQUEST && resultCode == RESULT_OK && data != null) { 
     Uri path = data.getData(); 
     try { 
      String imagePath = getRealPathFromUri(path); 
      File file = new File(imagePath); 
      RequestBody reqFile = RequestBody.create(MediaType.parse("image/*"), file); 
      MultipartBody.Part imageFile = MultipartBody.Part.createFormData("userImage", file.getName(), reqFile); 
संबंधित मुद्दे