2013-11-21 3 views
0

조각에서 사진을 클릭하여 내 앱에 표시하려고합니다. 사진을 클릭하면 imageView가 설정되지 않고 사진이 표시되는 것을 볼 수 없습니다. 아무도 이유를 아나요?카메라를 사용하여 사진을 찍은 후 사진이 표시되지 않습니다.

조각 내에서 사진을 찍는 코드를 작성하는 더 좋은 방법이 있다고 생각합니까?

public class ImageFragment extends Fragment{ 


private Uri imageUri; 
private String mPath; 
private ImageView image; 
Bitmap bitmap = null; 
private File tempPhoto; 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    { 
    View v = inflater.inflate(R.layout.fragment_image, container, false); 
    ImageButton snap=((ImageButton)v.findViewById(R.id.snap)); 
    image = ((ImageView) v.findViewById(R.id.image)); 
    snap.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View arg0) { 

       Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
       File imagePath; 

       try 
       { 
        tempPhoto = createTemporaryFile("picture", ".png"); 
        tempPhoto.delete(); 
       } 

       catch(Exception e) 
       { 

        return ; 
       } 

       imageUri = Uri.fromFile(tempPhoto); 
       cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri); 
       startActivityForResult(cameraIntent, 1); 

      } 
     }); 

    return v; 
    } 

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

    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     // super.onActivityResult(requestCode, resultCode, data); 
     ContentResolver cr = getActivity().getContentResolver(); 
     try { 
      cr.notifyChange(imageUri, null); 
      File imageFile = new File(tempPhoto.getAbsolutePath()); 


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

      Bitmap photo=null; 
      if (resultCode == 1) { 
       try { 
        photo = android.provider.MediaStore.Images.Media.getBitmap(cr, Uri.fromFile(tempPhoto)); 
        image.setImageBitmap(photo); 
       } catch (FileNotFoundException e) { 

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

        e.printStackTrace(); 
       } 
      } 

    } 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
    } 

    @Override 
    public void onLowMemory() { 
     // TODO Auto-generated method stub 
     super.onLowMemory(); 
    } 

}

답변

1

당신은 Activity.RESULT_OK로 '의 resultCode를'비교 (= - 1) onActivityResult를 기능을한다.

Replace: (의 resultCode가 == 1) by: (의 resultCode == Activity.RESULT_OK)

+0

덕분 일 경우 만약 – gazubi

관련 문제