从图库中选择图像的对话框

时间:2018-11-17 22:22:15

标签: android firebase android-studio android-activity firebase-storage

我试图显示一个对话框,在其中按下ImageButton的位置,然后从图库中选择图片或从相机中获取图片。  我已经在努力选择图像并将其插入正确的图像视图中。

现在,单击ImageButton(android:id =“ @ + id / imageSelect”)将弹出对话框,然后从图库中选择,但它将图像插入(android:id =“ @ + id / imageSelect1)应该插入(android:id =“ @ + id / imageSelect)。 当我选择(android:id =“ @ + id / imageSelect1)时,它只会覆盖图像,因此(android:id =” @ + id / imageSelect1)保持为空

我觉得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);
        }
    });

SelectPhotoDialog文件:

 @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文件:

https://pastebin.com/PJXB3cq1

完成SelectPhotoDialog

https://pastebin.com/fF2Jj986

IniversalImageLoader文件

Universal image loader

1 个答案:

答案 0 :(得分:0)

我不确定,但是我认为没有足够的信息...我找不到所引用的UniversalImageLoader,我想它可以处理图像的实际设置,因为setImage暗示。

@Override

    public void getImagePath(Uri imagePath) {

        Log.d(TAG, "getImagePath: setting the image to imageview");

       // THIS LINE  UniversalImageLoader.setImage(imagePath.toString(), mSelectImage);

 

        //assign to global variable

        mSelectedBitmap = null;

        mSelectedUri = imagePath;

    }

更新为答案
好的,这就是我通常执行的对话框

private static final int READ_REQUEST_CODE = 42;

public void performFileSearch()
{

    // ACTION_OPEN_DOCUMENT is the intent to choose a file via the system's file
    // browser.
    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);

    // Filter to only show results that can be "opened", such as a
    // file (as opposed to a list of contacts or timezones)
    intent.addCategory(Intent.CATEGORY_OPENABLE);

    // Filter to show only images, using the image MIME data type.
    // If one wanted to search for ogg vorbis files, the type would be "audio/ogg".
    // To search for all documents available via installed storage providers,
    // it would be "*/*".
    intent.setType("*/*");

    startActivityForResult(intent, READ_REQUEST_CODE);
}
@Override
public void onActivityResult(int requestCode, int resultCode,
                             Intent resultData)
{
   if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK)
    {
       Uri uri = null;
        if (resultData != null)
        {
            uri = resultData.getData();
            String back = uri.toString();
           // DO SOMETHING WITH STRING
        }
    }
}

这样您的代码就可以了

    @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.");
            performFileSearch()
        }
    });

    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.");
            performFileSearch();
        }
    });

    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;
}

//我不认为这是原因,仅是因为一切似乎一直流到设置图像的那一部分...对话框将uri返回到图像,但是另一个代码正在设置图像在错误的位置,

ImageView X = (ImageView) findViewById(R.id.ImageViewID); 
X.setBackgroundResource ?

我正在寻找类似的东西,我认为这可能是导致问题的原因。

抓取
我找到了ImageLoader,一切似乎都很好..

Constructor Detail:

ImageLoader

public ImageLoader(RequestQueue queue, ImageLoader.ImageCache imageCache)Constructs a new ImageLoader.

// Parameters:queue - The RequestQueue to use for making image requests.imageCache - The cache to use as an L1 cache.

您是否尝试过另一种设置图像的方式?