0

何が起こっている:

  1. showCameraApp()と呼ばれます。(現在の日付) にcurrentPhotoPath設定されます。/mnt/sdcard/20_08_22_06_33.jpg
  2. 写真を作成するためのデフォルトの Android アプリケーションが表示されます (Intentで始まりstartActivityForResultます)。
  3. ユーザーが写真を作成して保存しています。
  4. のアプリケーションに戻り、onActivityResult()呼び出さcameraManager.getPhoto()れます。
  5. currentPhotoPathcameraManager.getPhoto()ですnull

質問 (必ずしもすべてではなく、任意の質問に自由に回答してください):

  1. 以前に設定したにもかかわらず、なぜcurrentPhotoPathですか?null
  2. プライベート変数に格納する必要がないかもしれませんが、 ?currentPhotoPathで渡すことができるかもしれません。Intentで試しましたがintent.putExtra("some", "thing")、後で.Intent datanull
  3. おそらく一般的にBitmap、ユーザーが作成した写真から(冗長ファイルに保存せずに)取得する簡単な方法がありますか?

私の盲目的な推測では、おそらくマルチスレッドを使用しているので、に追加volatileしましたcurrentPhotoPathが、役に立ちませんでした。

MainActivityクラス:

public class MainActivity extends Activity implements OnClickListener 
{
    private CameraManager cameraManager;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        cameraManager = new CameraManager(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if (requestCode == CameraManager.REQUEST_CODE && resultCode == RESULT_OK)
        {
            Bitmap bitmap = cameraManager.getPhoto(); // HERE bitmap is null!
        }
    }   
}

CameraManagerクラス:

public class CameraManager
{
    public final static int REQUEST_CODE = 666;

    private /*volatile*/ String currentPhotoPath; // THIS VARIABLE BEHAVES STRANGE
    private final Activity activity;

    public CameraManager(Activity activity)
    {
        this.activity = activity;
    }

    public void showCameraApp()
    {
        final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        currentPhotoPath = createNewPhotoPath(); // HERE currentPhotoPath is set
        final File file = new File(currentPhotoPath);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
        activity.startActivityForResult(intent, REQUEST_CODE);
    }

    private String createNewPhotoPath()
    {
        final String date = new SimpleDateFormat("dd_MM_HH_mm_ss", Locale.UK).format(new Date());
        return Environment.getExternalStorageDirectory() + "/" + date + ".jpg";
    }

    public Bitmap getPhoto()
    {
        return BitmapFactory.decodeFile(currentPhotoPath); // HERE currentPhotoPath is null!
    }
}
4

2 に答える 2

2

MainActivity戻ってきたときにの新しいインスタンスが作成されていると思います。CameraManagerしたがって、フィールドが設定されていない新しいインスタンスも割り当てます。

ドキュメントのSaving Activity Stateをご覧ください。

于 2013-08-21T10:54:04.247 に答える