何が起こっている:
showCameraApp()
と呼ばれます。(現在の日付) にcurrentPhotoPath
設定されます。/mnt/sdcard/20_08_22_06_33.jpg
- 写真を作成するためのデフォルトの Android アプリケーションが表示されます (
Intent
で始まりstartActivityForResult
ます)。 - ユーザーが写真を作成して保存しています。
- のアプリケーションに戻り、
onActivityResult()
呼び出さcameraManager.getPhoto()
れます。 currentPhotoPath
中cameraManager.getPhoto()
ですnull
!
質問 (必ずしもすべてではなく、任意の質問に自由に回答してください):
- 以前に設定したにもかかわらず、なぜ
currentPhotoPath
ですか?null
- プライベート変数に格納する必要がないかもしれませんが、 ?
currentPhotoPath
で渡すことができるかもしれません。Intent
で試しましたがintent.putExtra("some", "thing")
、後で.Intent data
null
- おそらく一般的に
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!
}
}