2013-07-08 2 views
1

MediaStore를 사용하여 사진을 찍고 특정 폴더에 이미지를 저장하고 표시하려고합니다. 폴더가 생성되었지만 이미지 파일은 생성되지 않았습니다.안드로이드 카메라가 이미지를 저장하지 않습니다.

public void getPicture(View view) 
{ 
    File folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM/Camera/folder"); 
    if(!folder.exists()) { 
      if(!folder.mkdirs()){ 
       Toast.makeText(this, "cannot create folder", Toast.LENGTH_LONG).show();  
      } 
    } 

    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
    File imageFile = new File(folder, "image.jpg"); 

    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imageFile)); 
    sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory()))); 
    startActivityForResult(cameraIntent, RESULT_CAMERA);  
} 

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 

    setContentView(R.layout.activity); 

    if(resultCode == Activity.RESULT_OK) 
    { 
     if(requestCode == RESULT_CAMERA) 
     { 
      Bitmap bitmap = BitmapFactory.decodeFile(REAR_END_PATH + "/image.jpg"); 

      // create byte array from the Bitmap object 
      ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
      bitmap.compress(Bitmap.CompressFormat.JPEG, 20, stream); 

      ImageView iv = new ImageView(this); 
      iv.setImageBitmap(bitmap); 
      setContentView(iv); 
     } 
    } 
} 

답변

0

내 코드 파일

BitmapFactory.Options 옵션 = 새로운 BitmapFactory.Options을 시도();

 // downsizing image as it throws OutOfMemory Exception for larger 
     // images 
     options.inSampleSize = 2; 

     final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(), 
       options); 
     poto.setImageBitmap(bitmap); 

    } 
    catch (NullPointerException e) 
    { 
     //e.printStackTrace(); 
    } 

}

//------------ Helper Methods ---------------------- 

    // Creating file uri to store image/video 
    public Uri getOutputMediaFileUri(int type) 
{ 
     return Uri.fromFile(getOutputMediaFile(type)); 
} 

// returning image 
private static File getOutputMediaFile(int type) 

{

 // External sdcard location 

     File mediaStorageDir = new File 
       (
       Environment 
       .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), 
       IMAGE_DIRECTORY_NAME 
       ); 

     // Create the storage directory if it does not exist 
     if (!mediaStorageDir.exists()) 
     { 
     if (!mediaStorageDir.mkdirs()) 
     { 
     Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create " 
      + IMAGE_DIRECTORY_NAME + " directory"); 
     return null; 
     } 
     } 

     String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", 
       Locale.getDefault()).format(new Date()); 
     File mediaFile; 

     if (type ==MEDIA_TYPE_IMAGE) 
     { 
      mediaFile = new File (mediaStorageDir.getPath()+ File.separator + "IMG_" + timeStamp + ".jpg"); 
     } 
     else 
     { 
      return null; 
     } 
    // TODO Auto-generated method stub 
     return mediaFile; 

}

관련 문제