Java 如何从移动的文件中检测URI
有人知道如何在Java/Android中以编程方式检测移动文件中的URI吗?当我启动应用程序时,我会检测手机中的所有图像(带有它们的URI,路径…),如果我使用媒体管理器移动图像,下次启动应用程序时,我可以使用新路径再次获取整个URI。但是,如果我下次启动应用程序时以编程方式移动这些图像(复制图像并从原始路径删除图像),URI将是前一个路径,而不是新路径。我正在尝试修复删除应用缓存的问题,但不起作用。有人知道会发生什么吗 我尝试使用两个函数移动文件,但遇到了相同的问题:Java 如何从移动的文件中检测URI,java,android,path,uri,Java,Android,Path,Uri,有人知道如何在Java/Android中以编程方式检测移动文件中的URI吗?当我启动应用程序时,我会检测手机中的所有图像(带有它们的URI,路径…),如果我使用媒体管理器移动图像,下次启动应用程序时,我可以使用新路径再次获取整个URI。但是,如果我下次启动应用程序时以编程方式移动这些图像(复制图像并从原始路径删除图像),URI将是前一个路径,而不是新路径。我正在尝试修复删除应用缓存的问题,但不起作用。有人知道会发生什么吗 我尝试使用两个函数移动文件,但遇到了相同的问题: public stati
public static void moveFile(ArrayList<ImagesData> images) throws IOException {
for (int i = 0; i < images.size(); i++) {
File file_Source = new File(images.get(i).imagePath);
File file_Destination = new File(Environment.getExternalStorageDirectory() + "/PrivateGallery/" + new File(images.get(i).imagePath).getName());
FileChannel source = null;
FileChannel destination = null;
try {
source = new FileInputStream(file_Source).getChannel();
destination = new FileOutputStream(file_Destination).getChannel();
Log.i(TAG, "Source " + source + " destination " + destination);
long count = 0;
long size = source.size();
while((count += destination.transferFrom(source, count, size-count)) < size);
//destination.transferFrom(source, 0, source.size());
file_Source.delete();
}
finally {
if(source != null) {
source.close();
}
if(destination != null) {
destination.close();
}
}
}
}
public void changeToNewPath(ArrayList<ImagesData> images) {
String outputPath = Environment.getExternalStorageDirectory() + "/PrivateGallery/";
//TODO COMPROBAR SI EXISTE YA UN ARCHIVO CON SU NOMBRE
InputStream in = null;
OutputStream out = null;
for(int i = 0; i < images.size(); i++) {
try { //TODO revisar lod el cambio de directorio
//create output directory if it doesn't exist
//File dir = new File(outputPath);
File f = new File(images.get(i).imagePath);
String a = f.getName();
Log.e(TAG, a);
in = new FileInputStream(images.get(i).imagePath);
out = new FileOutputStream(outputPath + new File(images.get(i).imagePath).getName());
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
in.close();
in = null;
// write the output file
out.flush();
out.close();
out = null;
// delete the original file
new File(images.get(i).imagePath).delete();
} catch (FileNotFoundException fnfe1) {
Log.e("tag", fnfe1.getMessage());
} catch (Exception e) {
Log.e("tag", e.getMessage());
}
}
}
publicstaticvoidmovefile(arraylistimages)引发IOException{
对于(int i=0;i
ContentObserver
——我认为一个例子是正确的答案,但我试图实现它,但什么也得不到。因为,当我移动图像时,我必须复制该图像并删除前一个图像,对吗?。所以,我想,uri会不一样,还是会一样?如果是相同的uri,我只需要更改图像路径。对不起,我弄糊涂了,我不知道该怎么做