【问题标题】:ThumbnailUtils.createVideoThumbnailThumbnailUtils.createVideoThumbnail
【发布时间】:2018-07-10 22:52:40
【问题描述】:

谁能告诉我我的做法是否正确?

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")); 的路径,它返回了正确的路径。

视频存在于我提供的位置并且我有权限。我不明白为什么它给了我一个空指针?

【问题讨论】:

  • 我认为你需要创建文件。

标签: java android nullpointerexception


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-13
    • 1970-01-01
    • 1970-01-01
    • 2014-02-25
    相关资源
    最近更新 更多