1

In the below code I am getting an image from sd card , and sending the image through an intent, however I want to change the code to send the path of the image , and not the image itself

  @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);
                    ByteArrayOutputStream stream = new ByteArrayOutputStream();               
                   byte imageInByte[] = stream.toByteArray();                    
                  Intent i = new Intent(this, AddImage.class);
                  i.putExtra("image", imageInByte);
                  startActivity(i);
                } catch (IOException e) {
                    e.printStackTrace();     }   }   }    




and here I am receiving the image



     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);

Also I tried this but it didn't work:

 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) {

                filePath = data.getData();

                  Intent i = new Intent(this, AddImage.class);
                  i.putExtra("imagepath", filePath);
                  startActivity(i);
                } catch (IOException e) {
                    e.printStackTrace();     }   }}

but how to receive the uri path

geturi().getintent() ? and then get the image

edit--

I got error nullpointerexception uri string here

    img.setImageURI(Uri.parse(imagePath ));

this is the code

  String imagePath = getIntent().getStringExtra("imagePath");
            ImageView img=(ImageView) findViewById(R.id.imageView);
            img.setImageURI(Uri.parse(imagePath ));

            Bitmap bitmap = ((BitmapDrawable)img.getDrawable()).getBitmap();
            Bitmap out = Bitmap.createScaledBitmap(bitmap, 500, 500, false); 
            // bitmap is the image 
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
           out.compress(Bitmap.CompressFormat.JPEG, 60, stream); 
           out.recycle();

/*       byte[] byteArray = getIntent().getByteArrayExtra("image");

           //      encodedImage = Base64.encodeToString(byteArray, Base64);
//              bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);*/
           //   ImageView imageview = (ImageView) findViewById(R.id.imageView);
                img.setImageBitmap(out);

i am sending intent like this

filePath = data.getData(); Intent i = new Intent(this, AddImage.class); i.putExtra("imagepath", filePath); startActivity(i);

Moudiz
  • 7,211
  • 22
  • 78
  • 156

1 Answers1

2

AddImage Activity

   onCreate(...)
   {
        ....
        String imagePath = getIntent().getStringExtra("imagePath");
        ImageView img=(ImageView) findViewById(R.id.imageView1);
        img.setImageURI(Uri.parse(imagePath ));

        Bitmap bitmap = ((BitmapDrawable)img.getDrawable()).getBitmap();
        Bitmap out = Bitmap.createScaledBitmap(bitmap, 500, 500, false); 
        // bitmap is the image 
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        out.compress(Bitmap.CompressFormat.JPEG, 60, stream);
        img.setImageBitmap(out); 
        bitmap.recycle();
        ....
   }
arun
  • 1,728
  • 15
  • 15
  • but I need to compress the image , how to add those ? Bitmap out = Bitmap.createScaledBitmap(bitmap, 500, 500, false); // bitmap is the image ByteArrayOutputStream stream = new ByteArrayOutputStream(); out.compress(Bitmap.CompressFormat.JPEG, 60, stream); out.recycle(); – Moudiz Sep 15 '15 at 11:50
  • after getting the image I want to decrease the size – Moudiz Sep 15 '15 at 11:51
  • I got error while applying your code , check my edit , can you identify the problem ? – Moudiz Sep 15 '15 at 13:35
  • remove the out.recycle() – arun Sep 15 '15 at 13:38
  • but recylce is important if I want to load another image http://stackoverflow.com/questions/3823799/android-bitmap-recycle-how-does-it-work – Moudiz Sep 15 '15 at 13:43
  • you need to recycle bitmap.recycle(). Because out is currently used. – arun Sep 15 '15 at 13:46
  • still same error at same line .. what is the problem can you help ? @arun – Moudiz Sep 15 '15 at 15:33