Search code examples
javaandroidimageviewandroid-camera2

Displaying captured image in another activity


I am working on a android app and part of it requires the user to take a picture, the moment the user takes the picture it saves it to the file system and start a new activity to display the recent captured picture. The activity starts with no problem except it's blank!

When I hard-code the path into a string by typing the path manually, the image appears, so what's the problem?

This is the method of the saved file and creating the intent;

capturePicture.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        File f = capturePicture();
        Intent intent = new
        Intent(postSharingActivity.this, settingPostParam.class);
        intent.putExtra("picture", f);
        startActivity(intent);
    }
});

This is the other activity and retrieving the image file;

@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting_post_param);
        savedImage = findViewById(R.id.savedImage);
        File pic = (File) getIntent().getExtras().get("picture");
        Uri uri = Uri.fromFile(pic);
        Toast.makeText(settingPostParam.this, "+uri.toString(),Toast.LENGTH_LONG)
            .show();
            if (isStoragePermissionGranted()) {
                Picasso.get().load(uri).into(savedImage);
            } else {
                Toast.makeText(settingPostParam.this, "error", Toast.LENGTH_LONG).show();
            }
        }

[here's what it displays when I take a picture ][1]:

UPDATE: I found the problem, it execute the intent before the capturePicture() complete. It takes the path before even the file is saved! So i changed back to my original way, which is changing views not activities. And added the AsyncTask.

 private class myAsyncTask extends AsyncTask<Void,Void,Void>{
    File f;

    @Override
    protected Void doInBackground(Void... voids) {
         f=  capturePicture();
        return null;
    }
    @Override
    protected  void onPostExecute(Void result){
        String rr = f.getAbsolutePath();
        File g=new File(rr);
        Toast.makeText(postSharingActivity.this,"onBloodyClick: "+g.toString(),Toast.LENGTH_LONG).show();
        Uri uri = Uri.fromFile(g);
        Toast.makeText(postSharingActivity.this,"URI :"+uri.toString(),Toast.LENGTH_LONG).show();
        if(isStoragePermissionGranted()){
            savedImage.setImageURI(uri);
            cameraPreviewView.setVisibility(View.GONE);
            postCaptureView.setVisibility(View.VISIBLE);
        } else{
            Toast.makeText(postSharingActivity.this,"error",Toast.LENGTH_LONG).show();
        }
    }
}

And here is the onClickListener

 capturePicture.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

      new myAsyncTask().execute();


        }

    });

I've tested it and it is the same problem, the Toasts are in this order:

  1. onBloodyClick: /storage/0/emulated/0/DCIM/Camera/IMG_20180604_104845.jpg

  2. URI:file:////storage/0/emulated/0/DCIM/Camera/IMG_20180604_104845.jpg

  3. Saved: /storage/0/emulated/0/DCIM/Camera/IMG_20180604_104845.jpg

    The third one is in the capturePicture() method. What am i doing wrong here? And thanks for replying.


Solution

  • I found it. The problem is in the calling of the methods and returning types.

    The returning type of the method doInBackground and the params type of the two methods, the onPostExecute is going to use the resulting file of the doInBackground method

    private class myAsyncTask extends AsyncTask<File,Void,File>{
    
     @Override
        protected File doInBackground(File... voids) {
         File f = capturePicture();
         return f;
        }
        @Override
        protected  void onPostExecute(File f){
    
            String rr = f.getAbsolutePath();
            File g=new File(rr);
            Toast.makeText(postSharingActivity.this,"onBloodyClick: "+g.toString(),Toast.LENGTH_LONG).show();
            Uri uri = Uri.fromFile(g);
            Toast.makeText(postSharingActivity.this,"URI :"+uri.toString(),Toast.LENGTH_LONG).show();
            if(isStoragePermissionGranted()){
                savedImage.setImageURI(uri);
                cameraPreviewView.setVisibility(View.GONE);
                postCaptureView.setVisibility(View.VISIBLE);
            } else{
                Toast.makeText(postSharingActivity.this,"error",Toast.LENGTH_LONG).show();
            }
    
        } 
    }
    

    And then on the onClickListener, you specify the order

    capturePicture.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
    
          myAsyncTask task=new myAsyncTask();
          task.doInBackground();
          task.execute();
    
    
            }
    
        });