public class MainActivity extends AppCompatActivity {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button);
button2 = (Button)findViewById(R.id.button2);
imageView = (ImageView)findViewById(R.id.imageView);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Camera();
}
});
}
public void Camera()
{
String state = Environment.getExternalStorageState();
if(Environment.MEDIA_MOUNTED.equals(state))
{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if(intent.resolveActivity(getPackageManager()) != null)
{
File photoFile = null;
try
{
photoFile = createImageFile();
}
catch(IOException e)
{
Toast.makeText(this, "Camera Error", Toast.LENGTH_SHORT).show();
}
if(photoFile != null)
{
Uri providerUri = FileProvider.getUriForFile(this, getPackageName()+".fileprovider", photoFile);
imageUri = providerUri;
intent.putExtra(MediaStore.EXTRA_OUTPUT, providerUri);
startActivityForResult(intent, REQUEST_CAMERA);
}
}
else
{
Toast.makeText(this, "None createImageFile", Toast.LENGTH_SHORT).show();
}
}
else
{
Toast.makeText(this, "저장 공간 접근 불가", Toast.LENGTH_SHORT).show();
return;
}
}
private File createImageFile() throws IOException
{
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
imageFileName = timeStamp + ".jpg";
imageFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/image/"+imageFileName);
if(!imageFile.exists())
{
imageFile.getParentFile().mkdirs();
imageFile.createNewFile();
}
mCurrentPhotoPath = imageFile.getAbsolutePath(); <- 여기에서는 파일 경로가 잘지정되어 확인가능한데요
return imageFile;
}
private void galleryAddPic()
{
Log.i("gelleyAddPic", "Start");
Log.i("mCurrentPhotoPath", mCurrentPhotoPath);
Toast.makeText(this, mCurrentPhotoPath, Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File file = new File(mCurrentPhotoPath); <-- 여기로 넘어와서 확인해보면 null 값이 들어와있어요
contentUri = Uri.fromFile(file);
intent.setData(contentUri);
bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
imageView.setImageBitmap(bitmap);
sendBroadcast(intent);
Toast.makeText(this, "사진 저장 완료", Toast.LENGTH_SHORT).show();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode)
{
case REQUEST_CAMERA :
try
{
galleryAddPic();
}
catch(Exception e)
{
e.printStackTrace();
Toast.makeText(this, "Camera Error2", Toast.LENGTH_SHORT).show();
}
break;
}
}
createImageFile 메소드를 벗어나면 mCurrentPhotoPath 변수가 null값이 되어버립니다;;
근데 더 웃긴건 위 소스로도 어쩔때는 정상적으로 실행이 되다가 갑자기 안된다는 점입니다;;
갤럭시 s4로 테스트 했고, AVD로 이용시에는 정상적으로 실행됩니다;; 도데체 왜 됬다가 안됬다가 하는 건지 이유를 모르겠습니다. 답변 부탁드려요 ㅠㅠ