捕获图像无法存储在SD卡中?

时间:2014-07-12 05:17:03

标签: android button android-emulator

当我点击按钮时相机捕获图像但无法保存在SD卡中。请检查此代码。如果我有任何错误

  

`           ImgPhoto =(ImageView)findViewById(R.id.imageView1);

    BtnSelectImage = (Button) findViewById(R.id.button1);
    BtnSelectImage.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            // TODO Auto-generated method stub
            try {
                   Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                   startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), "Couldn't load photo", Toast.LENGTH_LONG).show();
            }
        }
    });

}
   private void SaveIamge(Bitmap finalBitmap) {

    String root = Environment.getExternalStorageDirectory().toString();
    File file1=new File(Environment.getExternalStorageDirectory()+File.separator+"image.jpeg");
    File myDir = new File(root + "/sdcard/");    
    myDir.mkdirs();
    Random generator = new Random();
    int n = 10000;
    n = generator.nextInt(n);
    String fname = "Image-"+ n +".jpeg";
    File file = new File (myDir, fname);
    if (file.exists ()) file.delete (); 
    try {
           FileOutputStream out = new FileOutputStream(file);
           finalBitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
           out.flush();
           out.close();

    } catch (Exception e) {
           e.printStackTrace();
    }
    sendBroadcast(new Intent(
            Intent.ACTION_MEDIA_MOUNTED,
                        Uri.parse("file://" + Environment.getExternalStorageDirectory())));
}
}`

2 个答案:

答案 0 :(得分:1)

按下后退按钮时,将调用当前活动的onBackPressed()方法。默认行为是完成活动。您不应该做任何事情来启用此行为。

答案 1 :(得分:1)

  • 转到AVD经理。
  • 编辑您的设备。
  • 使用动态硬件控件选择皮肤。
  • 取消选中硬件键盘(或键盘中的Escape =后退按钮)