2016-07-22 62 views
0

我想下面的代碼無法刪除的Android棒棒糖文件和更高

String Data = cursor.getString(cursor 
         .getColumnIndex(MediaStore.Audio.Media.DATA)); 

    File file = new File(Data); 
    if (file != null && file.exists()) { 
          // delete it 
          Toast.makeText(FileEditorDialog.this, "deleted not null", 
            Toast.LENGTH_LONG).show(); 
         } 


         boolean deleted = file.delete(); 


         if (deleted) { 
          Toast.makeText(FileEditorDialog.this, 
            "Successfully Deleted", Toast.LENGTH_LONG).show(); 

          sendBroadcast(new Intent(
            Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, 
            Uri.parse("file://" 
              + Environment.getExternalStorageDirectory()))); 
          finish(); 
         } 

這個代碼在安卓果凍豆,但不是在Android的棒棒糖,我們如何能在Android的棒棒糖刪除文件

+1

你有權限WRITE_EXTERNAL_STORAGE嗎? –

+0

請提供給我們一個清單文件 –

+0

錯誤是什麼? – Henry

回答

1

嘗試德克斯特庫檢查權限 - WRITE_EXTERNAL_STORAGE

確保您在AndroidManifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
有權限

這是可選的,但名爲Dexter的庫更容易處理即時權限。添加這種依賴到的build.gradle(APP)

compile 'com.karumi:dexter:2.2.2' 

創建應用程序類:

public class MyApplication extends Application { 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     Dexter.initialize(getApplicationContext()); 
    } 

} 

,並設置它在你的AndroidManifest.xml

<application 
     android:name=".MyApplication" 

使用這段代碼問用戶權限。

   Dexter.checkPermission(new PermissionListener() { 
       @Override public void onPermissionGranted(PermissionGrantedResponse response) { 

        deleteFile();//call the method to delete file 

       } 
       @Override public void onPermissionDenied(PermissionDeniedResponse response) { 
        Toast.makeText(getApplicationContext(), "Permission denied", Toast.LENGTH_LONG).show(); 
       } 
       @Override public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token) { 
        token.continuePermissionRequest(); 
       } 
      }, Manifest.permission.WRITE_EXTERNAL_STORAGE); 
1

您必須從android 6.0中的用戶獲得Runitme權限,只聲明他們的清單是不夠的。

在我之前的回答中看here