我想创建一个文件并将 URI 设置为我的 imageView,如下所示:
// this code will work, but it will generate a random number at the end of my file
f = File.createTempFile(image, JPEG_FILE_SUFFIX, getAlbumDir());
// this will result with an exception at imageView.setImageURI()
f = new File(getAlbumDir(), image + JPEG_FILE_SUFFIX);
imageView.setImageURI(Uri.fromFile(f));
异常(exception)情况:
01-02 15:19:06.575: W/ImageView(26097): Unable to open content: file:///storage/sdcard0/dcim/myapp/20000102_151858_.jpg
01-02 15:19:06.575: W/ImageView(26097): java.io.FileNotFoundException: /storage/sdcard0/dcim/myapp/20000102_151858_.jpg: open failed: ENOENT (No such file or directory)
因为我想去掉图像名称末尾的随机数。我怎样才能使 new File()
工作?
最佳答案
How can I make new File()work?
它确实有效,但它不会在辅助存储上创建文件。它只是在内存中创建一个 File
对象。因此,在第二种情况下,您根本没有创建文件。
如果你想创建一个空文件,调用f.createNewFile()
。您可能不想这样做:您可能想通过 new FileOutputStream(f).
关于java - 创建新文件与 createTempFile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22337398/