Markus
Markus

Reputation: 1563

Create new File vs createTempFile

I want to create a file and set the URI to my imageView like this:

// 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));

here's the 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)

Since I want to get rid of the random number at the end of my image name. How can I make new File()work?

Upvotes: 1

Views: 2247

Answers (1)

user207421
user207421

Reputation: 310893

How can I make new File()work?

It does work, but it doesn't create files on secondary storage. It just creates a File object in memory. So, n the second case you aren't creating a file at all.

If you want to create an empty file, call f.createNewFile(). You probably don't want to do that: you probably want to put some content into it, via a new FileOutputStream(f).

Upvotes: 1

Related Questions