如何从图库中导入图像并替换图像视图中的图像?

我构建了这个应用程序,我希望我可以从图库中选择一个图像来替换顶部的图像视图。就像 facebook 上的个人资料等...

我试过上面的代码:

public class MainActivity extends Activity {

private static int RESULT_LOAD_IMAGE = 1;

Uri myPicture = null;

Button buttonLoadImage;


/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);


    //getting View

    buttonLoadImage =(Button) findViewById(R.id.regButton);


    buttonLoadImage.setOnClickListener(new View.OnClickListener() {


        @Override

        public void onClick(View arg0) {


            Intent i = new Intent(

                    Intent.ACTION_PICK,

                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);


            startActivityForResult(i, RESULT_LOAD_IMAGE);

        }

    });

}



@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);


    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {

        Uri selectedImage = data.getData();

        String[] filePathColumn = { MediaStore.Images.Media.DATA };


        Cursor cursor = getContentResolver().query(selectedImage,

                filePathColumn, null, null, null);

        cursor.moveToFirst();


        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);

        String picturePath = cursor.getString(columnIndex);

        cursor.close();


        ImageView imageView = (ImageView) findViewById(R.id.regUserPhoto);

        imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

    }

}

在我的 android 4.4.4 手机中:我可以选择图像,但是当我按下 ok 时,应用程序崩溃了。


在模拟器 android 5.1.1 中:当我按下图像时,应用程序崩溃


慕标5832272
浏览 64回答 1
1回答

慕容708150

使用 startActivityForResult 使您的按钮在此处调用活动工具: https ://www.youtube.com/watch?v=w06OnGwhh4I&feature=youtu.be此活动从相机、画廊、谷歌驱动器等中挑选图像。裁剪图像并将其旋转到。代码Java:import android.app.Activity;import android.content.Intent;import android.net.Uri;import android.support.annotation.Nullable;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.ImageButton;import android.widget.ImageView;import android.widget.Toast;import com.example.android.controledevendas.R;import com.theartofdev.edmodo.cropper.CropImage;import com.theartofdev.edmodo.cropper.CropImageView;public class Image_Cropper_Activity extends AppCompatActivity {&nbsp; &nbsp; private ImageButton btn_browse;&nbsp; &nbsp; private ImageButton btn_reset;&nbsp; &nbsp; private ImageView imageView;&nbsp; &nbsp; private Uri uriPass = null;&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; &nbsp; &nbsp; setContentView(R.layout.activity_image__cropper_);&nbsp; &nbsp; &nbsp; &nbsp; btn_browse = findViewById(R.id.image_cropper_btn_add_picture);&nbsp; &nbsp; &nbsp; &nbsp; btn_reset = findViewById(R.id.image_cropper_btn_reset);&nbsp; &nbsp; &nbsp; &nbsp; imageView = findViewById(R.id.cropper_image);&nbsp; &nbsp; &nbsp; &nbsp; btn_browse.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CropImage.startPickImageActivity(Image_Cropper_Activity.this);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; btn_reset.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; imageView.setImageBitmap(null);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; uriPass = null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public boolean onCreateOptionsMenu(Menu menu) {&nbsp; &nbsp; &nbsp; &nbsp; // Inflate the menu; this adds items to the action bar if it is present.&nbsp; &nbsp; &nbsp; &nbsp; getMenuInflater().inflate(R.menu.add_cliente_fornecedor, menu);&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public boolean onOptionsItemSelected(MenuItem item) {&nbsp; &nbsp; &nbsp; &nbsp; switch (item.getItemId()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case (android.R.id.home):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setResult(RESULT_OK);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; finish();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case (R.id.save_client_fornecedor):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; saveImage();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return super.onOptionsItemSelected(item);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {&nbsp; &nbsp; &nbsp; &nbsp; if(requestCode == CropImage.PICK_IMAGE_CHOOSER_REQUEST_CODE && resultCode == Activity.RESULT_OK){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Uri imageUri = CropImage.getPickImageResultUri(this, data);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(CropImage.isReadExternalStoragePermissionsRequired(this, imageUri)){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startCrop(imageUri);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if(requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CropImage.ActivityResult result = CropImage.getActivityResult(data);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(resultCode == RESULT_OK){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; imageView.setImageURI(result.getUri());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; uriPass = result.getUri();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Toast.makeText(this, "Imagem Atualizada Com Sucesso!", Toast.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; private void startCrop(Uri imageUri) {&nbsp; &nbsp; &nbsp; &nbsp; CropImage.activity(imageUri).setGuidelines(CropImageView.Guidelines.ON).setMultiTouchEnabled(true).start(this);&nbsp; &nbsp; }&nbsp; &nbsp; private void saveImage() {&nbsp; &nbsp; &nbsp; &nbsp; if (uriPass==null){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(this, "Por favor, insira uma imagem.", Toast.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; Intent dataForAddEditScreen = new Intent();&nbsp; &nbsp; &nbsp; &nbsp; dataForAddEditScreen.setData(uriPass);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; setResult(RESULT_OK, dataForAddEditScreen);&nbsp; &nbsp; &nbsp; &nbsp; finish();&nbsp; &nbsp; }}代码:<?xml version="1.0" encoding="utf-8"?><LinearLayout&nbsp; &nbsp; xmlns:android="http://schemas.android.com/apk/res/android"&nbsp; &nbsp; xmlns:app="http://schemas.android.com/apk/res-auto"&nbsp; &nbsp; xmlns:tools="http://schemas.android.com/tools"&nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; android:layout_height="match_parent"&nbsp; &nbsp; android:orientation="vertical"&nbsp; &nbsp; android:gravity="center"&nbsp; &nbsp; android:padding="10dp"&nbsp; &nbsp; tools:context=".Activities.Image_Cropper_Activity">&nbsp; &nbsp; <ImageView&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/cropper_image"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="0dp"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_weight="1" />&nbsp; &nbsp; <LinearLayout&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:gravity="center"&nbsp; &nbsp; &nbsp; &nbsp; android:orientation="horizontal"&nbsp; &nbsp; &nbsp; &nbsp; android:padding="20dp">&nbsp; &nbsp; &nbsp; &nbsp; <ImageButton&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/image_cropper_btn_add_picture"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="80dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="80dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:background="@drawable/bg_round"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; app:srcCompat="@drawable/ic_image_sample_50x50_white" />&nbsp; &nbsp; &nbsp; &nbsp; <Space&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="20dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content" />&nbsp; &nbsp; &nbsp; &nbsp; <Space&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="20dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content" />&nbsp; &nbsp; &nbsp; &nbsp; <ImageButton&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/image_cropper_btn_reset"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="80dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="80dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:background="@drawable/bg_round"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; app:srcCompat="@drawable/ic_refresh_50x50_white" />&nbsp; &nbsp; </LinearLayout></LinearLayout>返回图片 Uri 后:private ImageView imageProduto;protected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; &nbsp; &nbsp; setContentView(R.layout.activity_add__edit__produto_);imageProduto = findViewById(R.id.image_produto_detail);}...@Overrideprotected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {&nbsp; &nbsp; super.onActivityResult(requestCode, resultCode, data);&nbsp; &nbsp; if(requestCode == Constantes.REQUEST_CODE_ADD_PICTURE && resultCode == RESULT_OK && data!= null){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Uri imageUri = data.getData();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; imageProduto.setImageURI(imageUri);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(this, "Image Update Successfully!", Toast.LENGTH_SHORT).show();&nbsp; &nbsp; }else {&nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(this, "Image Not Updated!", Toast.LENGTH_SHORT).show();&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java