Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android getActivityResult()方法在Intent参数中提供null_Android_Android Intent_Android Fragments - Fatal编程技术网

Android getActivityResult()方法在Intent参数中提供null

Android getActivityResult()方法在Intent参数中提供null,android,android-intent,android-fragments,Android,Android Intent,Android Fragments,早上好,先生,我用过导航抽屉和碎片。我正在使用ActionBarActivity,我在其中调用了这个片段。在我的片段中,我从相机捕获图像,因此在getActivityResult()方法中,我没有给出意图值 public class CameraDemo extends Fragment implements View.OnClickListener { ImageButton mImgBtnActivityBack,mImgBtnActivityWrong,mImgBtnActivit

早上好,先生,我用过导航抽屉和碎片。我正在使用
ActionBarActivity
,我在其中调用了这个片段。在我的片段中,我从相机捕获图像,因此在
getActivityResult()
方法中,我没有给出意图值

public class CameraDemo extends Fragment implements View.OnClickListener {
    ImageButton mImgBtnActivityBack,mImgBtnActivityWrong,mImgBtnActivityRight ,mImgBtnActivityEdit ,mImgBtnActivitySearch ,mImgBtnActivityAdd ,mImgBtnActivityNext,mImgBtnActivitySetting;
    LinearLayout mLinearLayout1,mLinearLayout2;
    Button mFragSignatureClear,mBtnBlack,mBtnRed,mSIBtnSubmit,mSIBtnCamera,mSIBtnAdd ;
    PaintView mPaintView;
    int currrID = 0;
    android.support.v4.app.Fragment fragment;
    public int st = 3;
    int sizex, sizeh;
    Paint paint = new Paint() ;
    Uri imageUri=null ;
    final int CAMERA_DATA = 1888, INTENT_DATA = 1 ;
    Bitmap mbkground ;
    public CameraDemo() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        mbkground = null ;
        mLinearLayout2=(LinearLayout) rootView.findViewById(R.id.draw);
        mPaintView = new PaintView(getActivity().getApplicationContext());
        mLinearLayout2.addView(mPaintView);
        mFragSignatureClear=(Button) rootView.findViewById(R.id.fragSignatureClear);
        mFragSignatureClear.setOnClickListener(this);
        mBtnBlack=(Button)rootView.findViewById(R.id.black);
        mBtnBlack.setOnClickListener(this);
        mBtnRed=(Button)rootView.findViewById(R.id.red);
        mBtnRed.setOnClickListener(this);
        mSIBtnSubmit=(Button) rootView.findViewById(R.id.fragSIBtnSubmit);
        mSIBtnSubmit.setOnClickListener(this);
        mSIBtnCamera=(Button) rootView.findViewById(R.id.camera) ;
        mSIBtnCamera.setOnClickListener(this) ;
        mSIBtnAdd = (Button) rootView.findViewById(R.id.add) ;
        mSIBtnAdd . setOnClickListener( this );
        DisplayMetrics m = this.getResources().getDisplayMetrics();
        float density = getResources().getDisplayMetrics().density;

        int w = m.widthPixels;
        int h = m.heightPixels;

        sizex = w;
        sizeh = h;

        Log.i("hi", "" + sizex);
        Log.i("hi", "" + sizeh);
        Log.i("hi", "" + density);
        float fsize = sizex / density;

        return rootView;
    }
    @Override
    public void onClick(View v) {
        if(v.getId()==R.id.black){
            currrID=v.getId();
        }else if(v.getId()==R.id.black){
            currrID=v.getId();
        }else if(v.getId()==R.id.fragSIBtnSubmit){
            mPaintView.toJPEGFile();
            mPaintView.SaveFile();
        }else if(v.getId()==R.id.camera){
                captureImage();
        }else if(v.getId()==R.id.add){
            mPaintView.loadFromFile();
        }

        else {
            Toast.makeText(getActivity().getApplicationContext(), "FragSIPhotos Add Button OnClick", Toast.LENGTH_SHORT).show();

        }
    }
    class Point {
        float x, y;

        public String toString() {
            return x + "," + y;
        }
    }
    public class LineData {
        List<Point> points = new ArrayList<Point>();

        LineData(List<Point> p) {
            points.addAll(p);
        }

        public void Draw(Canvas mCanvas, Paint paint) {
            float fData[] = new float[points.size() * 4];
            int Index = 0;
            for (Point point : points) {
                if (Index > 0) {
                    fData[Index++] = point.x;
                    fData[Index++] = point.y;
                }
                fData[Index++] = point.x;
                fData[Index++] = point.y;
                // canvas.drawCircle(point.x, point.y, 5, paint);

            }
            if (Index > 2)
                mCanvas.drawLines(fData, 0, Index - 2, paint);

        }
    }
    public class PaintView extends View implements View.OnTouchListener {
        private static final String TAG = "PaintView" ;
        Paint paint = new Paint() ;
        int t = 0 ;
        List<Point> points = new ArrayList<Point>() ;
        List<LineData> pointsred = new ArrayList<LineData>() ;
        List<LineData> pointsblack = new ArrayList<LineData>() ;
        public PaintView(Context context) {
            super(context) ;
            setFocusable(true) ;
            setFocusableInTouchMode(true) ;
            List<Point> points = new ArrayList<Point>() ;

            setDrawingCacheEnabled(true) ;
            this.setOnTouchListener(this) ;
            mbkground = BitmapFactory.decodeResource(getResources(),
                    R.drawable.plain) ;

            paint.setAntiAlias(true) ;
            paint.setStrokeMiter(10.0f) ;
            paint.setStrokeWidth(st) ;

            currrID = R.id.red ;

        }
        @Override
        public void onDraw(Canvas mCanvas) {
            mCanvas.drawBitmap(mbkground, 0, 0, null);
            // canvas.drawColor(Color.WHITE) ;
            this.setBackgroundColor(Color.BLACK);
            // To Do Paint for text
            //this code for  my image app
            //yesss very good
        /*    Paint mText= new Paint(Color.RED);
            mText.setTextSize(25);
            mCanvas.drawText("Hiren",100,100,mText);
        */    if (currrID == R.id.red)
                paint.setColor(Color.RED) ;
            else if (currrID == R.id.black)
                paint.setColor(Color.BLACK) ;
            else
                paint.setColor(Color.BLACK) ;

            float fdata[] = new float[points.size() * 4] ;
            int Index = 0 ;
            for (Point point : points) {
                if (Index > 0) {
                    fdata[Index++] = point.x ;
                    fdata[Index++] = point.y ;
                }
                fdata[Index++] = point.x ;
                fdata[Index++] = point.y ;
                // canvas.drawCircle(point.x, point.y, 5, paint);

            }
            if (Index > 2)
                mCanvas.drawLines(fdata, 0, Index - 2, paint);

            paint.setColor(Color.RED) ;
            for (LineData lines : pointsred)
                lines.Draw(mCanvas, paint) ;


            paint.setColor(Color.BLACK) ;
            for (LineData lines : pointsblack)
                lines.Draw(mCanvas, paint) ;

            paint.setColor(Color.WHITE) ;

        }

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
                points.clear() ;
            } else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
                {
                    LineData data = new LineData(points) ;

                    if (currrID == R.id.red)
                        pointsred.add(data) ;
                    else if (currrID == R.id.black)
                        pointsblack.add(data) ;

                    points.clear() ;
                }
            } else {

                Point point = new Point() ;

                point.x = event.getX() ;

                point.y = event.getY() ;

                points.add(point) ;
            }
            invalidate() ;

            return true;

        }
        public void toJPEGFile() {
            File folder = new File(Environment.getExternalStorageDirectory()
                    + "/Mateco/") ;

            if (!folder.exists())
                folder.mkdirs() ;
            try {

                this.setDrawingCacheEnabled(true) ;

                File f = new File(Environment.getExternalStorageDirectory()
                        + "/Mateco/" + "Mateco" + ".png") ;
                // file mFile = new File(Environment.g)
                FileOutputStream fos = new FileOutputStream(f) ;

                Bitmap bitmap = this.getDrawingCache() ;
                bitmap.compress(Bitmap.CompressFormat.PNG, 80, fos) ;
                // 80 quality bet 0-100 define

                fos.flush() ;
                fos.close() ;
                this.setDrawingCacheEnabled(false) ;

            } catch (FileNotFoundException e) {

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

                e.printStackTrace() ;
            }

        }

        public void loadFromFile() {

            FileInputStream in;
            BufferedInputStream buf;

            try {
                in = new FileInputStream(
                        Environment.getExternalStorageDirectory() + "/Mateco/"
                                + "mateco" + ".jpg");
                buf = new BufferedInputStream(in);

                mbkground = BitmapFactory.decodeStream(buf);

                if (in != null) {
                    in.close();
                }
                if (buf != null) {
                    buf.close();
                }
            } catch (Exception e) {
                Log.e("Error reading file", e.toString());
            }
        }

        public void setBitMap(){

           /* mbkground = BitmapFactory.decodeResource(getResources(),
                    R.drawable.background) ;
*/
        }
        public void setBitMap(Bitmap photo){

            mbkground = photo;
        }

        public void SaveFile() {
            Toast.makeText(getActivity().getApplicationContext(),"Save 1",Toast.LENGTH_SHORT).show();
            File folder = new File(Environment.getExternalStorageDirectory()
                    + "/Mateco/");

            if (!folder.exists())
                folder.mkdirs();
            try {
                this.setDrawingCacheEnabled(true);
                t++;
                File f = new File(Environment.getExternalStorageDirectory()
                        + "/Mateco/" + System.currentTimeMillis() + ".jpg");
                FileOutputStream fos = new FileOutputStream(f);
                Bitmap bitmap = this.getDrawingCache();
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
                // 80 quality bet 0-100 define

                fos.flush();
                fos.close();
                File temp = new File(Environment.getExternalStorageDirectory()
                        + "/Mateco/" + "Mateco" + ".png");
                temp.delete();
                this.setDrawingCacheEnabled(false);

            } catch (FileNotFoundException e) {

                Toast.makeText(getActivity().getApplicationContext(),"FILE_NOT_FOUND_EXCEPTION  "+e.getMessage(),Toast.LENGTH_SHORT).show();

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

                Toast.makeText(getActivity().getApplicationContext(),"IO_EXCEPTION  "+e.getMessage(),Toast.LENGTH_SHORT).show();

                e.printStackTrace();
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                Intent mediaScanIntent = new Intent(
                        Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                File f = new File(
                        "file://"
                                + Environment
                                .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES));
                Uri contentUri = Uri.fromFile(f);
                mediaScanIntent.setData(contentUri);
                getActivity().getApplicationContext().sendBroadcast(mediaScanIntent);
                Toast.makeText(getActivity().getApplicationContext(),"Save 9",Toast.LENGTH_SHORT).show();

            } else {
                getActivity().getApplicationContext().sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
                        Uri.parse("file://"
                                + Environment.getExternalStorageDirectory())));
                Toast.makeText(getActivity().getApplicationContext(),"Save 11",Toast.LENGTH_SHORT).show();

            }
        }
    }
    public void captureImage(){
        // Define the file-name to save photo taken by Camera activity

        String fileName = "Mateco.jpg";

        // Create parameters for Intent with filename

        ContentValues values = new ContentValues();

        values.put(MediaStore.Images.Media.TITLE, fileName);
        // Log.e("Intrenal Storage "+MediaStore.Images.Media.INTERNAL_CONTENT_URI.toString(),"");

        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);
      /*  Uri imageUri=getActivity().getApplicationContext().getContentResolver().insert( MediaStore. Images. Media. INTERNAL_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("captureIma   ge()", "end");

    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.e("OnActivityResult()", "1");
        try {
            if (requestCode == CAMERA_DATA && resultCode == getActivity().RESULT_OK) {
                // Image captured and saved to fileUri specified in the Intent
                Log.e("OnActivityResult()", "2");

                InputStream stream =getActivity().getApplicationContext(). getContentResolver().openInputStream(
                        data.getData());
                mbkground = BitmapFactory.decodeStream(stream);
                Log.e("OnActivityResult()", "2.1");

                Bitmap photo = (Bitmap) data.getExtras().get("data");
                Log.e("OnActivityResult()", "2.2");

                mPaintView.setBitMap(photo);
                Log.e("OnActivityResult()", "3");
            } else {
                // Image capture failed, advise user
                Log.e("OnActivityResult()", "5");
            }
        }catch (Exception objException){
            Log.e("onActivityResult();",""+objException.getMessage());
        }
    }
}
公共类CameraDemo扩展片段实现View.OnClickListener{
图像按钮mImgBtnActivityBack、mImgBtnActivityWrong、mImgBtnActivityRight、mImgBtnActivityEdit、mImgBtnActivitySearch、mImgBtnActivityAdd、mimgbtnactivityext、mImgBtnActivitySetting;
线性布局mLinearLayout1、mLinearLayout2;
按钮mFragSignatureClear、mBtnBlack、mBtnRed、mSIBtnSubmit、MSIBTNAMERA、mSIBtnAdd;
PaintView;
int currrID=0;
android.support.v4.app.Fragment片段;
公共int st=3;
int sizex,sizeh;
油漆=新油漆();
Uri-imageUri=null;
最终int摄像机数据=1888,意图数据=1;
位图mbkground;
公共CameraDemo(){
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图根视图=充气机。充气(R.layout.fragment_main,容器,错误);
mbkground=null;
mLinearLayout2=(LinearLayout)rootView.findViewById(R.id.draw);
mPaintView=new PaintView(getActivity().getApplicationContext());
mLinearLayout2.addView(mpainview);
mFragSignatureClear=(按钮)rootView.findViewById(R.id.fragSignatureClear);
mFragSignatureClear.setOnClickListener(此);
mBtnBlack=(按钮)rootView.findviewbyd(R.id.black);
mBtnBlack.setOnClickListener(this);
mBtnRed=(按钮)rootView.findviewbyd(R.id.red);
mBtnRed.setOnClickListener(this);
mSIBtnSubmit=(按钮)rootView.findviewbyd(R.id.fragSIBtnSubmit);
mSIBtnSubmit.setOnClickListener(此);
mSIBtnCamera=(按钮)rootView.findviewbyd(R.id.camera);
mSIBtnCamera.setOnClickListener(此);
mSIBtnAdd=(按钮)rootView.findviewbyd(R.id.add);
mSIBtnAdd.setOnClickListener(此);
DisplayMetrics m=this.getResources().getDisplayMetrics();
浮动密度=getResources().getDisplayMetrics().density;
int w=m.1像素;
int h=m.heightPixels;
sizex=w;
sizeh=h;
Log.i(“hi”和“+sizex”);
Log.i(“hi”,“sizeh”);
对数i(“hi”和“+密度);
浮球尺寸=尺寸/密度;
返回rootView;
}
@凌驾
公共void onClick(视图v){
if(v.getId()==R.id.black){
currrID=v.getId();
}else if(v.getId()==R.id.black){
currrID=v.getId();
}else if(v.getId()==R.id.fragSIBtnSubmit){
mpainview.toJPEGFile();
mpainview.SaveFile();
}else if(v.getId()==R.id.camera){
captureImage();
}else if(v.getId()==R.id.add){
mpainview.loadFromFile();
}
否则{
Toast.makeText(getActivity().getApplicationContext(),“FragSIPhotos添加按钮OnClick”,Toast.LENGTH\u SHORT.show();
}
}
类点{
浮动x,y;
公共字符串toString(){
返回x+“,”+y;
}
}
公共类行数据{
列表点=新的ArrayList();
LineData(列表p){
积分。全部相加(p);
}
公共空白绘制(画布mCanvas、油漆){
浮点fData[]=新浮点[points.size()*4];
int指数=0;
用于(点:点){
如果(索引>0){
fData[Index++]=point.x;
fData[Index++]=点y;
}
fData[Index++]=point.x;
fData[Index++]=点y;
//画布.画圈(点x,点y,5,绘画);
}
如果(索引>2)
mCanvas.绘制线(fData,0,索引-2,绘制);
}
}
公共类PaintView扩展视图实现View.OnTouchListener{
私有静态最终字符串TAG=“PaintView”;
油漆=新油漆();
int t=0;
列表点=新的ArrayList();
List pointsred=new ArrayList();
List pointsblack=new ArrayList();
公共PaintView(上下文){
超级(上下文);
设置聚焦(真);
setFocusableInTouchMode(真);
列表点=新的ArrayList();
setDrawingCacheEnabled(真);
this.setOnTouchListener(this);
mbkground=BitmapFactory.decodeResource(getResources(),
R.可拉深。平面);
paint.setAntiAlias(真);
油漆。调整行程器(10.0f);
油漆。设置行程宽度(st);
currrID=R.id.红色;
}
@凌驾
公共空间onDraw(Canvas mCanvas){
drawBitmap(mbkground,0,0,null);
//画布。drawColor(颜色。白色);
这个.setBackgroundColor(颜色.黑色);
//为文本绘制
//此代码适用于我的图像应用程序
//是的,非常好
/*绘制多行文字=新绘制(颜色为红色);
mText.setTextSize(25);
mCanvas.drawText(“Hiren”,100100,多行文字);
*/如果(currrID==R.id.red)
油漆。设置颜色(颜色。红色);
else if(currrID==R.id.black)
油漆。设置颜色(颜色。黑色);
其他的
油漆。设置颜色(颜色。黑色);
浮点fdata[]=新浮点[points.size()*4];
int指数=0;
用于(点:点){
如果(索引>0){
fdata[Index++]=point.x;
fdata[Index++]=点y;
}
fdata[Index++]=point.x;
fdata[Index++]=点y;
//c
    getActivity().startActivityForResult(intent, CAMERA_DATA);
    // this call your activity onActivityResult 
startActivityForResult(intent, CAMERA_DATA);
 // this call your Fragment onActivityResult 
 @Override
 protected void onActivityResult(int requestId, int responseId, Intent data) {   
   super.onActivityResult(requestId, responseId, data);
 }
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
  //to do
}
public String getFileName(){
    return "file.jpeg" ;
}
public void captureImage(){
    // Define the file-name to save photo taken by Camera activity

    fileName = getFileName();
    Intent mIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    mIntent.putExtra(MediaStore.EXTRA_OUTPUT, getPhotoFileUri(fileName)); // set the image file name
    // Start the image capture intent to take photo
    startActivityForResult(mIntent, CAMERA_DATA);
}
public Uri getPhotoFileUri(String fileName) {
    // Get safe storage directory for photos
    File mediaStorageDir = new File(
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Camera");

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

    // Return the file target for the photo based on filename
    return Uri.fromFile(new File(mediaStorageDir.getPath() + File.separator + fileName));
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_DATA) {
        if (resultCode == getActivity().RESULT_OK ) {
            Uri takenPhotoUri = getPhotoFileUri(fileName);
            // by this point we have the camera photo on disk
            Bitmap photo = BitmapFactory.decodeFile(takenPhotoUri.getPath());
            //   Bitmap photo = (Bitmap) data.getExtras().get("data");
            Log.e("OnActivityResult()", "2.2");

            mPaintView.setBitMap(photo);
            Toast.makeText(getActivity().getApplicationContext(), "Image Set SuccessFully", Toast.LENGTH_SHORT).show();

            // Load the taken image into a preview
           /* ImageView ivPreview = (ImageView) findViewById(R.id.ivPreview);
            ivPreview.setImageBitmap(takenImage);*/
        } else { // Result was a failure
            Toast.makeText(getActivity().getApplicationContext(), "Picture wasn't taken! or data is null", Toast.LENGTH_SHORT).show();
        }
    }
}