简体   繁体   中英

ThumbnailUtils.createVideoThumbnail

Can someone please tell me if what I'm doing is correct?

File directoryToStore;
directoryToStore = getBaseContext().getExternalFilesDir("MyFiles");
Bitmap b = ThumbnailUtils.createVideoThumbnail(directoryToStore + "/" + SavedVideoName, 3);
File newFile = new File(directoryToStore, SavedVideoName.replace(".mp4", ".jpg"));
FileOutputStream outputStream = null;
try {
    outputStream = new FileOutputStream(newFile);
} catch (FileNotFoundException e) {
    e.printStackTrace();
}

b.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);

try {
    outputStream.close();
} catch (IOException e) {
    e.printStackTrace();
}

I'm trying create a thumbnail from a video, but for some the FileOutputStream returns null .

I have checked the path of File newFile = new File(directoryToStore, SavedVideoName.replace(".mp4", ".jpg")); and it returns the correct path.

The video exists at the location I have given and I have permissions. I can't understand why it is gives me a null pointer?

According to this post , new FileOutputStream() will try to create a new file if it doesn't exist already. From the docs :

If the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason then a FileNotFoundException is thrown.

When you are debugging (at least in Android Studio), if you add a breakpoint and hover over newFile , it shows the file path. However, it doesn't show any details about the file, because the file doesn't (shouldn't) exist yet. You could try newFile.createNewFile() as suggested in the linked post, to confirm you are able to write the file first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM