Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 复制后如何获取文件路径?_Java_Android - Fatal编程技术网

Java 复制后如何获取文件路径?

Java 复制后如何获取文件路径?,java,android,Java,Android,我正在使用从另一个问题中得到的复制图像的函数: public static boolean copyFile(String from, String to) { try { File sd = Environment.getExternalStorageDirectory(); if (sd.canWrite()) { int end = from.toString().lastIndexOf("/"); S

我正在使用从另一个问题中得到的复制图像的函数:

public static boolean copyFile(String from, String to) {
    try {
        File sd = Environment.getExternalStorageDirectory();
        if (sd.canWrite()) {
            int end = from.toString().lastIndexOf("/");
            String str1 = from.toString().substring(0, end);
            String str2 = from.toString().substring(end+1, from.length());
            File source = new File(str1, str2);
            File destination= new File(to, str2);
            if (source.exists()) {
                FileChannel src = new FileInputStream(source).getChannel();
                FileChannel dst = new FileOutputStream(destination).getChannel();
                dst.transferFrom(src, 0, src.size());
                src.close();
                dst.close();
            }
        }
        return true;
    } catch (Exception e) {
        return false;
    }
}
我是这样用的:

               String filePath = Helper.pathgal(act, data.getData());
               File mydir = new File(Environment.getExternalStorageDirectory() + "/Albummaker/Pics/");

               if (!mydir.exists()) {
                   mydir.mkdirs();
               } else {
                   Log.d("error", "dir. already exists");
               }

               String dest = mydir.getAbsolutePath();
               copyFile(filePath, dest);

               // now I want to use destination file path for using in a bitmap
               bmp = Helper.decodeFile(dest, act);

               // now I'm loading that bitmap in an imageview
               Helper.showpic(act, id1, bmp);
但它并没有显示在ImageView中(如果我使用原始文件路径,它仍然可以工作)。复制功能正在工作,图像文件存在于最终文件路径中,但我认为我应该添加一些文件名,以便在位图中使用“mydir”文件路径

复制功能正在工作,图像文件存在于最终文件路径中,但我认为我应该添加一些文件名,以便在位图中使用“mydir”文件路径

是的,dest变量不包含图像文件。它只是一个文件夹“./Albummaker/Pics/”

在copyFile方法中显示并返回目标文件:

public static File copyFile(String from, String to) {
try {
    File sd = Environment.getExternalStorageDirectory();
    if (sd.canWrite()) {
        int end = from.toString().lastIndexOf("/");
        String str1 = from.toString().substring(0, end);
        String str2 = from.toString().substring(end+1, from.length());
        File source = new File(str1, str2);
        File destination= new File(to, str2);
        if (source.exists()) {
            FileChannel src = new FileInputStream(source).getChannel();
            FileChannel dst = new FileOutputStream(destination).getChannel();
            dst.transferFrom(src, 0, src.size());
            src.close();
            dst.close();
        }
    }
    return destination;
} catch (Exception e) {
    return null;
}
}

您尚未在上述任何代码中指定文件名。字符串dest=mydir.getAbsolutePath()+“myfile.png”;像这样的东西是required@IllegalArgument我的问题就是这样。如何获取此“文件名”?(它不是每次都是同一个文件,而且每次都会改变。)我的回答是,你在上面的评论中的文件名是“myfile.png”,所以你在使用它之前知道它是“myfile.png”,然后呢?如何从目的地获取文件名?(您的返回位置也不正确)。我认为您可以通过File desFile=copyFile(filePath,dest)获取路径;decodeFile(desFile.getAbsolutePath(),act);谢谢你的简单回答:)