ThumbnailUtils.createVideoThumbnail

时间:2018-01-31 18:09:43

标签: java android nullpointerexception

有人可以告诉我,我在做什么是正确的吗?

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();
}

我正在尝试从视频中创建缩略图,但有些FileOutputStream会返回null

我已检查File newFile = new File(directoryToStore, SavedVideoName.replace(".mp4", ".jpg"));的路径,并返回正确的路径。

视频存在于我提供的位置,我有权限。我无法理解为什么它会给我一个空指针?

1 个答案:

答案 0 :(得分:0)

根据此postnew FileOutputStream()会尝试创建一个新文件,如果它尚未存在的话。来自docs

  

如果文件存在但是是目录而不是常规文件,不存在但无法创建,或者由于任何其他原因无法打开,则抛出FileNotFoundException。

在进行调试时(至少在Android Studio中),如果添加断点并将鼠标悬停在newFile上,则会显示文件路径。但是,它没有显示有关该文件的任何详细信息,因为该文件尚未存在(不应该存在)。您可以按照链接帖子中的建议尝试newFile.createNewFile(),以确认您可以先写入文件。