我正在调用相机并拍摄图像,我必须一张一张地拍摄 10 张图像并将它们存储在 SD 卡上,然后才能将它们设置为图像视图。请检查我下面的代码,它没有设置为图像视图。
我如何将它存储在 SD 卡上并检索它以设置为图像视图?在存储之前如何命名图像?
在第一个活动中,我调用了相机:
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mContext = this;
init();
}
private void init() {
String extStorageDirectory = Environment.getExternalStorageDirectory()
+ "/testing";
File xmlDirectory = new File(extStorageDirectory);
if (!xmlDirectory.exists())
xmlDirectory.mkdirs();
iv1 = (ImageView) findViewById(R.id.iv1);
}
private OnClickListener onBtnClicked = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case PHOTO:
Intent selectImageIntent = new Intent(first.this,
second.class);
startActivityForResult(selectImageIntent, 1);
break;
}
}
};
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
String result = data.getStringExtra("result");
Log.d("*****************",
"inside onactivityresult in main activity=" + result);
Bitmap bitmap = BitmapFactory.decodeFile(result);
iv1.setImageBitmap(bitmap);
iv1.setScaleType(ScaleType.FIT_XY);
}
}
}
在我的第二个活动中,我正在捕获图像并将其传递给第一个活动:
private void init() {
picturePath = Environment.getExternalStorageDirectory() + "/Camera/"
+ "test.jpg";
System.out.println("thumbnail path~~~~~~" + picturePath);
File file = new File(picturePath);
outputFileUri = Uri.fromFile(file);
}
public void startCamera() {
Intent intent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, IMAGE_CAPTURE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == IMAGE_CAPTURE) {
if (resultCode == RESULT_OK) {
Intent returnIntent = new Intent();
returnIntent.putExtra("result", picturePath);
setResult(RESULT_OK, returnIntent);
finish();
}
}
}