我不知道如果这个问题被标记为没有用或类似的东西,但我真的好奇它,所以认为这将是最好的问题。
如果没有root权限,是否无法从非root电话的内存中删除文件?我尝试了deleteFile(uri.toString()),其中uri是文件路径,在它下面添加了一个日志Log.e(“msg”,“deleted”),它也显示在logcat中,但问题是所有这些之后文件仍然存在。怎么办?
更新: 这是我的代码:
public class MainActivity extends Activity {
ListView list ;
ListAdapter listAdapter;
ArrayList<String> listTest;
String name;
Uri uri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView)findViewById(R.id.list);
listTest = new ArrayList<String>( );
Scanner(Environment.getRootDirectory()+"/");
if(listTest.size() != 0)
{
listAdapter=new ListAdapter(getApplicationContext(),listTest);
list.setAdapter(listAdapter);
list.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
name = (String) ((TextView) view).getText();
view.setSelected(true);
uri = Uri.parse(listTest.get(position).toString());
File fl = new File(uri.toString());
deleteFile(uri.toString());
Log.e("msg", "deleted");
Toast.makeText(getApplicationContext(), name+".apk"+" is deleted from internal storage", Toast.LENGTH_SHORT).show();
/*if(fl.exists())
{
fl.delete();
Log.e("msg", "deleted");
Toast.makeText(getApplicationContext(), name+".apk"+" is deleted from internal storage", Toast.LENGTH_SHORT).show();
}*/
list.setSelection(position);
}});
}
}
private void Scanner(String path) {
// TODO Auto-generated method stub
{
try
{
File fl = new File(path);
File[] listOfFiles = fl.listFiles();
for (File listOfFile : listOfFiles)
{
String s = listOfFile.getName();
if(s.endsWith(".apk"))
{
// store file name in listSoundNames
int pos = s.lastIndexOf(".");
if (pos > 0)
{
name = s.substring(0, pos);
}
listTest.add(name);
}
/////////////////////////////////
File f = new File(path+s+"/");
if (f.exists() && f.isDirectory()) {
Scanner(path+s+"/");
}
////////////////////////////////
}
}
catch (Exception e) { }
}
}
}
答案 0 :(得分:1)
如果没有根,那是不可能的。您只能删除/ data / data / your_package_name中的文件。