0

在主要活动中,我有一个按钮 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 在图像视图中设置拍摄的图像(我已通过意图接收)

        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并且该文件夹在画廊中不可见

ps 我已在清单中授予写入外部存储和访问相机的权限

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

4

1 回答 1

0

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

byte[] buffer = new byte[1024];

int remainingData;

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

  {

    output.write(buffer, 0, remainingData);

  }

用于将流写入位图文件。

于 2013-07-28T13:51:08.330 回答