我目前正在設計一款Android應用,需要在某一點,爲用戶無論是從相機或從手機圖庫中選擇圖片拍攝的圖像。之後,我需要在活動的圖片視圖中顯示所述圖片。ImageView的不顯示從手機相機或照片庫
我認爲獲取圖片的代碼段是可以的,因爲我可以從兩個選項中有效地選擇照片。但之後,除了沒有顯示圖片之外,圖像視圖的src徽標消失。我不明白我是否做錯了什麼,或者是否有什麼遺漏,因爲這是一個非致命錯誤,應用程序會繼續執行。
因此,這裏的活動我的XML佈局:
<?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>
以及活動的Java(重要的東西):
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;
}
}
我必須說,這是我的第一個android項目,所以我被這麼多的信息和APIS所壓倒,也做了很多實驗。
'ACTION_PICK'不使用''data''extra。你得到的'Uri'('imageReturnedIntent.getData()')指向圖像。使用圖像加載庫(Glide,Picasso等)將其填充到「ImageView」中。 – CommonsWare