1
I’m finishing developing a form in android studio, where the user send an image along with information via email. Already managed to do everything, the problem now is when I take the photo and I send to the email appears a message:
it is not possible to attach due to the error of e/s
Here’s my class where I developed my form.
package br.com.diego.tecnologiadanet.fragments;
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);
img = (ImageView)view.findViewById(R.id.imageButton);
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("image/jpeg");
File bitmapFile = new File(Environment.getExternalStorageDirectory()+"/image.jpg");
Uri myUri = Uri.fromFile(bitmapFile);
email.putExtra(Intent.EXTRA_STREAM, myUri);
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 == Activity.RESULT_OK){
try {
if(bitmap != null){
bitmap.recycle();
}
stream = getActivity().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;
}
I did a search and some comments say that I am not able to get the address where my photo was saved, is that my code is not doing this?
How’s your
AndroidManifest.xml
?– Igor Mello
this way:
<uses-permission android:name="ANDROID.PERMISSION.CALL_PHONE"/>
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
 <uses-permission android:name="android.permission.INTERNET"/>
 <uses-permission android:name="android.permission.WAKE_LOCK"/>
 <uses-permission android:name="android.permission.CAMERA" />
 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
 <uses-feature android:name="android.hardware.camera"/>
 </application>

</manifest>
– Carlos Diego
Try adding this write permission to the method
getExternalStorageDirectory()
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
– Igor Mello
I added the permission but still with the same problem =[.. just need to finish my apk.
– Carlos Diego
I see there in your code also that you use twice the method
setType
in the same reference ofIntent email
? Try commenting on the second one there!– Igor Mello
I commented and the only thing that changed was that more sharing options appeared, like Dropbox, facebook.. etc but the problem remains = Igor here does not have the chat option? would be more agile
– Carlos Diego
Let’s go continue this discussion in chat.
– Carlos Diego