【问题标题】:Saving a bitmap and naming it as an inputted string保存位图并将其命名为输入字符串
【发布时间】:2016-03-06 03:35:39
【问题描述】:

我在保存图像时遇到了文件名问题。

String fileName = currentDate + currentTime + personName;

private void saveImage()
{
    View u = mView;
    ScrollView z = (ScrollView) findViewById(R.id.scroll_view);
    int totalHeight = z.getChildAt(0).getHeight();
    int totalWidth = z.getChildAt(0).getWidth();

    Bitmap b = getBitmapFromView(u, totalHeight, totalWidth);


    try {
        FileOutputStream fos = new FileOutputStream(fileName);
        b.compress(Bitmap.CompressFormat.JPEG, 100, fos);
        fos.flush();
        fos.close();
        MediaStore.Images.Media.insertImage(getContentResolver(), b, "Screen", "screen");
    }catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

输出文件时,文件名设置为一系列数字(例如:“1457223074488”)而不是字符串。 如果有人可能有解决方案,请告诉我。

【问题讨论】:

  • 什么是currentDate和currentTime?这些时间可能以毫秒为单位吗?
  • currentDate 是日期格式 mm/dd/yy,currentTime 是时间格式 HHMM

标签: android filenames


【解决方案1】:

您调用了错误版本的 MediaStore.Images.Media.insertImage。您现在调用的只是将位图作为输入进行保存。它对您创建的文件一无所知。 javadoc中没有定义它将用于位图的文件名。

相反,您可能希望使用您创建的名称文件来调用它:

MediaStore.Images.Media.insertImage(getContentResolver(), fileName, "Screen", "screen");

此外,您应该在 javadoc 中注意 MediaStore 将为图像创建一个缩略图,它为其创建的文件的名称可以是它想要的任何名称。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-13
    • 1970-01-01
    • 2015-02-08
    • 1970-01-01
    相关资源
    最近更新 更多