How to treat a high resolution image

Asked

Viewed 595 times

0

I am facing a problem when trying to send a high resolution image to a WS.

I capture the image,pick up her path and send as a parameter to another Activity in which send to WS.

Before sending the image to WS I do a treatment on it due to its size, mobile phones with resolution above 8mp already from the memory overflow.

I made some adjustments in my code but still giving memory crash.

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 to 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: From the log message it seems that the problem is in the decodeFile of the Bitmapfactory class inserir a descrição da imagem aqui inserir a descrição da imagem aqui

The log: inserir a descrição da imagem aqui

Could someone help me solve this problem? Thank you.

  • 1

    You can put the error that is being presented?

  • Of course Tony, my fault. I will edit my topic with the moment of error.

1 answer

1

Try using the attribute android:largeHeap="true" on the tag <application ... of your Manifest. This adds a bit of memory to your application and I’ve already used it to fix a memory crash problem similar to yours. Link to the Android documentation about Largeheap

  • Thanks for the tip Leosantana, this complicated solve this problem of memory overflow.

Browser other questions tagged

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