保存图像视图作为SD卡JPEG图像 [英] Save image view as a jpeg image in sdcard

查看:126
本文介绍了保存图像视图作为SD卡JPEG图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class MapDemoActivity extends Activity {

Button capture;

    ImageView image;
    int cameracode=100;
    Bitmap bm;
    Boolean result;
    FileOutputStream fos;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        capture=(Button)findViewById(R.id.capture);
        capture.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                image=(ImageView)findViewById(R.id.image);
                Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(i, cameracode);
                image.setDrawingCacheEnabled(true);
                bm = image.getDrawingCache();

                try {
                    fos = new FileOutputStream("sdcard/image.jpg");
                    result=bm.compress(CompressFormat.JPEG, 75, fos);

                    fos.flush();
                    fos.close();
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block

                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block

                    e.printStackTrace();
                }


            }
        });

    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub

                    if(requestCode==100)
                    {

                        bm=(Bitmap) data.getExtras().get("data");
                        image.setImageBitmap(bm);


                    }

        super.onActivityResult(requestCode, resultCode, data);
    }
}

在此PROG我正在捕获来自相机与影像放大器;显示到图像视图,然后我试图
其转换为JPEG在SD卡存储...
但是,当我pressed 捕获映像按钮PROG得到强制关闭..
&安培;在SD卡创建的空JPEG文件...我希望存储JPEG文件到SD卡

In this prog i am capturing image from camera & display to Image view then i am trying to convert it to Jpeg to store in sdcard... But when i pressed capture image Button prog get force close.. & empty jpeg file created in sdcard... i want to store jpeg file to sdcard

推荐答案

startActivityForResult 是一个异步调用,因此立即执行这一呼吁之后,你有正确的一切,前等待捕获完成。而不是有这样的说法,你应该将节省code到的onActivityResult

startActivityForResult is an asynchronous call, therefore everything that you have right after that call is executed immediately, before waiting for the capture to complete. Instead of having it that way, you should move the saving code into the onActivityResult.

此外,你应该不难code SD卡而是使用 Environment.getExternalStorageDirectory()来代替。

Additionally, you should not hardcode sdcard but instead use Environment.getExternalStorageDirectory() instead.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    capture=(Button)findViewById(R.id.capture);
    capture.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            image=(ImageView)findViewById(R.id.image);
            Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(i, cameracode);
        }
    });

}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub

    if(requestCode==100)
    {

        bm=(Bitmap) data.getExtras().get("data");
        image.setImageBitmap(bm);

        if(bm == null) {
            return; //probably user cancelled;
        }

        try {
            fos = new FileOutputStream(new File(Environment.getExternalStorageDirectory(), "image.jpg"));
            result=bm.compress(CompressFormat.JPEG, 75, fos);

            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block

            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block

            e.printStackTrace();
        }

    }
    super.onActivityResult(requestCode, resultCode, data);
}

最后,你可能要检查您实际上是试图挽救它,因为用户可能已经取消了图像采集前有一个图像。

Finally, you may want to check that you actually got an image before trying to save it, as the user may have cancelled image capture.

除了它,我建议您发布完整的logcat的堆栈跟踪。也许还有别的险恶在那里。

Apart from it, I suggest that you post your full logcat stack trace. Maybe there's something else sinister in there.

这篇关于保存图像视图作为SD卡JPEG图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆