【问题标题】:Android save bmp imageAndroid保存bmp图片
【发布时间】:2013-04-05 13:02:23
【问题描述】:

我正在尝试将捕获的 .bmp 文件保存到 sdcard。 这是负责此的代码片段:

    String root = Environment.getExternalStorageDirectory().toString();
    File mFolder = new File(root + "/mFolder");

    if (!mFolder.exists()) 
    {
        mFolder.mkdir();
    }
    String strF = mFolder.getAbsolutePath();
    File mSubFolder = new File(strF + "/MyApp-SubFolder");

    if (!mSubFolder.exists()) 
    {
        mSubFolder.mkdir();
    }
    String s = "myfile.png";

    File f = new File(mSubFolder.getAbsolutePath(),s);
    String strMyImagePath = f.getAbsolutePath();
    FileOutputStream fos = null;
    try 
    {
        fos = new FileOutputStream(f);
        bmp.compress(Bitmap.CompressFormat.PNG,70, fos);

        fos.flush();
        fos.close();
        Log.d("asd", "yeah!");
    //  MediaStore.Images.Media.insertImage(getContentResolver(), b, "Screen", "screen");
    }catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

但是有一个错误:

图片无效,大小为0kb

我做错了什么?

【问题讨论】:

  • String s = "myfile.png"; ?你说文件是.bmp
  • 试试这个文件 storagePath = new File(Environment.getExternalStorageDirectory() + "/MyApp-SubFolder/");

标签: android bmp


【解决方案1】:

试试这个

private boolean SaveToSD() {

        String imageName = null;

        Bitmap sourceBitmap = ((BitmapDrawable) img.getDrawable()).getBitmap();

        boolean imageSaved = false;

        if (sourceBitmap != null && !sourceBitmap.isRecycled()) {
            File storagePath = new File(
                    Environment.getExternalStorageDirectory() + "/iGridu/");

            if (!storagePath.exists()) {
                storagePath.mkdirs();
            }

            int count = storagePath.list().length;

            Log.i("SaveToSD count", "" + count);

            imageName = String.valueOf(count + 1) + "_igridu";

            FileOutputStream out = null;
            File imageFile = new File(storagePath, String.format("%s.jpg",
                    imageName));
            try {
                out = new FileOutputStream(imageFile);
                imageSaved = sourceBitmap.compress(Bitmap.CompressFormat.JPEG,
                        90, out);
                out.flush();
                out.close();
            } catch (Exception e) {
                Log.e("SaveToSD ", "Unable to write the image to gallery" + e);

            }

            ContentValues values = new ContentValues(3);
            values.put(Images.Media.TITLE, imageName);
            values.put(Images.Media.MIME_TYPE, "image/jpeg");
            values.put("_data", imageFile.getAbsolutePath());

            getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
        }

        return imageSaved;
    }

【讨论】:

  • 可能你的位图在写入文件之前并不完整。
【解决方案2】:

你必须把它放在你的 Manifest.xml 中

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

【讨论】:

  • 我已经添加了它,但它并没有改变任何东西。
猜你喜欢
  • 2015-12-21
  • 1970-01-01
  • 1970-01-01
  • 2021-10-06
  • 2012-03-25
  • 1970-01-01
  • 1970-01-01
  • 2014-10-16
  • 2019-09-18
相关资源
最近更新 更多