2014-01-09 80 views
0

我已經是從所謂的活動TabActivity(我知道這是不建議使用)爲:帶攝像頭的意圖拍照永遠不會回到onActivityResult()

intent2 = new Intent().setClass(this, sin2.class); 
spec = tabHost.newTabSpec("second").setIndicator("Second").setContent(intent2); 
tabHost.addTab(spec); 

這是拍攝照片的活動,將它們保存並設置縮略圖。它完美的作品在大量的設備,但三星Galaxy掌上銀河奇蹟(總的Android 2.X)有時當我啓動相機意圖,它永遠不會回到onActivityResult(),這意味着:

  1. 我拍照
  2. 我保存它
  3. 我不回去onActivityResult()
  4. 它讓我再拍一張照片

和循環...我永遠不會回來,如果我按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()給出了一個String,但有時候會用於執行一個進程,但這個錯誤並不重要。

+0

「num」的值是什麼? –

+0

我有很多'if(requestCode == someNum)',我處理它...它在一些設備上正常工作,例如我的。 num可能是1,2或3 –

+0

將您的完整代碼 –

回答

1

我敢肯定,你認爲它從來沒有返回,但實際上它返回RESULT_CANCEL。嘗試在onActivityResult中添加if(result == RESULT_CANCEL)。也許攝像頭不可能保存圖片或類似的東西。

+0

但是,它有時可以正常工作...我的意思是...如果時間不起作用,我重新啓動應用程序並工作正常......無論如何,你會給我這個答案嗎? =)。謝謝你的時間。 –

+0

嘗試做一些調試並找出錯誤。意圖是否返回null extras?是保存的圖像? –

+0

是的,圖像保存爲我說的,然後意圖是空=) –

相關問題