【发布时间】:2019-04-20 17:13:46
【问题描述】:
我试图在按下 ImageButton 的位置显示一个对话框,然后从图库中选择图片或从相机中获取一些图片。 我已经在努力选择一个图像并将其插入到正确的图像视图中。
现在,点击ImageButton (android:id="@+id/imageSelect") 会弹出对话框,然后选择从图库中选择,但它会将图像插入到(android:id="@+id/ imageSelect1) 应插入 (android:id="@+id/imageSelect)。 当我选择 (android:id="@+id/imageSelect1) 它只是覆盖图像,所以 (android:id="@+id/imageSelect) 保持为空
我感觉 SelectPhotoDialog 类有问题。
在没有对话框的情况下使用效果很好,但我希望有该选项可以从相机或画廊中进行选择。
这是我的 xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Image 1" />
<ImageButton
android:id="@+id/imageSelect"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"
android:background="@android:color/white"
android:scaleType="centerCrop"
/>
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Image 2" />
<ImageButton
android:id="@+id/imageSelect1"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"
android:background="@android:color/white"
android:scaleType="centerCrop"
/>
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Image 3" />
<ImageButton
android:id="@+id/imageSelect2"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"
android:background="@android:color/white"
android:scaleType="centerCrop"
/>
<EditText
android:id="@+id/input_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="Title"
android:inputType="textPersonName"
android:padding="7dp"
android:singleLine="true"
android:textSize="14sp" />
<EditText
android:id="@+id/input_description"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="Description"
android:inputType="textMultiLine"
android:padding="7dp"
android:textSize="14sp" />
<EditText
android:id="@+id/input_price"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="Price"
android:inputType="numberDecimal"
android:padding="7dp"
android:textSize="14sp" />
<EditText
android:id="@+id/input_country"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="Country"
android:inputType="textCapWords"
android:padding="7dp"
android:textSize="14sp" />
<EditText
android:id="@+id/input_state_province"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="State/Province/Region"
android:inputType="textCapWords"
android:padding="7dp"
android:textSize="14sp" />
<EditText
android:id="@+id/input_city"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="City"
android:inputType="textCapWords"
android:padding="7dp"
android:textSize="14sp" />
<EditText
android:id="@+id/input_email"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="Your Contact Email"
android:inputType="textEmailAddress"
android:padding="7dp"
android:textSize="14sp" />
<Button
android:id="@+id/btn_post"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="50dp"
android:background="@color/colorPrimary"
android:text="Post"
android:textColor="@android:color/white" />
<ProgressBar
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/progressBar"
android:layout_centerHorizontal="true"
android:layout_marginTop="120dp"
android:visibility="invisible"/>
</LinearLayout>
</ScrollView>
</FrameLayout>
我的帖子片段文件:
private void init(){
mSelectImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "onClick: opening dialog to choose new photo");
SelectPhotoDialog dialog = new SelectPhotoDialog();
dialog.show(getFragmentManager(), getString(R.string.dialog_select_photo));
dialog.setTargetFragment(PostFragment.this, 1);
}
});
mSelectImage1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "onClick: opening dialog to choose new photo");
SelectPhotoDialog dialog1 = new SelectPhotoDialog();
dialog1.show(getFragmentManager(), getString(R.string.dialog_select_photo1));
dialog1.setTargetFragment(PostFragment.this, 2);
}
});
选择照片对话框文件:
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_selectphoto, container, false);
TextView selectPhoto = (TextView) view.findViewById(R.id.dialogChoosePhoto);
selectPhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "onClick: accessing phones memory.");
Intent intent = new Intent();
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, PICKFILE_REQUEST_CODE);
}
});
TextView selectPhoto1 = (TextView) view.findViewById(R.id.dialogChoosePhoto1);
selectPhoto1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "onClick: accessing phones memory.");
Intent intent = new Intent();
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, PICKFILE_REQUEST_CODE1);
}
});
TextView takePhoto = (TextView) view.findViewById(R.id.dialogOpenCamera);
takePhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "onClick: starting camera.");
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_REQUEST_CODE);
}
});
return view;
}
完整的 PostFragment 文件:
完成 SelectPhotoDialog
InversalImageLoader 文件
【问题讨论】:
-
使用这个库,你的生活会简单很多github.com/jkwiecien/EasyImage
标签: android firebase android-studio android-activity firebase-storage