android 删除字符串 /storage_root/storage/emulated/0/PlayMotoRunLuPai/config/didian.png 的t/storage前面的 代码举例
时间: 2024-04-20 08:21:52 浏览: 121
在Android中删除字符串中指定位置之前的代码可以使用`substring()`方法。以下是一个示例代码:
```java
String filePath = "/storage_root/storage/emulated/0/PlayMotoRunLuPai/config/didian.png";
String newFilePath = filePath.substring(filePath.indexOf("/storage"));
File file = new File(newFilePath);
if (file.exists()) {
boolean deleted = file.delete();
if (deleted) {
// 文件删除成功的处理逻辑
} else {
// 文件删除失败的处理逻辑
}
} else {
// 文件不存在的处理逻辑
}
```
相关问题:
1. `substring()`方法的作用是什么?
2. 如何判断文件是否存在?
3. 如何删除文件?
相关问题
Compile error: In file included from /storage/emulated/0/Android/data/com.cjkj.clanide/files/CJ_IDE/CProject/我的/src/Main.c:1: /storage/emulated/0/Android/data/com.cjkj.clanide/files/CJ_IDE/CProject/我的/src/game.h:19:5: error: expected identifier before 'int' int up =2; ^~~ In file included from /storage/emulated/0/Android/data/com.cjkj.clanide/files/CJ_IDE/CProject/我的/src/game.c:1: /storage/emulated/0/Android/data/com.cjkj.clanide/files/CJ_IDE/CProject/我的/src/game.h:19:5: error: expected identifier before 'int' int up =2; ^~~ /storage/emulated/0/Android/data/com.cjkj.clanide/files/CJ_IDE/CProject/我的/src/game.h:19:5: error: expected identifier before 'int' int up =2; ^~~ In file included from /storage/emulated/0/Android/data/com.cjkj.clanide/files/CJ_IDE/CProject/我的/src/menu.c:1: /storage/emulated/0/Android/data/com.cjkj.clanide/files/CJ_IDE/CProject/我的/src/game.h:19:5: error: expected identifier before 'int' int up =2; ^~~ In file included from /storage/emulated/0/Android/data/com.cjkj.clanide/files/CJ_IDE/CProject/我的/src/move.c:1: /storage/emulated/0/Android/data/com.cjkj.clanide/files/CJ_IDE/CProject/我的/src/game.h:19:5: error: expected identifier before 'int' int up =2; ^~~
这个错误是由于在game.h头文件中,int变量的声明位置不正确导致的。在C语言中,变量的声明应该在函数内部或者全局范围,而不是在头文件中。请将game.h头文件中的int变量声明移动到函数内部或者全局范围即可解决这个编译错误。以下是修复后的示例代码:
```c
// game.h
#ifndef GAME_H
#define GAME_H
extern int up; // 在头文件中声明全局变量,用extern关键字
#endif
// game.c
#include "game.h"
int up = 2; // 在.c文件中定义全局变量
// 其他文件
#include "game.h"
// 使用全局变量up
```
请根据你的实际需求将代码中的int变量声明放置在合适的位置。
Compile error: /storage/emulated/0/Android/data/com.cjkj.clanide/files/CJ_IDE/CProject/我的/src/game.c: In function 'play': /storage/emulated/0/Android/data/com.cjkj.clanide/files/CJ_IDE/CProject/我的/src/game.c:31:41: warning: initialization of 'void (*)(int, int *, int *)' from incompatible pointer type 'void (*)(int (*)[10], int *, int *)' [-Wincompatible-pointer-types] void(*prr[4])(int , int*, int*) = { up,down,left,right}; ^~ /storage/emulated/0/Android/data/com.cjkj.clanide/files/CJ_IDE/CProject/我的/src/game.c:31:41: note: (near initialization for 'prr[0]') /storage/emulated/0/Android/data/com.cjkj.clanide/files/CJ_IDE/CProject/我的/src/game.c:31:44: warning: initialization of 'void (*)(int, int *, int *)' from incompatible pointer type 'void (*)(int (*)[10], int *, int *)' [-Wincompatible-pointer-types] void(*prr[4])(int , int*, int*) = { up,down,left,right}; ^~~~ /storage/emulated/0/Android/data/com.cjkj.clanide/files/CJ_IDE/CProject/我的/src/game.c:31:44: note: (near initialization for 'prr[1]') /storage/emulated/0/Android/data/com.cjkj.clanide/files/CJ_IDE/CProject/我的/src/game.c:31:49: warning: initialization of 'void (*)(int, int *, int *)' from incompatible pointer type 'void (*)(int (*)[10], int *, int *)' [-Wincompatible-pointer-types] void(*prr[4])(int , int*, int*) = { up,down,left,right}; ^~~~ /storage/emulated/0/Android/data/com.cjkj.clanide/files/CJ_IDE/CProject/我的/src/game.c:31:49: note: (near initialization for 'prr[2]') /storage/emulated/0/Android/data/com.cjkj.clanide/files/CJ_IDE/CProject/我的/src/game.c:31:54: warning: initialization of 'void (*)(int, int *, int *)' from incompatible pointer type 'void (*)(int (*)[10], int *, int *)' [-Wincompatible-pointer-types] void(*prr[4])(int , int*, int*) = { up,down,left,right}; ^~~~~ /storage/emulated/0/Android/data/com.cjkj.clanide/files/CJ_IDE/CProject/我的/src/game.c:31:54: note: (near initialization for 'prr[3]')
这个编译警告是因为你尝试将一个接受 `int (*)[10]` 类型参数的函数指针赋值给接受 `int*` 类型参数的函数指针。这两者是不兼容的类型,因此编译器给出了警告。
要解决这个问题,你需要确保函数指针数组 `prr` 的元素类型与函数 `up`、`down`、`left`、`right` 的类型匹配。如果这些函数的参数类型是 `int (*)[10]`,而不是 `int*`,那么你需要相应地更改函数指针数组的声明。
你可以检查一下函数 `up`、`down`、`left`、`right` 的定义,确保它们的参数类型与你所期望的一致。然后,根据实际情况修改函数指针数组的声明,确保类型匹配。这样就可以解决编译警告了。
阅读全文