I'm having a problem trying to send a high-resolution image to a WS.
I get the image, I get the path and send it as a parameter to another activity in which it sends to WS.
Before sending the image to the WS I treat it due to its size, cell phones with resolution above 8mp already from the memory overflow.
I made some adjustments to my code but it is still crashing.
Method call:
private void TakeFoto(View v)
{
try{
final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getTempFile(getApplicationContext())));
startActivityForResult(intent, TAKE_PICTURE);
}catch(Exception e){
e.printStackTrace();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == TAKE_PICTURE) {
try {
final File file = getTempFile(this);
Bitmap captureBmp = Media.getBitmap(getContentResolver(), Uri.fromFile(file));
Uri imagemSelecionada = getImageUri(getBaseContext(),captureBmp);
String[] colunas = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(imagemSelecionada,colunas, null, null, null);
cursor.moveToFirst();
int indexColuna = cursor.getColumnIndex(colunas[0]);
String pathImg = cursor.getString(indexColuna);
cursor.close();
Bundle params = new Bundle();
params.putString("imagem", pathImg);
Intent intent = new Intent(this, TelaDadosUpload.class);
intent.putExtras(params);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
public Uri getImageUri(Context inContext, Bitmap inImage) {
String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}
private File getTempFile(Context context){
//it will return /sdcard/image.tmp
final File path = new File( Environment.getExternalStorageDirectory(), context.getPackageName() );
if(!path.exists()){
path.mkdir();
}
return new File(path, "image.tmp");
}
Upload method for WS:
public void upload(String fileUri)
{
HttpClient httpClient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://localhost/WS/upload.php");
String fileName = fileUri;
try{
Bitmap bm = BitmapFactory.decodeFile(fileName);
Bitmap resized = Bitmap.createScaledBitmap(bm,(int)(bm.getWidth()*1.0), (int)(bm.getHeight()*1.0), true);
ByteArrayOutputStream output = new ByteArrayOutputStream();
resized.compress(Bitmap.CompressFormat.PNG, 0, output); //bm is the bitmap object
byte[] bytes = output.toByteArray();
try{
String base64Image = Base64.encodeToString(bytes, Base64.DEFAULT);
ArrayList<NameValuePair> valores = new ArrayList<NameValuePair>();
valores.add(new BasicNameValuePair("image",base64Image));
valores.add(new BasicNameValuePair("tipodoc",ClasseUsuario.spinner));
httppost.setEntity(new UrlEncodedFormEntity(valores));
HttpResponse resposta = httpClient.execute(httppost);
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
catch (ClientProtocolException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}catch(Exception e){
e.printStackTrace();
}
}
At this point the error occurs: By the message of the log it seems that the problem is in the decodeFile of class BitmapFactory
The log:
Could anyone help me solve this problem? Thank you.