I am currently designing an android app that requires at a certain point for the user to select a picture either from the camera or from the phone's gallery. After that I need to display said picture in the activity's image view.
I think the code section that gets the picture is ok, as I can effectively pick the photo from both options. But after that, besides having no picture shown, the src logo of the image view dissapears. I do not understand if I'm doing anything wrong or if there is something missing here, as it is a non-fatal error and the app continues to perform.
So here is my XML layout of the activity:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.LinearLayoutCompat
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
style="@style/Widget.Design.CoordinatorLayout"
android:orientation="vertical"
tools:context="pt.unl.fct.di.www.myapplication.ReportActivity">
<ScrollView
android:id="@+id/report_form"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/submit_report_form"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="1">
<TextView
android:id="@+id/type_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="30dp"
android:text="@string/select_report_type"
android:visibility="visible" />
<Spinner
android:id="@+id/report_type_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:layout_marginBottom="30dp"/>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/description"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Description"
android:inputType="textMultiLine"
android:selectAllOnFocus="false"
android:layout_marginBottom="30dp"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Location"
android:layout_marginBottom="30dp"/>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Map"
android:layout_marginBottom="30dp"/>
</android.support.design.widget.TextInputLayout>
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="200dp"
android:src="@android:drawable/ic_menu_camera"
android:adjustViewBounds="true"
/>
<Button
android:id="@+id/camera_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add Picture" />
<Button
android:id="@+id/submit_report_button"
style="?android:textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="@string/action_submit_report"
android:textStyle="bold"
android:layout_weight="0.68" />
</LinearLayout>
</ScrollView>
And the Activity's java(important stuff):
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_report);
Spinner spinner = (Spinner) findViewById(R.id.report_type_spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.report_types, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
mReportType = spinner.getItemAtPosition(0).toString();
spinner.setOnItemSelectedListener(this);
mDescription = (EditText)findViewById(R.id.description);
mImageLocationView =(ImageView) findViewById(R.id.imageView);
Button cameraButton = (Button)findViewById(R.id.camera_button);
cameraButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle(R.string.choose_photo_from)
.setItems(R.array.picture_types, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// The 'which' argument contains the index position
// of the selected item
switch(which){
case 0: //fotografia
Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePicture, 0);
onPause();
//zero can be replaced with any action code
break;
case 1:
//galeria
Intent pickPhoto = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickPhoto , 1);
//one can be replaced with any action code
break;
}
}
});
builder.create().show();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
BitmapFactory.Options o = new BitmapFactory.Options();
mImageLocationView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
switch(requestCode) {
case 0:
if(resultCode == RESULT_OK){
Bitmap photo = (Bitmap) imageReturnedIntent.getExtras().get("data");
mImageLocationView.setImageBitmap(photo);
}
break;
case 1:
if(resultCode == RESULT_OK){
Bitmap photo = (Bitmap) imageReturnedIntent.getExtras().get("data");
mImageLocationView.setImageBitmap(photo);
}
break;
}
}
I must say, this is my very first android project so I am kind of overwhelmed by so much information and APIS, and also doing a lot of experimenting.
Simply you can add the following code in `
if(resultCode == RESULT_OK){
// the new code
Uri imageUri = data.getData();
String[] filePath = { MediaStore.Images.Media.DATA };
Cursor cursor = getActivity().getContentResolver().query(imageUri, filePath, null, null, null);
cursor.moveToFirst();
String path = cursor.getString(cursor.getColumnIndex(filePath[0]));
cursor.close();
//refresh Image view
updateImageView(path);
}
// the update image method
public void updateImageView(String completePath) {
Picasso picasoo = Picasso.with(getActivity());
String path = "file://" + completePath;
Long tsLong = System.currentTimeMillis();
String ts = tsLong.toString();
picasoo.load(path+"?time="+ts).fit().centerCrop().networkPolicy(OFFLINE).into(imgUserProfile);
}` Picasso is an image management library that make your life easy with lazy loading the image.
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.