10

my question is: how to create imageButton which allow the user to upload image from their phone and insert it in the application as a picture profile? like whatsapp for example it allow the user to choose image from the phone and set that as a picture profile.

Thanks

Mack
  • 335
  • 2
  • 5
  • 12

2 Answers2

8

Here are following links..

create image button

upload image

example 1

example 2

example 3

jpthesolver2
  • 1,009
  • 1
  • 11
  • 21
Janmejoy
  • 2,723
  • 1
  • 17
  • 38
  • 1
    Thanks for the links. i managed to run the codes in the examples that you have provided. How about if an application required a user to sign-up for an account and the user has to insert image as a profile picture, so once the user login to his account the user can see the profile picture. So how that can be done? Similar like whatsapp; how do they store the pictures that the user upload to the profile? – Mack Jan 24 '13 at 02:48
  • @Mack you mean upload pictures it can be done in several ways,like parsing from facebook or twitter or from gallery – Janmejoy Jan 24 '13 at 04:54
  • 2
    i have an app that ask user to insert a picture for the profile. my question is: where do i need to store the user picture? MySQL DB, Sqlite DB, internal storage or external storage? – Mack Jan 25 '13 at 04:25
1

MY XML FILE

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent">

<ImageView
    android:id="@android:id/icon"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:onClick="selectImage"
    />

MY FILE

public class Test extends AppCompatActivity {
private static final int SELECT_PICTURE = 0;
private ImageView imageView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);
    imageView = (ImageView) findViewById(android.R.id.icon);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        Bitmap bitmap = getPath(data.getData());
        imageView.setImageBitmap(bitmap);
    }
}

private Bitmap getPath(Uri uri) {

    String[] projection = {MediaStore.Images.Media.DATA};
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    String filePath = cursor.getString(column_index);
   // cursor.close();
    // Convert file path into bitmap image using below line.
    Bitmap bitmap = BitmapFactory.decodeFile(filePath);

    return bitmap;
}

private void selectImage() {

    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);
}

}
Sabby
  • 393
  • 2
  • 15