保存的图像文件在sdcard中以0kb存储,文件夹甚至在库中不可见

时间:2013-07-28 13:37:57

标签: android

在主要活动中,我有一个按钮takeApic,它将启动访问摄像头的意图,然后onActivityResult我已经将位图传递给其他活动(ShowActivity)

               @Override
              protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_PIC_REQUEST) {
        // 2
        Bitmap show_img = (Bitmap) data.getExtras().get("data");
        Intent mIntent = new Intent(MainActivity.this,ShowActivity.class);
        mIntent.putExtra("BitmapImage", show_img);
        startActivity(mIntent);

    }

然后ShowActivity在imageview中设置拍摄的图像(我通过意图接收)

        Intent mIntent = getIntent();
    /** retrieve the string extra passed */
    Bitmap our_img = (Bitmap) mIntent.getParcelableExtra("BitmapImage");
    mImage.setImageBitmap(our_img);

当ShowActivity上按下Save按钮时,应保存图像,这是我的代码

                 FileOutputStream output;


        File filepath = Environment.getExternalStorageDirectory();

        // Create a new folder AndroidBegin in SD Card
        File dir = new File(filepath.getAbsolutePath() + "/Hello/");

        dir.mkdirs();

        // Create a name for the saved image
        File file = new File(dir, pic_name+".png");

        // Notify the user on successful save
        Toast.makeText(this, "Image Saved to SD Card", Toast.LENGTH_SHORT).show();
        try {

            // Image starts saving
            output = new FileOutputStream(file);

            our_img.compress(Bitmap.CompressFormat.PNG, 100, output);

            output.flush();
            output.close();
        }
        // Catch exceptions
        catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        //  to see saved images in the gallery view.

        sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
                Uri.parse("file://"
                        + Environment.getExternalStorageDirectory())));

*程序可以拍照 *也创建一个文件夹和图像文件 但 创建的文件是0KB 并且文件夹在图库

中不可见

p.s我已经在清单中授予了写入外部存储并访问摄像头的权限

如果您能为我提供解决方案,我将非常感谢您!

1 个答案:

答案 0 :(得分:0)

好像你还没有将流写入输出文件。试试这个

byte[] buffer = new byte[1024];

int remainingData;

while ((remainingData = yourBitmapInputStream.read(buffer)) > 0) 

  {

    output.write(buffer, 0, remainingData);

  }

用于将流写入位图文件。