我有一个从TabActivity
(我知道它已被弃用)调用的活动:
intent2 = new Intent().setClass(this, sin2.class);
spec = tabHost.newTabSpec("second").setIndicator("Second").setContent(intent2);
tabHost.addTab(spec);
这是一项拍摄照片、保存照片并设置缩略图的活动。它在很多设备上都能完美运行,但有时在三星 Galaxy Pocket或Galaxy Wonder(总是 Android 2.x)上,当我启动相机意图时,它永远不会回到,这意味着:onActivityResult()
- 我拍照
- 我保存
- 我不回
onActivityResult()
- 它允许我拍另一张照片
和循环......我再也不会回来了,如果我按finish()
,我会回到我当前的活动。
我在 StackOverflow 上读过关于做类似getParent().startActivityForResult(Intent,RequestCode);
但不起作用的事情。
我是这样拍照的:
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
mMakePhotoUri = Uri.fromFile(photofile);
i.putExtra(MediaStore.EXTRA_OUTPUT, mMakePhotoUri);
startActivityForResult(i, num);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (resultCode==RESULT_OK)
{
if (requestCode == 1)
{
.............. things I do.
}
}
}
提前致谢!!
我的代码:
onCreate(photo
是一个位图):
scatta.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
photo = null;
if(foto1.getDrawable()==null)
foto(1);
else if(foto2.getDrawable()==null)
foto(2);
else if(foto3.getDrawable()==null)
foto(3);
else
Toast.makeText(sinistri2.this, "Cancella una foto per poter scattare un'altra", Toast.LENGTH_LONG).show();
}
});
照片(int)函数:
@SuppressLint("SimpleDateFormat")
public void hacerfoto(int num){
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
String n = sdf.format(new Date());
String fotoname = "Immagine-"+ n +".jpg";
File photostorage = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File photostorage2 = new File(photostorage, "im");
photostorage2.mkdirs();
photofile = new File(photostorage2, fotoname);
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
mMakePhotoUri = Uri.fromFile(photofile);
i.putExtra(MediaStore.EXTRA_OUTPUT, mMakePhotoUri);
startActivityForResult(i, num);
}
我的 onActivityResult 函数:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
String filename = sacarfoto();
if (requestCode == 1){
sacarfoto();
if(photo != null){
ruta1 = sacarfoto();
foto1.setBackgroundColor(Color.parseColor("#00000000"));
photo = f.resize(photo, filename);
foto1.setImageBitmap(photo);
cancellare1.setVisibility(View.VISIBLE);
}
else{
cancellare1.setVisibility(View.GONE);
foto1.setBackgroundResource(R.drawable.fondoicona);
Toast.makeText(this, "C'è stato un errore, riprova a scattare la foto", Toast.LENGTH_LONG).show();
}
try {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(ruta1);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
} catch (Exception e) {
}
}
if (requestCode == 2){
sacarfoto();
if(photo != null){
ruta2 = sacarfoto();
photo = f.resize(photo, filename);
foto2.setImageBitmap(photo);
foto2.setBackgroundColor(Color.parseColor("#00000000"));
cancellare2.setVisibility(View.VISIBLE);
}
else{
cancellare2.setVisibility(View.GONE);
foto2.setBackgroundResource(R.drawable.fondoicona);
Toast.makeText(this, "C'è stato un errore, riprova a scattare la foto", Toast.LENGTH_LONG).show();
}
try{
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(ruta2);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
} catch (Exception e) {
}
}
if (requestCode == 3){
sacarfoto();
if(photo != null){
ruta3 = sacarfoto();
photo = f.resize(photo, filename);
foto3.setImageBitmap(photo);
foto3.setBackgroundColor(Color.parseColor("#00000000"));
cancellare3.setVisibility(View.VISIBLE);
}
else{
cancellare3.setVisibility(View.GONE);
foto3.setBackgroundResource(R.drawable.fondoicona);
Toast.makeText(this, "C'è stato un errore, riprova a scattare la foto", Toast.LENGTH_LONG).show();
}
try {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(ruta3);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
} catch (Exception e) {
}
}
if (requestCode == 4) {
try{
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
photo = f.decodeAndResizeFile(new File(selectedImagePath));
} catch(NullPointerException ex){
try {
photo = (Bitmap) data.getExtras().get("data");
}
catch (Exception e){
photo = BitmapFactory.decodeFile(selectedImagePath);
}
}
if(photo != null){
if (foto1.getDrawable()==null){
photo = f.resize(photo,selectedImagePath);
ruta1 = selectedImagePath;
foto1.setImageBitmap(photo);
foto1.setBackgroundColor(Color.parseColor("#00000000"));
cancellare1.setVisibility(View.VISIBLE);
}
else if (foto2.getDrawable()==null){
photo = f.resize(photo,selectedImagePath);
ruta2 = selectedImagePath;
foto2.setImageBitmap(photo);
foto2.setBackgroundColor(Color.parseColor("#00000000"));
cancellare2.setVisibility(View.VISIBLE);
}
else if (foto3.getDrawable()==null){
photo = f.resize(photo,selectedImagePath);
ruta3 = selectedImagePath;
foto3.setImageBitmap(photo);
foto3.setBackgroundColor(Color.parseColor("#00000000"));
cancellare3.setVisibility(View.VISIBLE);
}
else
Toast.makeText(sinistri2.this, "Cancella una foto per poter scattare un'altra", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(this, "Non si è potuto riuscire, riprova scattando una foto o scegliendo una foto dalla gallery.", Toast.LENGTH_LONG).show();
}
}
}
}
该函数sacarfoto()
给出了一个字符串,但有时可以用于执行一个过程,但在这个错误中并不重要。