如何使用onActivityResult方法使Image适合ImageButton

时间:2016-10-01 19:21:13

标签: java android onactivityresult android-imagebutton

我想设置来自手机图库的任何图片适合 imageButton,并且没有旋转。当我尝试从我的画廊访问图像时,它不适合我的imageButton在旋转时出现。我怎么能摆脱这个。

以下是java代码

import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;

public class PostActivity extends AppCompatActivity {

    private ImageButton imageSelect;
    private static final int GALLERY_REQUEST = 1;
    private EditText mPostTitle;
    private EditText mPostDesc;
    private Button postButton;
    private  Uri imageUri = null;
    private ProgressDialog mProgress;
    private StorageReference mStorage;
    private DatabaseReference mDatabase;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.post_activity);

        mStorage = FirebaseStorage.getInstance().getReference();
        mDatabase = FirebaseDatabase.getInstance().getReference().child("Blog");
 /*        getSupportActionBar().setDisplayShowHomeEnabled(true);
        android.support.v7.app.ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("Post a Picture");*/
        mProgress = new ProgressDialog(this);
        imageSelect = (ImageButton) findViewById(R.id.imageSelect);
        mPostTitle = (EditText) findViewById(R.id.mPostTitle);
        mPostDesc = (EditText) findViewById(R.id.mPostDesc);
        postButton = (Button) findViewById(R.id.postButton);
        imageSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
                galleryIntent.setType("image/");
                startActivityForResult(galleryIntent, GALLERY_REQUEST);
            }
        });


        postButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startPosting();
            }
        });


    }

    private void startPosting() {
        mProgress.setMessage("Uploading Image...");
        mProgress.show();

        final String title_val = mPostTitle.getText().toString().trim();
        final String desc_val = mPostDesc.getText().toString().trim();
  if(!TextUtils.isEmpty(title_val) &&!TextUtils.isEmpty(desc_val) && imageUri!=null){

      StorageReference filepath = mStorage.child("BlogImages").child(imageUri.getLastPathSegment());
      filepath.putFile(imageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
          @Override
          public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {


              Uri downloadUri = taskSnapshot.getDownloadUrl();
              DatabaseReference newPost = mDatabase.push();
              newPost.child("title").setValue(title_val);
              newPost.child("description").setValue(desc_val);
              newPost.child("imageUrl").setValue(downloadUri.toString());
            //  newPost.child("uid").setValue(FirebaseAuth.getc);


              mProgress.dismiss();

              startActivity(new Intent(PostActivity.this,MainActivity.class));
          }
      });


  }



    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == GALLERY_REQUEST && resultCode == RESULT_OK) {


             imageUri = data.getData();
            imageSelect.setImageURI(imageUri);
        }
    }


}

所以我的onActivityResult结果也包含在上面。

以下是我的xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1">
<ScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <LinearLayout
        android:layout_width="wrap_content"
        android:orientation="vertical"
        android:layout_height="wrap_content">
    <ImageButton
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/imageSelect"
        android:layout_gravity="center_horizontal"
        android:background="#00ffffff"
        android:src="@mipmap/add_btn"
        android:scaleType="centerCrop" />
        <include layout="@layout/content_post" />
    </LinearLayout>
</ScrollView>
</LinearLayout>

2 个答案:

答案 0 :(得分:0)

如果我理解你的问题,你希望图像填满ImageButton的范围。您可以使用属性android:adjustViewBounds执行此操作。

如果要禁用屏幕漫游,请在android:screenOrientation的{​​{1}}元素中将activity设置为纵向或横向。

Manifest.xml

答案 1 :(得分:0)

我已经回答了你解决轮换问题的问题,你也可以尝试解决这个问题。

Compression and rotation of image picked from gallery is solved