【发布时间】:2014-09-02 19:08:28
【问题描述】:
当我单击按钮时,相机正在捕获图像但无法保存在 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())));
}
}`
【问题讨论】:
-
调用 finish() 应该会引导您回到之前的活动。你不应该让用户点击返回按钮
-
如何启用或禁用手机返回键?
-
无法禁用后退按钮
-
但是如何启用呢?有什么方法吗?
-
您可以覆盖 OnbackPressed。但是您应该考虑重新设计而不是解决方法
标签: android button android-emulator