2012-06-23 59 views
0

我製作了一個基於標籤的應用程序。有三個選項卡。其中一個標籤用於相機。所以當我按下相機標籤時,它會打開相機活動,以便我可以拍照。直到這裏它工作正常。當我拍照時,那張照片在ImageView中顯示。 從這裏,當我按任何其他選項卡,然後我按相機選項卡,然後相機活動不啓動。它不斷向我顯示ImageView上的上一張圖像。我想要的是每當用戶點擊相機選項卡,然後它應該打開相機的活動。 我該如何達到這個效果?第二次沒有啓動相機活動

這是我的代碼。在os下創建攝像頭活動中的方法。所以當按下攝像機標籤時,它會打開這個活動。

protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.camera); 

    userFunctions = new UserFunctions(); 
    globalUID = getIntent().getExtras().getString("globalUID"); 
    Toast.makeText(getApplicationContext(), globalUID, Toast.LENGTH_LONG).show(); 
    ivUserImage = (ImageView)findViewById(R.id.ivUserImage); 
    bUpload = (Button)findViewById(R.id.bUpload); 
    openCamera(); 
} 

private void openCamera() { 
    i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
    startActivityForResult(i, CameraResult); 
} 

更新

我這樣做,但仍是同樣的問題

boolean flag = true; 
@Override 
protected void onResume() { 
    super.onResume(); 
    //flag = true; 
    if(flag) { 
     openCamera(); 
     flag = false; 
    } 
} 

編輯

以下Imran的回答我的代碼看起來現在這個樣子了。我還添加了額外的代碼。但是這個代碼仍然不起作用。

public class Camera extends Activity { 
    ImageView ivUserImage; 
    Button bUpload; 
    Intent i; 
    int CameraResult = 0; 
    Bitmap bmp; 
    public String globalUID; 
    UserFunctions userFunctions; 
    String photoName; 
    InputStream is; 
    String largeImagePath; 
    int serverResponseCode = 0; 
    public static boolean flag = true; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.camera); 

     userFunctions = new UserFunctions(); 
     globalUID = getIntent().getExtras().getString("globalUID"); 
     //flag = getIntent().getExtras().getBoolean("flag"); 
     Toast.makeText(getApplicationContext(), globalUID, Toast.LENGTH_LONG).show(); 
     ivUserImage = (ImageView)findViewById(R.id.ivUserImage); 
     bUpload = (Button)findViewById(R.id.bUpload); 
     //openCamera(); 
     if(flag ==true) 
      openCamera(); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     if(flag==true) 
      openCamera(); 
    } 

    private void openCamera() { 
     i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
     startActivityForResult(i, CameraResult); 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

     super.onActivityResult(requestCode, resultCode, data); 
     flag = false; 
     if(resultCode == RESULT_OK) { 

      //set image taken from camera on ImageView 
      Bundle extras = data.getExtras(); 
      bmp = (Bitmap) extras.get("data"); 
      ivUserImage.setImageBitmap(bmp); 

      //Create new Cursor to obtain the file Path for the large image 
      String[] largeFileProjection = { 
       MediaStore.Images.ImageColumns._ID, 
       MediaStore.Images.ImageColumns.DATA 
      }; 

      String largeFileSort = MediaStore.Images.ImageColumns._ID + " DESC"; 
      Cursor myCursor = this.managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, largeFileProjection, null, null, largeFileSort); 

      try { 
       myCursor.moveToFirst(); 
       //This will actually give you the file path location of the image. 
       largeImagePath = myCursor.getString(myCursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.DATA)); 

       File f = new File("" + largeImagePath); 

       photoName = f.getName(); 

       bUpload.setOnClickListener(new View.OnClickListener() { 
        public void onClick(View v) { 
         new Upload().execute(largeImagePath, globalUID, photoName); 
        } 

       }); 

      } finally { 
       myCursor.close(); 
      } 
     } 
    } 
} 

這裏是我的標籤活動代碼

public class DashboardActivity extends TabActivity { 
    UserFunctions userFunctions; 
    public String globalUID; 
    DatabaseHandler db; 
    boolean flag = true; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.dashboard); 

     db = new DatabaseHandler(getApplicationContext()); 
     // Check login status in database 
     userFunctions = new UserFunctions(); 

     if(userFunctions.isUserLoggedIn(getApplicationContext())){ 
      //globalUID = getIntent().getExtras().getString("globalUID"); 
      HashMap<String, String> data = db.getUserDetails(); 
      globalUID = data.get("uid"); 
      Resources res = getResources(); // Resource object to get Drawables 
      TabHost tabHost = getTabHost(); // The activity TabHost 
      TabHost.TabSpec spec; // Resusable TabSpec for each tab 
      Intent intent; // Reusable Intent for each tab 

      //Home 
      intent = new Intent(this, Home.class); 
      intent.putExtra("globalUID", globalUID); 
      spec = tabHost.newTabSpec("home").setIndicator("", res.getDrawable(R.drawable.home_icon)).setContent(intent); 
      tabHost.addTab(spec); 

      //Camera 
      intent = new Intent(this, Camera.class); 
      intent.putExtra("globalUID", globalUID); 
      //intent.putExtra("flag", flag); 
      spec = tabHost.newTabSpec("camera").setIndicator("", res.getDrawable(R.drawable.camera_icon)).setContent(intent); 
      tabHost.addTab(spec); 

      //Albums 
      intent = new Intent(this, Albums.class); 
      intent.putExtra("globalUID", globalUID); 
      spec = tabHost.newTabSpec("albums").setIndicator("", res.getDrawable(R.drawable.albums_icon)).setContent(intent); 
      tabHost.addTab(spec); 

      //tabHost.setCurrentTab(1); 
     } else { 
      // user is not logged in show login screen 
      Intent login = new Intent(getApplicationContext(), LoginActivity.class); 
      login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
      startActivity(login); 
      // Closing dashboard screen 
      finish(); 
     } 
    } 
} 

這裏是我的整個相機活動

public class Camera extends Activity { 
    ImageView ivUserImage; 
    Button bUpload; 
    Intent i; 
    int CameraResult = 0; 
    Bitmap bmp; 
    public String globalUID; 
    UserFunctions userFunctions; 
    String photoName; 
    InputStream is; 
    String largeImagePath; 
    int serverResponseCode = 0; 
    public static boolean flag = true; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.camera); 

     userFunctions = new UserFunctions(); 
     globalUID = getIntent().getExtras().getString("globalUID"); 
     //flag = getIntent().getExtras().getBoolean("flag"); 
     Toast.makeText(getApplicationContext(), globalUID, Toast.LENGTH_LONG).show(); 
     ivUserImage = (ImageView)findViewById(R.id.ivUserImage); 
     bUpload = (Button)findViewById(R.id.bUpload); 
     //openCamera(); 
     //if(flag ==true) 
      openCamera(); 
    } 
    /* 
    @Override 
    protected void onResume() { 
     super.onResume(); 
     if(flag==true) 
      openCamera(); 
    }*/ 

    private void openCamera() { 
     i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
     startActivityForResult(i, CameraResult); 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

     super.onActivityResult(requestCode, resultCode, data); 
     flag = false; 
     if(resultCode == RESULT_OK) { 

      //set image taken from camera on ImageView 
      Bundle extras = data.getExtras(); 
      bmp = (Bitmap) extras.get("data"); 
      ivUserImage.setImageBitmap(bmp); 

      //Create new Cursor to obtain the file Path for the large image 
      String[] largeFileProjection = { 
        MediaStore.Images.ImageColumns._ID, 
        MediaStore.Images.ImageColumns.DATA 
      }; 

      String largeFileSort = MediaStore.Images.ImageColumns._ID + " DESC"; 
      Cursor myCursor = this.managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, largeFileProjection, null, null, largeFileSort); 

      try { 
       myCursor.moveToFirst(); 
       //This will actually give you the file path location of the image. 
       largeImagePath = myCursor.getString(myCursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.DATA)); 

       File f = new File("" + largeImagePath); 

       photoName = f.getName(); 

       bUpload.setOnClickListener(new View.OnClickListener() { 
        public void onClick(View v) { 
         new Upload().execute(largeImagePath, globalUID, photoName); 
        } 

       }); 

      } finally { 
       myCursor.close(); 
      } 
     } 
    } 

    public class Upload extends AsyncTask<String, Integer, String> { 

     ProgressDialog dialog; 

     protected void onPreExecute() { 
      dialog = ProgressDialog.show(Camera.this, "", "Uploading file...", true); 
     } 

     @Override 
     protected String doInBackground(String... arg0) { 
      // TODO Auto-generated method stub 
      String success = "false"; 
      Bitmap bitmapOrg = setImageToImageView(largeImagePath);//BitmapFactory.decodeFile(largeImagePath); 
      ByteArrayOutputStream bao = new ByteArrayOutputStream(); 
      bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao); 

      byte [] ba = bao.toByteArray(); 
      String ba1=Base64.encodeToString(ba, 0); 

      ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
      nameValuePairs.add(new BasicNameValuePair("image",ba1)); 
      nameValuePairs.add(new BasicNameValuePair("imageName", photoName)); 
      nameValuePairs.add(new BasicNameValuePair("uid", globalUID)); 

      try { 
       HttpClient httpclient = new DefaultHttpClient(); 
       HttpPost httppost = new HttpPost("http://www.example.info/android/fileupload.php"); 
       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
       HttpResponse response = httpclient.execute(httppost); 
       HttpEntity entity = response.getEntity(); 
       if(response != null) { 
        success = "true"; 
       } 
       is = entity.getContent(); 
      } catch(Exception e) { 
       Log.e("log_tag", "Error in http connection "+e.toString()); 
      } 
      dialog.dismiss(); 
      return success; 
     } 

     protected void onProgressUpdate(Integer...progress) { 

     } 

     protected void onPostExecute(String f) { 
      Toast.makeText(getApplicationContext(), "File uploaded", Toast.LENGTH_LONG).show(); 
     } 

    } 

    public Bitmap setImageToImageView(String filePath) { 
     // Decode image size 
     BitmapFactory.Options o = new BitmapFactory.Options(); 
     o.inJustDecodeBounds = true; 
     BitmapFactory.decodeFile(filePath, o); 

     // The new size we want to scale to 
     final int REQUIRED_SIZE = 1024; 

     // Find the correct scale value. It should be the power of 2. 
     int width_tmp = o.outWidth, height_tmp = o.outHeight; 
     int scale = 1; 
     while (true) { 
      if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE) 
      break; 
      width_tmp /= 2; 
      height_tmp /= 2; 
      scale *= 2; 
     } 

     // Decode with inSampleSize 
     BitmapFactory.Options o2 = new BitmapFactory.Options(); 
     o2.inSampleSize = scale; 
     Bitmap bitmap = BitmapFactory.decodeFile(filePath, o2); 
     return bitmap; 

    } 

    public boolean fileData(String guid, String photoName) { 
     Toast.makeText(getApplicationContext(), photoName, Toast.LENGTH_LONG).show(); 

     JSONObject json = userFunctions.uploadFileData(guid, photoName); 

     try { 
      if(Integer.parseInt(json.getString("success")) != 1) { 
       Toast.makeText(getApplicationContext(), json.getInt("error_msg"), Toast.LENGTH_LONG).show(); 
       //register_error.setText(json.getString("error_msg")); 
       return false; 
      } 
     } catch (NumberFormatException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     return true; 
    } 
} 

回答

0

這是因爲當你的標籤你的活動犯規通話之間切換再次創建。

嘗試將呼叫移動到openCamera();進入onResume方法,它應該沒問題。

protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.camera); 

    userFunctions = new UserFunctions(); 
    globalUID = getIntent().getExtras().getString("globalUID"); 
    Toast.makeText(getApplicationContext(), globalUID, Toast.LENGTH_LONG).show(); 
    ivUserImage = (ImageView)findViewById(R.id.ivUserImage); 
    bUpload = (Button)findViewById(R.id.bUpload); 

} 



protectd void onResume(){ 
     super.onResume(); 
     openCamera(); 
} 

private void openCamera() { 
    i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
    startActivityForResult(i, CameraResult); 
} 
+0

我把openCamera中的onResume方法,但現在我甚至不能看到,這是採取在ImageView上。實際上,當我點擊保存按鈕開啓相機活動時,它會將我帶回相機活動,這會在拍攝五六張照片後繼續保持。 – 2619

0

TabHost電話onCreate()只有一次的理由效率的每個Activity。這就是爲什麼openCamera()不會被一次又一次調用。

爲了解決這個問題,您可以在父母Activity中實現並註冊一個OnTabChangeListener,您可以在其中捕獲哪個標籤被點擊並執行任何所需的操作。

希望這會有所幫助!

0

在您的活動中添加onResume()並調用openCamera();來自的onResume(),也爲:

public static boolean flag = true; 

protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.camera); 

    userFunctions = new UserFunctions(); 
    globalUID = getIntent().getExtras().getString("globalUID"); 
    Toast.makeText(getApplicationContext(), globalUID, Toast.LENGTH_LONG).show(); 
    ivUserImage = (ImageView)findViewById(R.id.ivUserImage); 
    bUpload = (Button)findViewById(R.id.bUpload); 
if(flag ==true) 
    openCamera(); 
} 


     @Override 
     protected void onResume() { 
      super.onResume(); 
if(flag==true) 
      openCamera(); 
     } 

,並在onActivityResut()

flag =false; 

或tabclick

YourflagActivity.flag=true; 
+0

我把openCamera放在onResume方法中,但現在我甚至看不到在ImageView上採用哪種方法。實際上,當我點擊保存按鈕開啓相機活動時,它會將我帶回相機活動,這會在拍攝五六張照片後繼續保持。 – 2619

+0

@ al0neevenings:使用一個布爾值在onResume中放置條件,然後如果Activity由Camara應用程序恢復,則不需要調用方法openCamera,或者如果通過單擊選項卡恢復活動,則調用openCamera.i認爲你明白我想說的話 –

+0

我怎麼知道活動目前是否恢復? – 2619

相關問題