-1

I built This App and i want that i can choose an image from gallery to replace the image view at the top. like the profil at facebook etc...

i tried the above code:

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

in my phone with android 4.4.4: i can choose the image but when i press ok the app crash.

in emulator android 5.1.1: when i press the image the app crash

fdelafuente
  • 1,061
  • 1
  • 11
  • 24
  • Possible duplicate of [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – Zoe Jun 02 '19 at 17:42
  • @Zoe , thats not my problem... i need a code to do what i wanna do – Planet of shadows Jun 02 '19 at 17:56

1 Answers1

-1

Make your button call the activity implement here with startActivityForResult: https://www.youtube.com/watch?v=w06OnGwhh4I&feature=youtu.be

This activity pick image from camera, gallery, google drive, etc. Crop and rotate the image to. Code 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 {

    private ImageButton btn_browse;
    private ImageButton btn_reset;
    private ImageView imageView;
    private Uri uriPass = null;

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

        btn_browse = findViewById(R.id.image_cropper_btn_add_picture);
        btn_reset = findViewById(R.id.image_cropper_btn_reset);
        imageView = findViewById(R.id.cropper_image);

        btn_browse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CropImage.startPickImageActivity(Image_Cropper_Activity.this);                      
            }
        });

        btn_reset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imageView.setImageBitmap(null);
                uriPass = null;
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.add_cliente_fornecedor, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {

            case (android.R.id.home):
                setResult(RESULT_OK);
                finish();
                return true;

            case (R.id.save_client_fornecedor):
                saveImage();
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if(requestCode == CropImage.PICK_IMAGE_CHOOSER_REQUEST_CODE && resultCode == Activity.RESULT_OK){
            Uri imageUri = CropImage.getPickImageResultUri(this, data);
            if(CropImage.isReadExternalStoragePermissionsRequired(this, imageUri)){
            }
            else {
                startCrop(imageUri);
            }
        }

        if(requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE){
            CropImage.ActivityResult result = CropImage.getActivityResult(data);
            if(resultCode == RESULT_OK){
                imageView.setImageURI(result.getUri());
                uriPass = result.getUri();
                //Toast.makeText(this, "Imagem Atualizada Com Sucesso!", Toast.LENGTH_SHORT).show();
            }
        }
    }

    private void startCrop(Uri imageUri) {
        CropImage.activity(imageUri).setGuidelines(CropImageView.Guidelines.ON).setMultiTouchEnabled(true).start(this);
    }

    private void saveImage() {
        if (uriPass==null){
            Toast.makeText(this, "Por favor, insira uma imagem.", Toast.LENGTH_SHORT).show();
            return;
        }

        Intent dataForAddEditScreen = new Intent();
        dataForAddEditScreen.setData(uriPass);                                                      
        setResult(RESULT_OK, dataForAddEditScreen);
        finish();
    }
}

Xml code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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"
    android:orientation="vertical"
    android:gravity="center"
    android:padding="10dp"
    tools:context=".Activities.Image_Cropper_Activity">

    <ImageView
        android:id="@+id/cropper_image"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal"
        android:padding="20dp">

        <ImageButton
            android:id="@+id/image_cropper_btn_add_picture"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:background="@drawable/bg_round"
            app:srcCompat="@drawable/ic_image_sample_50x50_white" />

        <Space
            android:layout_width="20dp"
            android:layout_height="wrap_content" />

        <Space
            android:layout_width="20dp"
            android:layout_height="wrap_content" />

        <ImageButton
            android:id="@+id/image_cropper_btn_reset"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:background="@drawable/bg_round"
            app:srcCompat="@drawable/ic_refresh_50x50_white" />

    </LinearLayout>
</LinearLayout>

After you return the image Uri:

private ImageView imageProduto;

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add__edit__produto_);

imageProduto = findViewById(R.id.image_produto_detail);
}

...

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == Constantes.REQUEST_CODE_ADD_PICTURE && resultCode == RESULT_OK && data!= null){

            Uri imageUri = data.getData();
            imageProduto.setImageURI(imageUri);
            Toast.makeText(this, "Image Update Successfully!", Toast.LENGTH_SHORT).show();

    }else {
        Toast.makeText(this, "Image Not Updated!", Toast.LENGTH_SHORT).show();
    }

}