如何从片段中的onActivityResult获取结果?

我已经在每个项目中使用了Navigation drawer点击我已经调用了Fragments因此在一个项目中我已经在这个片段中调用了一个Fragment ,我需要从相机获取图片并将其设置为canvas background 。 在这里我捕获了相机图片,但不知道如何在拍摄后获得这张照片并将其设置为canvas背景。

片段代码

 import android.content.ContentValues; import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.support.v4.app.Fragment; import android.util.DisplayMetrics; import android.util.Log; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.ImageButton; import android.widget.LinearLayout; import android.widget.Toast; import com.ssoft.admin.code.SharedPreferenceStore; import com.ssoft.admin.code.Tools; import com.ssoft.admin.salesmateco.FragSiteInspectionAdditional; import com.ssoft.admin.salesmateco.R; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class FragSignature extends Fragment implements View.OnClickListener { Button mSIBtnCamera; Fragment fragment; Tools mTools; private static final int RESULT_OK = 1; private static final int RESULT_CANCELED = 0; Uri imageUri = null; final int CAMERA_DATA = 100, INTENT_DATA = 1; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate( R.layout.frag_site_inspection_signature, null); mSIBtnCamera = (Button) rootView.findViewById(R.id.camera); mSIBtnCamera.setOnClickListener(this); return rootView; } @Override public void onClick(View v) { if (v.getId() == R.id.camera) { captureImage(); } else { Toast.makeText(getActivity().getApplicationContext(), "FragSIPhotos Add Button OnClick", Toast.LENGTH_SHORT) .show(); } } public void captureImage() { // Define the file-name to save photo taken by Camera activity String fileName = "Images.jpg"; // Create parameters for Intent with filename ContentValues values = new ContentValues(); values.put(MediaStore.Images.Media.TITLE, fileName); values.put(MediaStore.Images.Media.DESCRIPTION, "Image capture by camera"); // imageUri is the current activity attribute, define and save it for // later usage Uri imageUri = getActivity().getApplicationContext() .getContentResolver() .insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); /**** * EXTERNAL_CONTENT_URI : style URI for the "primary" external storage * volume. ****/ // Standard Intent action that can be sent to have the camera // application capture an image and return it. Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, INTENT_DATA); Log.e("captureImage()", "state -1"); getActivity().startActivityForResult(intent, CAMERA_DATA); Log.e("captureImage()", "end"); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { Log.e("OnActivityResult()", "1"); if (requestCode == CAMERA_DATA) { Log.e("OnActivityResult()", "2"); if (resultCode == RESULT_OK) { // Image captured and saved to fileUri specified in the Intent Log.e("OnActivityResult()", "3"); } else if (resultCode == RESULT_CANCELED) { // User cancelled the image capture Log.e("OnActivityResult()", "4"); } else { // Image capture failed, advise user Log.e("OnActivityResult()", "5"); } } Log.e("OnActivityResult()", "6"); super.onActivityResult(requestCode, resultCode, data); Log.e("OnActivityResult()", "7"); } } 

  • 在活动之间保留片段状态
  • 在片段中调用getSystemServices时未定义?
  • ScrollView中的视图与配置的父级不匹配
  • 如何刷新FragmentPagerAdapter的片段?
  • 通过触摸到下面的视图
  • Android片段工厂方法vs构造函数重载
  • 保留活动之间的片段状态
  • 带有ChildFragmentManager的FragmentStatePagerAdapter - FragmentManagerImpl.getFragment导致NullPointerException
  • 在Activity类中:

     @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode,resultCode,data); } 

    在片段中:

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

    选项1 :

    如果你从片段中调用startActivityForResult() ,那么你应该调用startActivityForResult()而不是getActivity().startActivityForResult() ,因为它会导致片段onActivityResult()

    如果你不确定你在哪里调用startActivityForResult()以及你将如何调用方法。

    选项2:

    由于Activity获取onActivityResult()的结果,您将需要覆盖活动的onActivityResult()并调用super.onActivityResult()以传播到未处理的结果代码或所有的相应片段。

    如果以上2个选项不起作用,请参考选项3,因为它肯定会起作用。

    选项3:

    从fragment到onActivityResult函数的显式调用如下

    在Parent Activity类中,覆盖onActivityResult()方法,甚至在Fragment Class中覆盖它,并调用以下代码。

    在活动中:

     @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { Fragment fragment = getSupportFragmentManager().findFragmentById("yourFragment"); fragment.onActivityResult(requestCode, resultCode, data); } 

    在片段中:

     @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { //in fragment class callback } 

    更换

     getActivity().startActivityForResult(intent, CAMERA_DATA); 

     startActivityForResult(intent, CAMERA_DATA); 

    onActivityResult应该在Activity中实现,这个类是你的Fragment。
    在FragmentActivity中应用onActivityResult