简体   繁体   中英

Android onActivityResult

I am stuck in a situation, which is described as follows in terms of my app chart flow :

Suppose I am on Activity A, Here at the onclick of a button I pick the images from the gallery and after picking, I am moving to the Activity B ( it comes internally from onActivity result - by passing the Intent ). Now here when I am on Activity B and click on a Button, there is a call to the Custom Video maker. Here I capture a video for 10 secs and then I finished ( Activity C) using

C.this.finish();

Now as per the Android Activity Flow I SHOULD MOVE BACK TO THE ACTIVITY B, But in my case I am getting one black Screen for few seconds and then getting Activity A, instead of Activity B. I can't disclose my code, so I am not attaching it. Sorry for the same. If anybody can help me with this much of info, Please help me

code:

Activity B code snippet where the button is clicked :

 btnTakeVideo.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {                
                Intent takePictureIntent = new Intent(TakeAnotherPhoto.this,Recorder.class);

                startActivityForResult(takePictureIntent,CreateAuctionScreen.ACTION_TAKE_VIDEO);
            }
        });

here is the onActivityResult

onActivityResult()
if(requestCode == CreateAuctionScreen.ACTION_TAKE_VIDEO) {
            if (resultCode == RESULT_OK) {
                if(CreateAuctionScreen.resetBitmap!=null)
                {                
                    CreateAuctionScreen.resetBitmap = null;
                }                
                Drawable dd;
                dd = getResources().getDrawable(R.drawable.list_pic_frame2x);
                CreateAuctionScreen.bmpSize = ((BitmapDrawable)dd).getBitmap();

                if(data != null){


                    Uri str = data.getData();
                    String path = getRealPathFromURI(str);
                    System.out.println(path);

                    Bitmap bmp = ThumbnailUtils.createVideoThumbnail(path, MediaStore.Images.Thumbnails.MINI_KIND);
                    CreateAuctionScreen.resetBitmap = Bitmap.createScaledBitmap(bmp, CreateAuctionScreen.bmpSize.getWidth(), CreateAuctionScreen.bmpSize.getHeight(), true);

                    if(CreateAuctionScreen.resetBitmap != null){

                        if(CreateAuctionScreen.thumbList.size() < 20){
                            dpv.set_chkString("Video");
                            dpv.get_chkString();
                            CreateAuctionScreen.chkList.add(dpv);
                            CreateAuctionScreen.str_thumbList.add(path);
                            CreateAuctionScreen.thumbList.add(CreateAuctionScreen.resetBitmap);

                            hl.setAdapter(new GallaryImageAdapter(TakeAnotherPhoto.this,CreateAuctionScreen.thumbList.size(),ImageAdapter.CREAT_AUCTION_SCREEN, CreateAuctionScreen.thumbList));
                        }else{
                            Toast.makeText(TakeAnotherPhoto.this, "You can add up to 20 pics & videos only", Toast.LENGTH_SHORT).show();
                        }
                    }
                }

Activity C (Custome Video Recorder )

public class Recorder extends Activity implements SurfaceHolder.Callback{

    Button myButton;
    MediaRecorder mediaRecorder;
    SurfaceHolder surfaceHolder;
    boolean back;
    Timer timer;
    int counter = 1;
    String strPath;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        back = false;

        mediaRecorder = new MediaRecorder();
        setContentView(R.layout.recorde);
        initMediaRecorder();

        timer = new Timer();
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                if(counter<=10)
                    counter++;
                //Android UI get Updated continouly
                else {
                    // If condition full filled the timer will stop here
                    mediaRecorder.stop();
                    mediaRecorder.release();
                    timer.cancel();
                    Intent i = new Intent();
                    setResult(RESULT_OK, i.putExtra("returnedVideo", strPath));

                    Recorder.this.finish();

                }
            } 
        };
        timer.schedule(timerTask, 1000, 1000);

        SurfaceView myVideoView = (SurfaceView)findViewById(R.id.videoview);
        surfaceHolder = myVideoView.getHolder();
        surfaceHolder.addCallback(this);
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        myButton = (Button)findViewById(R.id.mybutton);
        myButton.setOnClickListener(myButtonOnClickListener);
    }

    private Button.OnClickListener myButtonOnClickListener
    = new Button.OnClickListener(){

        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            mediaRecorder.stop();
            mediaRecorder.release();
            timer.cancel();

            Intent i = new Intent();
            setResult(RESULT_OK, i.putExtra("returnedVideo", strPath));
//            i.putExtra("returnedVideo", strPath);
//            i.putExtra("isRecorder", true);
//            startActivity(i);
            finish();
        }};

        public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
            // TODO Auto-generated method stub

        }
        public void surfaceCreated(SurfaceHolder arg0) {
            // TODO Auto-generated method stub
            prepareMediaRecorder();
        }
        public void surfaceDestroyed(SurfaceHolder arg0) {
            // TODO Auto-generated method stub

            mediaRecorder.stop();
            mediaRecorder.release();
            //Recorder.this.finish();
        }

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

            mediaRecorder.stop();
            mediaRecorder.release();
        }

        private void initMediaRecorder(){
            Random genraotr = new Random();
            int n = 10000;
            n = genraotr.nextInt(n);            

            mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
            mediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
            CamcorderProfile camcorderProfile_HQ = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
            mediaRecorder.setProfile(camcorderProfile_HQ);
            mediaRecorder.setOutputFile("/sdcard/auction_video"+n+".mp4");
            strPath = "/sdcard/auction_video"+n+".mp4";
            mediaRecorder.setMaxDuration(10000); // Set max duration 10 sec.
            mediaRecorder.setMaxFileSize(5000000); // Set max file size 5M
        }

        private void prepareMediaRecorder(){
            mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());
            try {
                mediaRecorder.prepare();
                mediaRecorder.start();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }


        }
}

Thanks

它似乎是活动B中的一个例外。正如大家所建议的那样,在B中调试OnActivityResult函数。检查你是否正在检索函数中'System.out.println(path)'打印的正确路径。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM