0

官方 facebook 应用程序有一个错误,当您尝试以共享意图共享图像时,图像会从 sdcard 中删除。这是您必须使用图像的 uri 将图像传递给 facebook 应用程序的方式:

File myFile= new File(Environment.getExternalStorageDirectory(), "car.jpg");
Uri uri = Uri.fromFile(myFile);
i.putExtra(Intent.EXTRA_STREAM, uri);

然后,假设我从原始myFile对象创建一个副本,并将副本的 uri 传递给 facebook 应用程序,那么,我的原始图像将不会被删除。

我尝试使用此代码,但它不起作用,原始图像文件仍然被删除:

    File myFile= new File(Environment.getExternalStorageDirectory(), "car.jpg");
    File auxFile=myFile.getAbsoluteFile();      
    Uri uri = Uri.fromFile(auxFile);

有人可以告诉我如何制作不重定向到原始文件的文件的精确副本吗?

4

2 回答 2

1

请检查:Android文件拷贝

该文件是逐字节复制的,因此不会保留对旧文件的引用。

于 2011-11-28T16:46:40.940 回答
1

在这里,这应该能够创建文件的副本:

private void CopyFile() {

        InputStream in = null;
        OutputStream out = null;
        try {
          in = new FileInputStream(<file path>);
          out = new FileOutputStream(<output path>);
          copyFile(in, out);
          in.close();
          in = null;
          out.flush();
          out.close();
          out = null;
        } catch(Exception e) {
            Log.e("tag", e.getMessage());
        }       
}
private void copyFile(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[1024];
    int read;
    while((read = in.read(buffer)) != -1){
      out.write(buffer, 0, read);
    }
}
于 2011-11-28T16:49:51.193 回答