2015-04-29 28 views
0

我似乎无法用相机拍摄照片,然后将其设置到imageview中。我尝试了许多不同的方式,但我要么收到错误或图像视图不会显示我拍的照片。我会后我此刻已编码使用碎片拍照并在imageview中设置它的错误

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_snap, container, false); 

    ImageView imageview = (ImageView) view.findViewById(R.id.camera_display); 
    Button camera_btn = (Button) view.findViewById(R.id.camera_btn); 


    camera_btn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 

      getActivity().startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE); 
      } 

    }); 

    return view; 
} 

那么对于onActivityResult

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE && resultCode == getActivity().RESULT_OK) { 
     Bundle extras = data.getExtras(); 
     Bitmap imageBitmap = (Bitmap) extras.get("data"); 

     imageview.setImageBitmap(imageBitmap); 
    } 
} 

编辑 我现在收到此错误

04-29 19:34:14.946 32713-32713/com.example.tim.cryptpix_10 W/Bundle﹕ Key output expected Parcelable but value was a java.lang.Integer. The default value <null> was returned. 

回答

0

这里是我的做过:

public class MainA ctivity延伸活动{

public final static int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 1034; 
public String photoFileName = "photo.jpg"; 
public final String APP_TAG = "MyCustomApp"; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    intent.putExtra(MediaStore.EXTRA_OUTPUT, getPhotoFileUri(photoFileName)); // set the image file name 
    // Start the image capture intent to take photo 
    startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 
    if (id == R.id.action_settings) { 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) { 
     if (resultCode == RESULT_OK) { 
     Uri takenPhotoUri = getPhotoFileUri(photoFileName); 
     // by this point we have the camera photo on disk 
     Log.d("MainActivity photo path",takenPhotoUri.getPath()); 
     Bitmap takenImage = BitmapFactory.decodeFile(takenPhotoUri.getPath()); 
     // Load the taken image into a preview 
     ImageView ivPreview = (ImageView) findViewById(R.id.ivPreview); 
     ivPreview.setImageBitmap(takenImage); 
     } else { // Result was a failure 
      Toast.makeText(this, "Picture wasn't taken!", Toast.LENGTH_SHORT).show(); 
     } 
    } 
} 

// Returns the Uri for a photo stored on disk given the fileName 
public Uri getPhotoFileUri(String fileName) { 
    // Get safe storage directory for photos 
    File mediaStorageDir = new File(
     Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), APP_TAG); 

    // Create the storage directory if it does not exist 
    if (!mediaStorageDir.exists() && !mediaStorageDir.mkdirs()){ 
     Log.d(APP_TAG, "failed to create directory"); 
    } 

    // Return the file target for the photo based on filename 
    return Uri.fromFile(new File(mediaStorageDir.getPath() + File.separator + fileName)); 
} 

}

+0

谢谢回答!我已经实现了你的代码,我没有得到任何错误,但我的图像视图仍然不显示任何东西?如果有任何帮助,我正在分段工作 – Timhua

相关问题