在片段中使用Android应用拍照

时间:2015-05-20 14:22:26

标签: android android-fragments android-camera

我正在开发一个Android应用程序,我需要启用它来拍照,我在这里阅读了许多类似的问题以及网上的许多链接,但似乎有很多方法可以做到这一点,此外我我在Fragment内写了大部分代码,所以这些链接都没有对我有用

这是我的代码

public class MyActivity extends ActionBarActivity {
 /*
  some codes here
 */  
 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new MyFragment())
                .commit();
    }
}

 @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);
    }
}


private File createImageFile() throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(
        Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
    imageFileName, 
    ".jpg",         
    storageDir     
);
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
return image;
  }

顺便说一下,我复制并粘贴了此链接中的onActivityResultcreateImageFile代码 http://developer.android.com/training/camera/photobasics.html

在我的MyFragment课程中我有

    public static class MyFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
       rootView = inflater.inflate(R.layout.my_activity, container, false);

       cameraButton = (ImageButton)rootView.findViewById(R.id.cameraButton);

        cameraButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent takePictureIntent = new     Intent(MediaStore.ACTION_IMAGE_CAPTURE);

                startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);

            }
        });

问题 现在我不知道应该如何从createImageFile OnActivityResult内的MyActivity调用这些方法MyFragment<?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <system.serviceModel> <bindings> <basicHttpsBinding> <binding name="WS_CubicusMobileSOAPBinding"> <security mode="Transport"> <transport clientCredentialType="Basic" proxyCredentialType="Basic" realm="xxxxxx.xx.xx" /> </security> </binding> </basicHttpsBinding > </bindings> <client> <endpoint address="https://xxx.xxx.xx/xxx/awws/xxxxx.awws" binding="basicHttpsBinding" bindingConfiguration="WS_CubicusMobileSOAPBinding" contract="ServiceReference1.WS_CubicusMobileSOAPPortType" name="WS_CubicusMobileSOAPPort" /> </client> </system.serviceModel> </configuration> 来让它们一起工作,换句话说,如何访问存储的图像里面片段。

如果我必须提供有关我的代码的更多详细信息,请告知我们

3 个答案:

答案 0 :(得分:1)

@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);
    }
}

将这段代码写在MyFragment上,并注明了Activity

答案 1 :(得分:0)

将此类添加到片段中,让片段处理结果。

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
}

答案 2 :(得分:0)

关于你的第一个问题,&#34;如何使用createImageFile()函数?&#34;

createImageFile()函数是为Camera应用程序创建一个文件来写入数据,createImageFile函数的返回值应该放入你作为参数广播的intent中,如下所示:

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // Ensure that there's a camera activity to handle the intent
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        // Create the File where the photo should go
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            // Error occurred while creating the File
            ...
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                    Uri.fromFile(photoFile));
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
        }
    }
}

对于你的第二个问题,&#34;如何在片段中获得结果?&#34; startActivityForResult()和onActivityResult()应该足够了。

顺便说一下,也许你可以让活动充当委托来处理startActivityForResult()和onActivityResult()调用片段的事情,并使用事件监听器(由片段实现)来通知图片是准备就绪,如下所示。

interface PictureDelegate{

     void requestToTakePicture( PictureEventListener listener);
}

interface PictureEventListener {

     void onPictureIsReady(int status, Uri fileUri);
}