9 Replies - 1055 Views - Last Post: 22 January 2017 - 11:47 AM

#1 comparter   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • Joined: 05-September 16

How to get the photo from the intent and put it in a profile picture

Posted 21 January 2017 - 01:28 PM

I've been trying to do that for hours, I've read the tutorial from the Android Developers website:
https://developer.an...hotobasics.html
But I can't understand how to combine this tutorial, with adding a profile picture for my app? (I want there to be a square / circle that I can click on and change a profile picture, like in WhatsApp for example)

I'm a newbie with this, would love to get some light shed on that!

*Edit: I can't even find a circle button in the layout xml to match that? (Those blank circles that change to the picture upon uploading)

Thanks

This post has been edited by comparter: 21 January 2017 - 01:42 PM


Is This A Good Question/Topic? 0
  • +

Replies To: How to get the photo from the intent and put it in a profile picture

#2 EndLessMind   User is offline

  • Android Expert
  • member icon

Reputation: 273
  • View blog
  • Posts: 1,252
  • Joined: 13-March 09

Re: How to get the photo from the intent and put it in a profile picture

Posted 21 January 2017 - 05:26 PM

There are not circular buttons or imageviews in the default set of wedigets.
When you see them in apps, they are all custom views.
You can check out my github for a example on custom shaped imageview.
Link is in my signature down below.
Was This Post Helpful? 1
  • +
  • -

#3 comparter   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • Joined: 05-September 16

Re: How to get the photo from the intent and put it in a profile picture

Posted 22 January 2017 - 02:52 AM

Thank you!
Although I am still struggling with simply making my button launch the camera app, could you help?
here is the code (Taken from the Android Developer website. In the logcat I am getting "Could not find method dispatchTakePictureIntent(View) in a parent or ancestor Context for android:onclick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'take_photo'"

Here is the code:

MainActivity.java:

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MainActivity extends AppCompatActivity {
    static final int REQUEST_TAKE_PHOTO = 1;
    String mCurrentPhotoPath;
    private Bitmap mImageBitmap;
    private ImageView mImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    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) {
                Uri photoURI = FileProvider.getUriForFile(this,
                        "com.example.android.fileprovider",
                        photoFile);
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
            }
        }
    }


    private File createImageFile() throws IOException {
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(
                imageFileName,  /* prefix */
                ".jpg",         /* suffix */
                storageDir      /* directory */
        );

        // Save a file: path for use with ACTION_VIEW intents
        mCurrentPhotoPath = image.getAbsolutePath();
        return image;
    }
}



And here is the button xml:
 <Button
        android:text="Take Photo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="72dp"
        android:id="@+id/take_photo"
        android:onclick="dispatchTakePictureIntent" />

This post has been edited by comparter: 22 January 2017 - 02:54 AM

Was This Post Helpful? 0
  • +
  • -

#4 EndLessMind   User is offline

  • Android Expert
  • member icon

Reputation: 273
  • View blog
  • Posts: 1,252
  • Joined: 13-March 09

Re: How to get the photo from the intent and put it in a profile picture

Posted 22 January 2017 - 02:59 AM

Have you tried changing dispatchTakePictureIntent() to dispatchTakePictureIntent(View v) ?
Was This Post Helpful? 1
  • +
  • -

#5 comparter   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • Joined: 05-September 16

Re: How to get the photo from the intent and put it in a profile picture

Posted 22 January 2017 - 03:05 AM

yes just tried, still the same.
I get too many errors it seems.
Also, in the xml, I see "method 'dispatchTakePictureIntent' in 'MainActivity' has incorrect signature", and in the MainActivity.java the dispatchTakePictureIntent method is grayed out showing "dispatchTakePictureIntent is never used"?

There must be more things wrong in what Im doing
Was This Post Helpful? 0
  • +
  • -

#6 EndLessMind   User is offline

  • Android Expert
  • member icon

Reputation: 273
  • View blog
  • Posts: 1,252
  • Joined: 13-March 09

Re: How to get the photo from the intent and put it in a profile picture

Posted 22 January 2017 - 03:11 AM

dispatchTakePictureIntent must be public also.
So it should be public void dispatchTakePictureIntent(View v)

If you only refer to dispatchTakePictureIntent from the xml, then the java editor will see it as unused , but that just a warning.
This hard to make an application completely without warnings, in most cases you'll be find.
You should really only watch out for stuff like deprecated and performance warnings and stuff like that.
A few "unused" methods is nothing to worry about.
Was This Post Helpful? 0
  • +
  • -

#7 comparter   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • Joined: 05-September 16

Re: How to get the photo from the intent and put it in a profile picture

Posted 22 January 2017 - 03:14 AM

Thank you! I was about to comment here that I found out that it was private instead of public for an onclick usage.
I did that, but now I get the following logcat:

java.lang.IllegalStateException: Could not execute method for android:onclick
at android.support.v7.app.AppCompatViewInflater$DeclaredonclickListener.onclick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:4764)
at android.view.View$PerformClick.run(View.java:19844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5349)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.support.v7.app.AppCompatViewInflater$DeclaredonclickListener.onclick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:4764) 
at android.view.View$PerformClick.run(View.java:19844) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5349) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703) 
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference
at android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:583)
at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:557)
at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:399)
at com.test.testapp.MainActivity.dispatchTakePictureIntent(MainActivity.java:55)
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at android.support.v7.app.AppCompatViewInflater$DeclaredonclickListener.onclick(AppCompatViewInflater.java:288) 
at android.view.View.performClick(View.java:4764) 
at android.view.View$PerformClick.run(View.java:19844) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5349) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703) 


*I'm just a beginner so I have no idea how to solve these, or how to approach them?

This post has been edited by comparter: 22 January 2017 - 03:18 AM

Was This Post Helpful? 0
  • +
  • -

#8 EndLessMind   User is offline

  • Android Expert
  • member icon

Reputation: 273
  • View blog
  • Posts: 1,252
  • Joined: 13-March 09

Re: How to get the photo from the intent and put it in a profile picture

Posted 22 January 2017 - 10:42 AM

Something is wrong with your fileprovider.
Have you set it up correctly?
Here is how you do that.
Was This Post Helpful? 1
  • +
  • -

#9 comparter   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • Joined: 05-September 16

Re: How to get the photo from the intent and put it in a profile picture

Posted 22 January 2017 - 11:24 AM

It is working now, it was the fileprovider, thank you! (I am studying new things along the way, I still don't really know the meaning of file provider, but merely did a copy-paste job)

This post has been edited by comparter: 22 January 2017 - 11:25 AM

Was This Post Helpful? 0
  • +
  • -

#10 EndLessMind   User is offline

  • Android Expert
  • member icon

Reputation: 273
  • View blog
  • Posts: 1,252
  • Joined: 13-March 09

Re: How to get the photo from the intent and put it in a profile picture

Posted 22 January 2017 - 11:47 AM

Glad that I can be of help!

I find that the best way to learn is by doing.
Copy-paste is not the best way, if you where to manually "copy" the code by
writing it by hand instead that would at least make you read it.
Even if you don't understand it, at that point, at least you have read it.
So when you go to look the functions and methods up (as well as lifecycles and more) you
will be familiar with more of what the documentation is referring to, like contexts and interfaces and more.


Good luck with all your future endeavors!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1