【问题标题】:saving bitmap from arraylist to SD card - saved file unreadable将位图从 arraylist 保存到 SD 卡 - 保存的文件不可读
【发布时间】:2014-10-29 23:34:04
【问题描述】:

我的情况如下:我正在将数组列表中的多个位图保存到设备 SD 卡中的特定文件夹中(成功),但是,保存的文件 - 单击时会提示来自手机的消息,说明: “无法找到执行此操作的应用程序。”该文件的文件大小与保存的位图图像的大小成正比,所以我有点困惑,因为设备打开图像文件没有问题,但无法打开(或识别)这些为媒体文件。

问题:什么会导致保存的图像文件(假设我已正确保存)在设备中表现出这种类型的行为,我应该如何解决这个问题?

额外:文件的缩略图是系统提供的两张纸叠放的缩略图。数组列表正在从一个活动传递到提供所提供方法的当前活动。

这是调用将文件保存到指定文件夹/filesdestination的方法:

private void saveImages(){
// to retrieve bitmaps
    ArrayList<Bitmap> images = getIntent().getParcelableArrayListExtra("images key");
//to retrieve bitmaps and save in specific order, while also naming them in that order
    int loopVal = 0;
    int postVal = 9;
    while ( loopVal < 9) {
        Bitmap Image = images.get(loopVal);
    try {

        String filedestination = new String(Environment.getExternalStorageDirectory()  + "/filedestination"); 
        String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmm").format(new Date());
        File file = new File(filedestination, postVal + ".post_order" +  ".jpg" + timeStamp);
        File picfile = file;
         FileOutputStream fos = new FileOutputStream(picfile);
            Image.compress(Bitmap.CompressFormat.PNG, 100, fos);      
            fos.close();



    } catch (Throwable e) {

        e.printStackTrace();

    }
    postVal--;
    loopVal++;

    }


}

任何见解将不胜感激,

-卢卡斯

【问题讨论】:

    标签: android arrays arraylist bitmap save


    【解决方案1】:

    我认为它无法读取文件类型,因为时间戳在文件扩展名 jpg 之后,并且您还将其压缩为 png,因此您可能想要更改或类似这样的内容

    File file = new File(filedestination, postVal + timeStamp +".post_order" +  ".png");
    

    【讨论】:

    • 这成功了!我现在知道 .jpg 的 .png 的位置对于跨应用程序的可读性有多么重要,谢谢!
    【解决方案2】:

    您似乎正在将 .jpg 文件压缩为 PNG。这可能会使图像阅读器应用程序行为不端。

    要么更改Image.compress(Bitmap.CompressFormat.PNG, 100, fos);

    Image.compress(Bitmap.CompressFormat.JPEG, 100, fos);

    改变

    File file = new File(filedestination, postVal + ".post_order" + ".jpg" + timeStamp);

    File file = new File(filedestination, postVal + ".post_order" + ".png" + timeStamp);

    【讨论】:

    • 我试过了,但同样的情况发生了:/
    • 是的,我错过了时间戳在扩展之后的事实,就像另一个答案指出的那样。 :)
    猜你喜欢
    • 2012-10-21
    • 2012-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多