如何从移动的文件中检测uris

时间:2017-06-01 15:55:58

标签: java android path uri

任何人都知道如何在Java / Android中以编程方式检测移动文件中的uris?当我启动应用程序时,我会检测手机中的所有图像(带有uris,路径......),如果我用媒体移动图像经理,下次我启动应用程序时,我可以再次使用新路径获得整个uris。但是,如果我以编程方式移动这些图像(复制图像并从原始路径删除图像),下次启动应用程序时,uris将是上一个路径而不是新路径。我试图修复删除应用缓存,但无法正常工作。谁知道会发生什么?

我试图用两个函数移动文件,我遇到同样的问题:

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());
        }

    }

}

0 个答案:

没有答案