so I am trying to upload an image i pick from gallery into my imageview that I have. But the image never shows up in the imageview? The program doesn't crash or anything. I used a toast message to see if onActivityResult grabbed the image or not, and it did. But it cannot load it into the image view.
Hopefully you guys can help, below is my code.
public class TakePhotoActivity extends AppCompatActivity implements View.OnClickListener {
private View snapButton;
private ImageButton galleryButton;
private int RESULT_LOAD_IMAGE = 1;
private String user_image_path;
private boolean pickedCamera = false;
private ImageView myImageView;
private ProgressBar myprogressBar;
//GPS
private LocationManager locationManager = null;
private LocationListener locationListener = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_photo);
setupCamera();
}
private void setupCamera()
{
snapButton = (View) findViewById(R.id.cameraActivity_CameraButton);
snapButton.setOnClickListener((View.OnClickListener) this);
snapButton.setOnClickListener(this);
galleryButton = (ImageButton) findViewById(R.id.cameraActivity_GalleryButton);
galleryButton.setOnClickListener(this);
myprogressBar = (ProgressBar) findViewById(R.id.myPB);
myprogressBar.setVisibility(View.INVISIBLE);
myImageView = (ImageView) findViewById(R.id.cameraActivity_ImageView);
}
private void grabImageFromGallery()
{
Intent imageGetter = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(imageGetter, RESULT_LOAD_IMAGE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data)
{
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};//Array size of 1, and we put in a string
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
user_image_path = cursor.getString(columnIndex);//here we have our image path.
cursor.close();
}
Picasso.with(this).load(user_image_path).fit().centerCrop().into(myImageView);
}
@Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.cameraActivity_GalleryButton:
grabImageFromGallery();
break;
}
}
}
Here is my xml.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:layout_height="wrap_content"
android:layout_width="match_parent"
layout="@layout/toolbar_layout">
</include>
<ImageView
android:layout_width="200dp"
android:layout_height="150dp"
android:id="@+id/cameraActivity_ImageView"
android:layout_marginLeft="100dp"
android:layout_marginTop="50dp"/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageButton
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@drawable/circle_fb_2"
android:src="@drawable/icon_camera_red"
android:layout_marginTop="70dp"
android:layout_marginLeft="70dp"
android:id="@+id/cameraActivity_CameraButton"/>
<ImageButton
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@drawable/circle_fb_2"
android:src="@drawable/icon_gallery"
android:layout_marginTop="70dp"
android:layout_marginLeft="150dp"
android:layout_alignLeft="@id/cameraActivity_CameraButton"
android:id="@+id/cameraActivity_GalleryButton"/>
<ProgressBar
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/myPB"
android:layout_below="@id/cameraActivity_CameraButton"
android:layout_marginLeft="170dp"/>
</RelativeLayout>
</LinearLayout>
While loading local image you have path should be like this "file:///image_path.jpg".
Replace
Picasso.with(this).load(user_image_path).fit().centerCrop().into(myImageView);
with
Picasso.with(this).load("file:///"+user_image_path).fit().centerCrop().into(myImageView);