gpt4 book ai didi

Android MediaStore 不会设置文件名

转载 作者:行者123 更新时间:2023-11-29 14:05:00 25 4
gpt4 key购买 nike

我正在尝试让 Android 的 MediaStore 将具有特定文件名的图像写入 SD 卡。它确实写入文件,但不使用通过

传递的标题参数
MediaStore.Images.Media.insertImage(cr, source, title, description)

这是我的相关代码:

PictureCallback myPictureCallback_JPG = new PictureCallback(){

public void onPictureTaken(byte[] arg0, Camera arg1) {
Bitmap bitmapPicture = BitmapFactory.decodeByteArray(arg0, 0, arg0.length);

int year = Calendar.getInstance().getTime().getYear();
int month = Calendar.getInstance().getTime().getMonth();
int day = Calendar.getInstance().getTime().getDay();

int hour = Calendar.getInstance().getTime().getHours();
int minute = Calendar.getInstance().getTime().getMinutes();
int seconds = Calendar.getInstance().getTime().getSeconds();

String imgName = "IMG_" + Integer.toString(year) + Integer.toString(month) +Integer.toString(day) + "_" + Integer.toString(hour) + Integer.toString(minute)
+ Integer.toString(seconds) + ".jpg";

MediaStore.Images.Media.insertImage(getContentResolver(), bitmapPicture, imgName, imgName);

camera.startPreview();
inPreview=true;
}};

它成功存储了图片,但文件名似乎是自 Unix 纪元以来的时间(即 13168297...16.jpg)

最佳答案

我没有让 MediaStore 工作,但我确实使用了 File 类和 FileOutputStream 来完成工作:

        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmapPicture.compress(Bitmap.CompressFormat.JPEG, 100, bytes);

File imageDirectory = new File(Environment.getExternalStorageDirectory() + File.separator + "DropPic 64");

imageDirectory.mkdirs();

File f = new File(Environment.getExternalStorageDirectory()
+ File.separator + "DropPic 64" + File.separator + currentPicture.fileName);
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(arg0);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

关于Android MediaStore 不会设置文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7536576/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com