2009-08-08 16 views
126

मैं एक ईमेल संलग्नक के रूप में भेजने के लिए एक फाइल बना रहा हूं। अब मैं ईमेल भेजने के बाद छवि को मिटाना चाहता हूं। क्या फ़ाइल को हटाने का कोई तरीका है?एसडी कार्ड से फ़ाइल को कैसे हटाएं?

मैंने myFile.delete(); को आजमाया है लेकिन यह फ़ाइल को हटा नहीं गया है।


मैं एंड्रॉइड के लिए इस कोड का उपयोग कर रहा हूं, इसलिए प्रोग्रामिंग भाषा एसडी कार्ड तक पहुंचने के सामान्य एंड्रॉइड तरीकों का उपयोग कर जावा है। मैं onActivityResult विधि में फ़ाइल को हटा रहा हूं, जब एक ईमेल भेजने के बाद Intent स्क्रीन पर वापस आ जाता है।

+0

आप की जरूरत है समस्या के बारे में अधिक जानकारी प्रदान करें, जैसे कि आप किस भाषा का उपयोग कर रहे हैं, आप एसडी कार्ड का उपयोग कैसे कर रहे हैं, आदि – Amok

+0

क्या आप डिस्क में बदलावों को फ़्लश कर रहे हैं? –

+10

@Amuck मुझे लगता है कि यह मानना ​​सुरक्षित है कि वह जावा का उपयोग कर रहा है क्योंकि उसने निर्दिष्ट नहीं किया था। –

उत्तर

352
File file = new File(selectedFilePath); 
boolean deleted = file.delete(); 

जहां selectedFilePath फ़ाइल का रास्ता आप हटाना चाहते हैं - उदाहरण के लिए:

/sdcard/YourCustomDirectory/ExampleFile.mp3

+0

मुझे लगता है कि इनर बच्चों को हटाया नहीं गया है .. आपको सभी आंतरिक बच्चों को हटाना होगा। नीचे मेरा जवाब देखें .. – Nepster

+1

दुर्भाग्य से यह एंड्रॉइड 4.4+ पर काम नहीं करेगा। नीचे मेरा जवाब देखें। –

+0

मुझे समझ में नहीं आता कि यह कई लोगों के लिए कैसे काम करता है। एंड्रॉइड स्टूडियो में "हटाया गया" दिखता है। – TheOnlyAnil

79

इसके अलावा, आप अगर अनुमति देने की आवश्यकता आप> 1.6 एसडीके

uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" 

में उपयोग कर रहे हैं

public abstract boolean deleteFile (String name) 

मैं इस रूप में ऊपर सूचीबद्ध क्या आप सही अनुप्रयोग premissions साथ चाहते हो जाएगा विश्वास करते हैं: 0 फ़ाइल

16

एंड्रॉयड प्रसंग निम्न विधि है।

+2

के लिए काम करने वाला एक ही कोड यह सही उत्तर होना चाहिए। 'संदर्भ।deleteFile (फ़ाइल का नाम), ' – Lisandro

6
public static boolean deleteDirectory(File path) { 
    // TODO Auto-generated method stub 
    if(path.exists()) { 
     File[] files = path.listFiles(); 
     for(int i=0; i<files.length; i++) { 
      if(files[i].isDirectory()) { 
       deleteDirectory(files[i]); 
      } 
      else { 
       files[i].delete(); 
      } 
     } 
    } 
    return(path.delete()); 
} 

इस संहिता आप मदद होगा .. और एंड्रॉयड प्रकट में अनुमति संशोधन करना प्राप्त करने के लिए ..

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

फ़ाइलें [] अशक्त हो सकता है: ' file.exists: सच file.isDirectory: सच file.canRead: झूठी file.canWrite: FALSE' – Andreyua

11

रिकर्सिवली फ़ाइल के सभी बच्चों को हटाना ...

public static void DeleteRecursive(File fileOrDirectory) 
    { 
     if (fileOrDirectory.isDirectory()) 
     { 
      for (File child : fileOrDirectory.listFiles()) 
      { 
       DeleteRecursive(child); 
      } 
     } 

     fileOrDirectory.delete(); 
    } 
33

एंड्रॉइड 4.4 +

ऐप्स की अनुमति नहीं हैलिखने(हटाने के लिए, संशोधित ...)उनके पैकेज विशेष निर्देशिका के लिए छोड़कर भंडारण के लिए बाहरी करने के लिए।

के रूप में Android दस्तावेज़ कहता है:

"Apps माध्यमिक बाहरी संग्रहण में उपकरणों, लिखने के लिए अपने पैकेज-विशिष्ट निर्देशिका में छोड़कर के रूप में संश्लेषित अनुमतियों द्वारा अनुमति अनुमति नहीं दी जानी चाहिए।"

हालांकि बुरा वैकल्पिक हल मौजूद (नीचे दिए गए कोड को देखें)। सैमसंग गैलेक्सी एस 4 पर परीक्षण किया गया, लेकिन यह फिक्स सभी उपकरणों पर काम नहीं करता है।इसके अलावा मैं पर भरोसा नहीं करता हूं यह कार्यवाही भविष्य में संस्करणों में उपलब्ध है।

great article explaining (4.4+) external storage permissions change है।

आप more about workaround here पढ़ सकते हैं। वर्कअराउंड स्रोत कोड this site से है।

public class MediaFileFunctions 
{ 
    @TargetApi(Build.VERSION_CODES.HONEYCOMB) 
    public static boolean deleteViaContentProvider(Context context, String fullname) 
    { 
     Uri uri=getFileUri(context,fullname); 

     if (uri==null) 
     { 
     return false; 
     } 

     try 
     { 
     ContentResolver resolver=context.getContentResolver(); 

     // change type to image, otherwise nothing will be deleted 
     ContentValues contentValues = new ContentValues(); 
     int media_type = 1; 
     contentValues.put("media_type", media_type); 
     resolver.update(uri, contentValues, null, null); 

     return resolver.delete(uri, null, null) > 0; 
     } 
     catch (Throwable e) 
     { 
     return false; 
     } 
    } 

    @TargetApi(Build.VERSION_CODES.HONEYCOMB) 
    private static Uri getFileUri(Context context, String fullname) 
    { 
     // Note: check outside this class whether the OS version is >= 11 
     Uri uri = null; 
     Cursor cursor = null; 
     ContentResolver contentResolver = null; 

     try 
     { 
     contentResolver=context.getContentResolver(); 
     if (contentResolver == null) 
      return null; 

     uri=MediaStore.Files.getContentUri("external"); 
     String[] projection = new String[2]; 
     projection[0] = "_id"; 
     projection[1] = "_data"; 
     String selection = "_data = ? "; // this avoids SQL injection 
     String[] selectionParams = new String[1]; 
     selectionParams[0] = fullname; 
     String sortOrder = "_id"; 
     cursor=contentResolver.query(uri, projection, selection, selectionParams, sortOrder); 

     if (cursor!=null) 
     { 
      try 
      { 
       if (cursor.getCount() > 0) // file present! 
       { 
        cursor.moveToFirst(); 
        int dataColumn=cursor.getColumnIndex("_data"); 
        String s = cursor.getString(dataColumn); 
        if (!s.equals(fullname)) 
        return null; 
        int idColumn = cursor.getColumnIndex("_id"); 
        long id = cursor.getLong(idColumn); 
        uri= MediaStore.Files.getContentUri("external",id); 
       } 
       else // file isn't in the media database! 
       { 
        ContentValues contentValues=new ContentValues(); 
        contentValues.put("_data",fullname); 
        uri = MediaStore.Files.getContentUri("external"); 
        uri = contentResolver.insert(uri,contentValues); 
       } 
      } 
      catch (Throwable e) 
      { 
       uri = null; 
      } 
      finally 
      { 
       cursor.close(); 
      } 
     } 
     } 
     catch (Throwable e) 
     { 
     uri=null; 
     } 
     return uri; 
    } 
} 
+1

note3 के लिए काम नहीं है। मुझे त्रुटि मिली "MediaProvider: /mnt/extSdCard/test.zip को हटा नहीं सका" – iscariot

+2

मेरे पास 4.4.4 पर एक अनियंत्रित मोटो एक्स है और/sdcard/mydirectory – Rob

+0

पर कोई समस्या नहीं है "दुर्भाग्य से यह अब और काम नहीं करता है किटकैट के नए संस्करण, यह सब बंद कर दिया गया है। " लेखक से उद्धृत "ghisler (लेखक)" – HendraWD

7

यह मेरे लिए काम करता है: (गैलरी से छवि हटाएं)

File file = new File(photoPath); 
file.delete(); 

context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(photoPath)))); 
+2

context.sendBroadcast() क्या करता है? – Megaetron

+0

असल में, यह इस इरादे से मेल खाने वाले सभी रिसीवरों को इरादा (ऑपरेशन करने के लिए) भेजता/प्रसारित करता है। [लिंक] (http://developer.android.com/reference/android/content/Context.html) – Jiyeh

0

यह मेरे लिए काम किया।

String myFile = "/Name Folder/File.jpg"; 

String my_Path = Environment.getExternalStorageDirectory()+myFile; 

File f = new File(my_Path); 
Boolean deleted = f.delete(); 
1

क्षमा करें: साइट सत्यापन के कारण मेरे कोड में कोई गलती है।

String myFile = "/Name Folder/File.jpg"; 

String myPath = Environment.getExternalStorageDirectory()+myFile; 

File f = new File(myPath); 
Boolean deleted = f.delete(); 

मुझे लगता है कि स्पष्ट है ... पहले तुम चाहिए आपकी फ़ाइल स्थान पता करने के लिए। दूसरा, Environment.getExternalStorageDirectory() एक तरीका है जो आपकी ऐप निर्देशिका प्राप्त करता है। अंततः कक्षा फ़ाइल जो आपकी फ़ाइल को संभालती है ...

1

मुझे 4.4 पर चल रहे एप्लिकेशन के साथ एक ही समस्या थी। मैंने जो किया वह एक हैक की तरह था।

मैंने फ़ाइलों का नाम बदल दिया और उन्हें मेरे आवेदन में अनदेखा कर दिया।

यानी।

File sdcard = Environment.getExternalStorageDirectory(); 
       File from = new File(sdcard,"/ecatAgent/"+fileV); 
       File to = new File(sdcard,"/ecatAgent/"+"Delete"); 
       from.renameTo(to); 
4

इसे आजमाएं।

File file = new File(FilePath); 
FileUtils.deleteDirectory(file); 
अपाचे कॉमन्स से

0
private boolean deleteFromExternalStorage(File file) { 
         String fileName = "/Music/"; 
         String myPath= Environment.getExternalStorageDirectory().getAbsolutePath() + fileName; 

         file = new File(myPath); 
         System.out.println("fullPath - " + myPath); 
          if (file.exists() && file.canRead()) { 
           System.out.println(" Test - "); 
           file.delete(); 
           return false; // File exists 
          } 
          System.out.println(" Test2 - "); 
          return true; // File not exists 
        } 
+0

आपको अपने कोड के बारे में कुछ स्पष्टीकरण प्रदान करने की आवश्यकता है! :) –

+0

फ़ाइल को पढ़ें एक्स्ट्रास्ट स्टोरेज डायरेक्टरी + "/ संगीत /" जोड़ें यह मेरे पथ है -> mypath =/storage/sdcard/music /। फ़ाइल मौजूद है या कोई मौजूद नहीं है और इसे पढ़ें। मौजूद होने पर हटाएं। यह निर्देशिका में सभी सूची संगीत हटा दिया गया है संगीत –

+0

उत्तर में वही जोड़ें! :) –

0
File filedel = new File("/storage/sdcard0/Baahubali.mp3"); 
boolean deleted1 = filedel.delete(); 

या, इस प्रयास करें:

String del="/storage/sdcard0/Baahubali.mp3"; 
File filedel2 = new File(del); 
boolean deleted1 = filedel2.delete(); 
0

आप इस प्रकार एक फ़ाइल को नष्ट कर सकते हैं:

File file = new File("your sdcard path is here which you want to delete"); 
file.delete(); 
if (file.exists()){ 
    file.getCanonicalFile().delete(); 
    if (file.exists()){ 
    deleteFile(file.getName()); 
    } 
} 
संबंधित मुद्दे