Error when implementing method to take photo by application

Asked

Viewed 118 times

4

I am wanting to add the method of taking photos by the application only when running my application error: erro

below is my form class code:

Spinner option;
Button btnSend;
EditText Nome;
EditText Message;
EditText Endereco;
EditText Telefone;
EditText Email;
ImageView img;
private Bitmap bitmap;

@Override
public View onCreateView(LayoutInflater inflater,
                         ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.reclame, container, false);

    img = (ImageView) view.findViewById(R.id.imageButton);
    img.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            abrirCamera();
        }
    });

    Nome = (EditText) view.findViewById(R.id.nome1);
    Message = (EditText) view.findViewById(R.id.msg);
    Endereco = (EditText) view.findViewById(R.id.endereco);
    Telefone = (EditText) view.findViewById(R.id.telefone);
    Email = (EditText) view.findViewById(R.id.email);
    btnSend = (Button) view.findViewById(R.id.send);
    option = (Spinner)view.findViewById(R.id.spinner);
    option.setOnItemSelectedListener(this);
    ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(), R.array.option, android.R.layout.simple_spinner_item);
    option.setAdapter(adapter);

    btnSend.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String nome = Nome.getText().toString();
            String message = Message.getText().toString();
            String matricula = Endereco.getText().toString();
            String telefone = Telefone.getText().toString();
            String assunto = option.getSelectedItem().toString();
            String ema = Email.getText().toString();
            Intent email = new Intent(Intent.ACTION_SEND);
            email.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"});
            email.putExtra(Intent.EXTRA_SUBJECT, assunto);
            email.putExtra(Intent.EXTRA_TEXT, "Nome: " +nome +'\n'+  "Endereço: "+matricula + '\n'+ "Telefone: "+telefone + '\n'+ "E-mail: "+ema + '\n'+ '\n'+message);


            email.setType("message/rfc822");
            startActivity(Intent.createChooser(email, "Selecione o serviço de e-mail:"));
        }
    });
    return view;
}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    TextView mtext = (TextView) view;
    //Toast.makeText(getActivity(), "Você selecionou: "+mtext.getText(), Toast.LENGTH_SHORT).show();
}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}

public void abrirCamera(){
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(intent, 0);
}

public void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode, resultCode, data);
    InputStream stream = null;
    if(requestCode == 0 && resultCode == RESULT_OK){
        try {
            if(bitmap != null){
                bitmap.recycle();
            }
            stream = getContentResolver().openInputStream(data.getData());
            bitmap = BitmapFactory.decodeStream(stream);
            img.setImageBitmap(resizeImage(getActivity(), bitmap, 700, 600));
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }finally {
            if (stream != null)
                try {
                    stream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }
}

private static Bitmap resizeImage (Context context, Bitmap bpmOriginal, float newWidth, float newHeight){
    Bitmap novoBpm = null;
    int w = bpmOriginal.getWidth();
    int h = bpmOriginal.getHeight();
    float densityFactor = context.getResources().getDisplayMetrics().density;
    float novoW = newWidth * densityFactor;
    float novoH = newHeight * densityFactor;

    float scalaW = novoW /w;
    float scalaH = novoH /h;
    Matrix matrix = new Matrix();
    matrix.postScale(scalaW, scalaH);
    novoBpm = Bitmap.createBitmap(bpmOriginal, 0, 0, w, h, matrix, true);
    return novoBpm;
}

What’s wrong? and how to send the image along with the form information by email?

1 answer

1


DRT:

Use getActivity().getContentResolver() where you have getContentResolver().


Your mistake clearly says:

error: cannot find Symbol method getContentResolver()

Because in the method onActivityResult you declare:

stream = getContentResolver().openInputStream(data.getData());

and the method getContentResolver needs a Context, which you don’t have. Simply provide the context:

stream = getActivity().getContentResolver().openInputStream(data.getData());

Browser other questions tagged

You are not signed in. Login or sign up in order to post.