resize image from sd card not working

时间:2015-09-14 15:43:24

标签: android

what is the problem with my code ? I am trying to decrease the size of image but its still 200kb

   @Override
       protected void onActivityResult(int requestCode, int resultCode, Intent data) {

           super.onActivityResult(requestCode, resultCode, data);

            if (requestCode == 1 && resultCode == RESULT_OK && data != null && data.getData() != null) {

                //file name
                filePath = data.getData();
                try {
                //  Bundle extras2 = data.getExtras();
                    bitmap  = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
                    Bitmap out = Bitmap.createScaledBitmap(bitmap, 500, 500, false);

                  //  imageview.setImageBitmap(bitmap);
                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    out.compress(Bitmap.CompressFormat.JPEG, 60, stream);
                    out.recycle();
                //    File dir=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
                  //  Bitmap b= BitmapFactory.decodeFile(PATH_ORIGINAL_IMAGE);
                  //  Bitmap out = Bitmap.createScaledBitmap(bitmap, 500, 500, false);

                 //   File file = new File(dir, "resize.png");
                   // FileOutputStream fOut;

                   byte imageInByte[] = stream.toByteArray();                    
                  Intent i = new Intent(this,
                            AddImage.class);
                  i.putExtra("image", imageInByte);
                  startActivity(i);

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
       }    

this is addimage class

public class AddImage extends Activity {
    InputStream inputStream;
    String categorie;
    String caption;
    private Bitmap bmp;
        @Override
    public void onCreate(Bundle icicle) {
            super.onCreate(icicle);
            setContentView(R.layout.main);

            //get values
            EditText captionetxt = (EditText) findViewById(R.id.caption);
            caption = captionetxt.getText().toString();
            ImageView view2 = (ImageView) findViewById(R.id.imageView);
            //spinner
            Spinner dropdown = (Spinner)findViewById(R.id.spinner1);
            String[] items = new String[]{"Lebanese jokes", "Student Jokes", "Quotes"};
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, items);
            dropdown.setAdapter(adapter);
            Spinner mySpinner=(Spinner) findViewById(R.id.spinner1);

            categorie = mySpinner.getSelectedItem().toString();



         byte[] byteArray = getIntent().getByteArrayExtra("image");
           //      encodedImage = Base64.encodeToString(byteArray, Base64);
                bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
              ImageView imageview = (ImageView) findViewById(R.id.imageView);
                imageview.setImageBitmap(bmp);
        }
        public void onclick(View view)
        {
            Toast.makeText(AddImage.this, "Uploading Image", Toast.LENGTH_LONG).show();                         
            upload();

              Intent i = new Intent(this,
                        MainActivity.class);
              startActivity(i);
        }
        public void upload()
        {
              Calendar thisCal = Calendar.getInstance();
              thisCal.getTimeInMillis();

                android.util.Log.i("Time Class ", " Time value in millisecinds "+ thisCal);

           // Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);  
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bmp.compress(Bitmap.CompressFormat.PNG, 90, stream); //compress to which format you want.
            byte [] byte_arr = stream.toByteArray();
            String image_str = Base64.encodeBytes(byte_arr);
            final ArrayList<NameValuePair> nameValuePairs = new  ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("image",image_str));
            nameValuePairs.add(new BasicNameValuePair("caption",caption));
            nameValuePairs.add(new BasicNameValuePair("name","je"));
            nameValuePairs.add(new BasicNameValuePair("categorie",categorie));
             Thread t = new Thread(new Runnable() {

            @Override
            public void run() {
                  try{

                         HttpClient httpclient = new DefaultHttpClient();
                         HttpPost httppost = new HttpPost("http://justedhak.comlu.com/images/upload_image.php");
                         httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                         HttpResponse response = httpclient.execute(httppost);
                         final String the_string_response = convertResponseToString(response);
                         runOnUiThread(new Runnable() {

                                @Override
                                public void run() {
                                    Toast.makeText(AddImage.this, "Response " + the_string_response, Toast.LENGTH_LONG).show();                         
                                }
                            });

                     }catch(final Exception e){
                          runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                Toast.makeText(AddImage.this, "ERROR " + e.getMessage(), Toast.LENGTH_LONG).show();                             
                            }
                        });
                           System.out.println("Error in http connection "+e.toString());
                     }  
            }
        });
         t.start();
        }

        public String convertResponseToString(HttpResponse response) throws IllegalStateException, IOException{

             String res = "";
             StringBuffer buffer = new StringBuffer();
             inputStream = response.getEntity().getContent();
             final int contentLength = (int) response.getEntity().getContentLength(); //getting content length…..
              runOnUiThread(new Runnable() {

            @Override
            public void run() {
                Toast.makeText(AddImage.this, "contentLength : " + contentLength, Toast.LENGTH_LONG).show();                        
            }
        });

             if (contentLength < 0){
             }
             else{
                    byte[] data = new byte[512];
                    int len = 0;
                    try
                    {
                        while (-1 != (len = inputStream.read(data)) )
                        {
                            buffer.append(new String(data, 0, len)); //converting to string and appending  to stringbuffer…..
                        }
                    }
                    catch (IOException e)
                    {
                        e.printStackTrace();
                    }
                    try
                    {
                        inputStream.close(); // closing the stream…..
                    }
                    catch (IOException e)
                    {
                        e.printStackTrace();
                    }
                    res = buffer.toString();     // converting stringbuffer to string…..

                    runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                       Toast.makeText(AddImage.this, "Result : res", Toast.LENGTH_LONG).show();
                    }
                });
                    //System.out.println("Response => " +  EntityUtils.toString(response.getEntity()));
             }
             return res;
        }




}

0 个答案:

没有答案