2011-07-15 27 views
2

我用下面的代碼,以我的應用程序啓動相機:製作圖片的意圖失敗的三星Galaxy I9000

private void saveFullImage() { 
    String storageState = Environment.getExternalStorageState(); 
    if (storageState.equals(Environment.MEDIA_MOUNTED)) { 
     String path = Environment.getExternalStorageDirectory().getName() 
       + File.separatorChar + "Android/data/" 
       + RegistrationDetails.this.getPackageName() + "/files/" 
       + md5("filedummy") + ".jpg"; 
     File photoFile = new File(path); 
     try { 
      if (photoFile.exists() == false) { 
       photoFile.getParentFile().mkdirs(); 
       photoFile.createNewFile(); 
      } 

     } catch (IOException e) { 
      Log.e(TAG, "Could not create file.", e); 
     } 
     Log.i(TAG, path); 

     Uri fileUri = Uri.fromFile(photoFile); 
     Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); 
     startActivityForResult(intent, TAKE_PICTURE); 
    } else { 
     new AlertDialog.Builder(this) 
       .setMessage(
         "External Storeage (SD Card) is required.\n\nCurrent state: " 
           + storageState).setCancelable(true) 
       .create().show(); 
    } 
} 

而且我在onActivityResult下列代碼顯示,圖像拍攝完畢,所以我可以進行下一步:

 } else if (requestCode == TAKE_PICTURE) { 
      if (data == null) { 
       Toast toast = Toast.makeText(getApplicationContext(), 
         "Take Picture finished", 10); 
       toast.show(); 
      } 

而且我已經定義在AndroidManifest以下設置:android.permission.CAMERA和android.permission.WRITE_EXTERNAL_STORAGE

的啓動相機意圖的作品,但是當我拍照並點擊保存時,它不會返回到onActivityResult,我的應用程序崩潰。

有人可以幫助我嗎?

謝謝

回答

2

我在Galaxy S 2.3.4版和相機上遇到了一些問題。

經過一些工作,它實際上可以與此代碼一起工作(使用Galaxy S和Nexus S進行測試)。 你可以試試看,如果它適合你。

private Uri mCapturedImageURI; 


private void takePhoto() { 
    String fileName = "temp.jpg"; 
    if (isGalaxyS()) { 
     fileName = Repertoires_S.getInstance().Get_Photos_Path() + fileName; 
     File fileTmp = new File(fileName); 
     if (fileTmp.exists()) fileTmp.delete(); 
     mCapturedImageURI = Uri.fromFile(fileTmp); 
    } else { 
     ContentValues values = new ContentValues(); 
     values.put(MediaStore.Images.Media.TITLE, fileName); 
     values.put(MediaStore.Images.Media.DESCRIPTION, "Image prise via XXX :)"); 
     mCapturedImageURI = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); 
    } 

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    intent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI); 
    startActivityForResult(intent, PICK_FROM_CAMERA); 
} 


private boolean isGalaxyS() { 
    Log.d("Photo", android.os.Build.BRAND + "/" + android.os.Build.PRODUCT + "/" 
      + android.os.Build.DEVICE); 

    ArrayList<String> devices = new ArrayList<String>(); 
    devices.add("samsung/GT-I9000/GT-I9000"); 

    return devices.contains(android.os.Build.BRAND + "/" + android.os.Build.PRODUCT + "/" 
      + android.os.Build.DEVICE); 
} 


protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode != RESULT_OK) { 
     return; 
    } 

    switch (requestCode) { 
     case PICK_FROM_CAMERA: { 
      Bitmap photo = null; 
      String filenameDest = Repertoires_S.getInstance().Get_Photos_Path() + DateTime_BO_S.getInstance().Date_Courante_AAAAMMJJ_HHMMSS() + ".jpg"; 
      File fDest = new File(filenameDest); 
      String capturedImageFilePath; 

      if (data == null) { 
       // "intent.putExtra(MediaStore.EXTRA_OUTPUT, XXXX);" utilisé 
       try { 
        if (isGalaxyS()) { 
         String fileName = "temp.jpg"; 
         capturedImageFilePath = Repertoires_S.getInstance().Get_Photos_Path() + fileName; 
        } else { 
         String[] projection = {MediaStore.Images.Media.DATA}; 
         Cursor cursor = managedQuery(mCapturedImageURI, projection, null, null, null); 
         int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
         cursor.moveToFirst(); 
         capturedImageFilePath = cursor.getString(column_index_data); 
        } 
        File fSrc = new File(capturedImageFilePath); 
        fSrc.renameTo(fDest); 
        photo = BitmapFactory.decodeFile(filenameDest); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 

      } else { 
       // "intent.putExtra(MediaStore.EXTRA_OUTPUT, XXXX);" non utilisé -> miniature 
       Bundle extras = data.getExtras(); 
       if (extras != null) { 
        photo = extras.getParcelable("data"); 
       } 
      } 

      if (photo != null) { 
       try { 
        int tailleMaxPhoto = 800; 
        double rap; 
        int newWidth; 
        int newHeight; 
        if (photo.getWidth() > tailleMaxPhoto || photo.getHeight() > tailleMaxPhoto) { 
         // Si c'est plus grand on redimensionne 
         if (photo.getWidth() > photo.getHeight()) { 
          rap = (double)tailleMaxPhoto/photo.getWidth(); 
          newWidth = tailleMaxPhoto; 
          newHeight = (int)((double)photo.getHeight() * rap); 
         } else { 
          rap = (double)tailleMaxPhoto/photo.getHeight(); 
          newHeight = tailleMaxPhoto; 
          newWidth = (int)((double)photo.getWidth() * rap); 
         } 
         Bitmap photoSmall = Bitmap.createScaledBitmap(photo, newWidth, newHeight, true); 
         if (photoSmall != null) { 
          FileOutputStream out = new FileOutputStream(filenameDest); 
          if (photoSmall.compress(Bitmap.CompressFormat.JPEG, 90, out)) { 
           mPhotosPrat.add(filenameDest); 
           mPhotoAdapt.notifyDataSetChanged(); 
          } 
         } 
        } else { 
         mPhotosPrat.add(filenameDest); 
         mPhotoAdapt.notifyDataSetChanged(); 
        } 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 

      break; 
     } 
    } 
} 
+0

你可以告訴Repertoires_S是什麼嗎?這是一個特殊的班級嗎?如果是的話,你能否也請包括代碼?謝謝。 – Tand

+0

這只是一個自定義函數,它給了我一個保存圖片的目錄。你可以用你想要的任何路徑代替這個函數(比如sdcard路徑) –