2014-07-09 3 views
20

मैं 4.4.2 पर हूं, यूरी के माध्यम से एक फ़ाइल (छवि) को हटाने की कोशिश कर रहा हूं। यहां मेरा कोड है:मैं एंड्रॉइड पर प्रोग्रामेटिक रूप से फ़ाइलों को कैसे हटा सकता हूं?

File file = new File(uri.getPath()); 
boolean deleted = file.delete(); 
if(!deleted){ 
     boolean deleted2 = file.getCanonicalFile().delete(); 
     if(!deleted2){ 
      boolean deleted3 = getApplicationContext().deleteFile(file.getName()); 
     } 
} 

अभी, इनमें से कोई भी डिलीट फ़ंक्शन वास्तव में फ़ाइल को हटा रहा है। मैं अपने AndroidManifest.xml में यह है:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" /> 
+0

मैं getCanonicalFile() का उपयोग नहीं करता, लेकिन केवल File.delete() और यह मेरे सिस्टम पर ठीक काम करता है। जब तक यूआरआई से आपका रास्ता मान्य नहीं है। –

+0

मेरा पथ यह है: /बाहरी/छवियों/मीडिया/2 9 18 क्या यह सही दिखता है? –

+0

... नहीं। कुछ '/ mnt/sdcard/your_folder/your_file.png' जैसा करता है। हालांकि, 'getExternalDirectory' के माध्यम से संग्रहण पथ प्राप्त करना बेहतर है। अंत में, अनुमति 'WRITE_EXTERNAL _... '' READ_EXTERNAL _... 'एक है। –

उत्तर

55

क्यों आप इस कोड के साथ यह परीक्षण नहीं है:

File fdelete = new File(uri.getPath()); 
if (fdelete.exists()) { 
    if (fdelete.delete()) { 
     System.out.println("file Deleted :" + uri.getPath()); 
    } else { 
     System.out.println("file not Deleted :" + uri.getPath()); 
    } 
} 

मुझे लगता है कि समस्या का एक हिस्सा आप फ़ाइल को नष्ट करने की कोशिश कभी नहीं किया जाता है, आप बस एक वेरिएबल बनाते रहें जिसमें एक विधि कॉल है।

तो आपके मामले में आप की कोशिश कर सकते:

File file = new File(uri.getPath()); 
file.delete(); 
if(file.exists()){ 
     file.getCanonicalFile().delete(); 
     if(file.exists()){ 
      getApplicationContext().deleteFile(file.getName()); 
     } 
} 

हालांकि मुझे लगता है कि एक छोटे से overkill है।

आपने एक टिप्पणी जोड़ा है कि आप एक यूरी के बजाय बाहरी निर्देशिका का उपयोग कर रहे हैं। तो इसके बजाय आपको कुछ जोड़ना चाहिए:

String root = Environment.getExternalStorageDirectory().toString(); 
File file = new File(root + "/images/media/2918"); 

फिर फ़ाइल को हटाने का प्रयास करें।

+0

मुझे लगता है कि आप कुछ पर हो सकते हैं। यह कहता रहता है कि fdelete मौजूद नहीं है। मेरा फ़ाइल पथ/बाहरी/छवियों/मीडिया/2 9 18 है। क्या यह सही दिखता है? –

+0

मुझे नहीं लगता कि आपका समाधान उसके मामले में काम करेगा क्योंकि वह पहले से ही ऐसा कर रहा है जैसा आपने उसे करने के लिए कहा था। – migos

+0

* .png या * .jpg जैसे कोई प्रत्यय नहीं है? 'यूरी के बजाय – migos

12

इसे आजमाएं। यह मेरे लिए काम कर रहा है।

handler.postDelayed(new Runnable() { 
          @Override 
          public void run() { 
           // Set up the projection (we only need the ID) 
           String[] projection = { MediaStore.Images.Media._ID }; 

// Match on the file path 
           String selection = MediaStore.Images.Media.DATA + " = ?"; 
           String[] selectionArgs = new String[] { imageFile.getAbsolutePath() }; 

           // Query for the ID of the media matching the file path 
           Uri queryUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; 
           ContentResolver contentResolver = getActivity().getContentResolver(); 
           Cursor c = contentResolver.query(queryUri, projection, selection, selectionArgs, null); 
           if (c.moveToFirst()) { 
            // We found the ID. Deleting the item via the content provider will also remove the file 
            long id = c.getLong(c.getColumnIndexOrThrow(MediaStore.Images.Media._ID)); 
            Uri deleteUri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id); 
            contentResolver.delete(deleteUri, null, null); 
           } else { 
            // File not found in media store DB 
           } 
           c.close(); 
          } 
         }, 5000); 
+0

मेरे लिए काम करता है – Guihgo

0

मुझे लगता है कि आपको अपना जवाब मिल गया है, हालांकि यह मेरे लिए काम नहीं करता है। रखा झूठी लौटने को हटाएँ, तो मैं निम्नलिखित की कोशिश की और यह काम किया (जिसे चुना जवाब काम नहीं किया के लिए किसी और के लिए):

System.out.println(new File(path).getAbsoluteFile().delete()); 

प्रणाली बाहर स्पष्ट रूप से अनदेखा किया जा सकता है, मैं यह पुष्टि करने की सुविधा के लिए डाल दिया हटाना

2

मैं नूगा एम्युलेटर पर यह कोड का परीक्षण किया और यह काम किया:

प्रकट ऐड में:

<?xml version="1.0" encoding="utf-8"?> 
<paths xmlns:android="http://schemas.android.com/apk/res/android"> 
    <external-path name="external_files" path="."/> 
    </paths> 
:

<application... 

    <provider 
     android:name="android.support.v4.content.FileProvider" 
     android:authorities="${applicationId}.provider" 
     android:exported="false" 
     android:grantUriPermissions="true"> 
     <meta-data 
      android:name="android.support.FILE_PROVIDER_PATHS" 
      android:resource="@xml/provider_paths"/> 
    </provider> 
</application> 

रेस फ़ोल्डर और provider_paths.xml में अतीत में खाली एक्सएमएल फ़ोल्डर बनाएँ

फिर अगला स्निपेट अपने कोड में रखें (उदाहरण के लिए खंड):

File photoLcl = new File(homeDirectory + "/" + fileNameLcl); 
Uri imageUriLcl = FileProvider.getUriForFile(getActivity(), 
    getActivity().getApplicationContext().getPackageName() + 
    ".provider", photoLcl); 
ContentResolver contentResolver = getActivity().getContentResolver(); 
contentResolver.delete(imageUriLcl, null, null); 
संबंधित मुद्दे

 संबंधित मुद्दे