2016-10-18 2 views
0

사진을 찍을 수있는 프로그램을 작성했습니다. 문제는 DIRECTORY_PICTURES에 저장하려고 할 때입니다. 사진 디렉토리에서 찍은 사진을 찾을 수 없습니다. 코드에 무엇이 잘못 되었습니까? 이 프로그램은 내가 문제없이 카메라를 개방 쓴 나는 성공적으로 사진을 찍을 수 있지만사진을 찍어 android studio를 사용하여 저장하십시오.

ImageView mImageView; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    mImageView=(ImageView)findViewById(R.id.imageView) ; 
} 


static final int REQUEST_IMAGE_CAPTURE=1; 
public void bucapture(View view) { 
    Intent takePictureIntent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    if(takePictureIntent.resolveActivity(getPackageManager())!=null){ 
     startActivityForResult(takePictureIntent,REQUEST_IMAGE_CAPTURE); 
    } 

} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if(requestCode==REQUEST_IMAGE_CAPTURE && resultCode==RESULT_OK){ 
     Bundle extras=data.getExtras(); 
     Bitmap imageBitmap=(Bitmap) extras.get("data"); 
     mImageView.setImageBitmap(imageBitmap); 
     try { 
      createImageFile(); 
      galleryAddpic(); 

     }catch (Exception ex){ 
      ex.printStackTrace(); 
     } 

    } 
} 

String mCurrentPhotpPath; 
//create image name 
private File createImageFile() throws IOException { 
    String timeStamp=new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
    String imageFileName="JPGE"+timeStamp+"_"; 
    File storgeDir= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); 
    File image=File.createTempFile(imageFileName,".jpg",storgeDir); 
    mCurrentPhotpPath="file:"+image.getAbsolutePath(); 
    return image; 
} 


//save image 
private void galleryAddpic(){ 
    Intent mediaScanIntent=new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); 
    File f=new File(mCurrentPhotpPath); 
    Uri contentUri=Uri.fromFile(f); 
    mediaScanIntent.setData(contentUri); 
    this.sendBroadcast(mediaScanIntent); 

    } 
} 
+0

당신을 드라이브에 결코 실제로 출력 비트 맵 어디서나 사진 디렉토리에 이미지가 없다 – dymmeh

답변

0
//This is one of the best can take photo full size in android an save it but create temp file that what exactly is in your code. it doesn't work some of custom phones like some model of samsung & Mi there you can check for file 
//and you have to use Intent Action picker intent for that. 

private URI mImageUri; 

Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); 
File photo; 
try 
{ 
    // place where to store camera taken picture 
    photo = this.createTemporaryFile("picture", ".jpg"); 
    photo.delete(); 
} 
catch(Exception e) 
{ 
    Log.v(TAG, "Can't create file to take picture!"); 
    Toast.makeText(activity, "Please check SD card! Image shot is impossible!", 10000); 
    return false; 
} 
mImageUri = Uri.fromFile(photo); 
intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri); 
//start camera intent 
activity.startActivityForResult(this, intent, MenuShootImage); 

private File createTemporaryFile(String part, String ext) throws Exception 
{ 
File tempDir= Environment.getExternalStorageDirectory(); 
tempDir=new File(tempDir.getAbsolutePath()+"/.temp/"); 
if(!tempDir.exists()) 
{ 
    tempDir.mkdirs(); 
} 
return File.createTempFile(part, ext, tempDir); 
} 

//Then after image capture intent finished to work - just grab your picture from imageUri using following code: 

public void grabImage(ImageView imageView) 
{ 
this.getContentResolver().notifyChange(mImageUri, null); 
ContentResolver cr = this.getContentResolver(); 
Bitmap bitmap; 
try 
{ 
    bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr,  mImageUri); 
    imageView.setImageBitmap(bitmap); 
} 
catch (Exception e) 
{ 
    Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT).show(); 
    Log.d(TAG, "Failed to load", e); 
    } 
} 


//called after camera intent finished taking photo 
    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent intent) 
{ 
if(requestCode==MenuShootImage && resultCode==RESULT_OK) 
{ 
    ImageView imageView; 
    this.grabImage(imageView); 
} 
super.onActivityResult(requestCode, resultCode, intent); 
} 
관련 문제