Android .JPG没有保存到SD卡

时间:2014-09-16 10:42:15

标签: android image

我试图捕获用户签名并将其存储在SD卡下。应用程序运行没有任何错误。但图像文件没有存储到SD卡。它只显示名为sign.jpg的文件夹。 图像丢失。请帮助我解决这个问题。我是新的Android .. 这是我目前的代码

    public Bitmap save(View v) {
        Log.v("log_tag", "Width: " + v.getWidth());
        Log.v("log_tag", "Height: " + v.getHeight());
        if (mBitmap == null) {
            mBitmap = Bitmap.createBitmap(mContent.getWidth(),
                    mContent.getHeight(), Bitmap.Config.RGB_565);
        }
        Canvas canvas = new Canvas(mBitmap);
        try {
            FileOutputStream mFileOutStream = new FileOutputStream(mypath);

            v.draw(canvas);
            mBitmap.compress(Bitmap.CompressFormat.PNG, 90, mFileOutStream);

            mFileOutStream.flush();
            mFileOutStream.close();
            String url = Images.Media.insertImage(getContentResolver(),
                mBitmap, "title", null);
            Log.v("log_tag", "url: " + url);

        } catch (Exception e) {
            Log.v("log_tag", e.toString());
        }
        return mBitmap;
    }

Logcat显示此错误消息

    09-16 17:11:52.517: E/BitmapFactory(1123): Unable to decode stream:   java.io.FileNotFoundException: /storage/sdcard0/Captures/sign1945.jpg: open failed: EISDIR (Is a directory)

1 个答案:

答案 0 :(得分:0)

您的问题出在mypath变量

- 请务必使用Environment.getExternalStorageDirectory().getPath()获取SD卡路径

- 你需要告诉compress方法:嘿我想用这个名字将位图保存到这个目录!像这样:

//saveDirectoryPath = Environment.getExternalStorageDirectory().getPath() + "/folder/";
File file = new File(saveDirectoryPath);
if (!file.exists())
    file.mkdir();  //or file.mkdirs(); depends on your need
File bitmapFile = new File(file, "yourImageFileName.jpg");
FileOutputStream mFileOutStream = new FileOutputStream(bitmapFile);
mBitmap.compress(CompressFormat.PNG, 90, mFileOutStream);