0

I'm a beginner in Android, my problem is I can't pass the captured image to another activity for it to be manipulated. My plan is to save the captured photo in byte since i only need it temporarily then pass it to another activity or for the sake of manipulating the image. Is their a way for me to get its "jpg url" without saving to the user's gallery? Thanks in advance.

EDIT: Here are some code from CameraUtility class added with
bitmapPicture = BitmapFactory.decodeByteArray(arg0 , 0, arg0.length);...

 try{
            //Write File
            String filename="bitmap.png";
            FileOutputStream stream = getApplicationContext().openFileOutput(filename, Context.MODE_PRIVATE);
           // bitmapPicture.compress(Bitmap.CompressFormat.JPEG, 50, stream);
            bitmapPicture = BitmapFactory.decodeByteArray(arg0 , 0, arg0.length);
            //Cleanup
            stream.close();
            Toast.makeText(getApplicationContext(), "Saved", Toast.LENGTH_LONG).show();
            bitmapPicture.recycle();
            //Pop intent
            Intent in1 = new Intent(CameraUtility.this, Receiver.class);
            in1.putExtra("image", filename);
            startActivity(in1);


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

Receiver class...

public class Receiver extends AppCompatActivity{


protected void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bitmap bitmapPicture = null;
    String filename = getIntent().getStringExtra("image");
   try{
       FileInputStream is = this.openFileInput(filename);
       bitmapPicture = BitmapFactory.decodeStream(is);
       is.close();
   }catch (Exception e){
       e.printStackTrace();
   }

    setContentView(R.layout.receive_bitmap);
    ImageView viewBtimap = findViewById(R.id.bitmapview);
    viewBtimap.setImageBitmap(bitmapPicture);


}

EDIT: But now, it doesn't have an error. But the image won't pass on to another activity.

qwerty
  • 51
  • 1
  • 9
  • https://stackoverflow.com/questions/11010386/send-bitmap-using-intent-android try this – Sivakumar S Jan 28 '18 at 06:46
  • thanks @SivakumarS I tried but it throws a nullpointer exception – qwerty Jan 28 '18 at 10:09
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Zoe Jan 28 '18 at 10:51

1 Answers1

0

To fix the particular crash, it's enough to add one line to your pictureTaken function:

bitmapPicture = BitmapFactory.decodeByteArray(arg0 , 0, arg0.length);

But this is not the best way to deal with your task.

Instead of converting the picture to PNG, you can save the arg0 byte array as is. It will be a legitimate Jpeg file, and any consumer, including your Receiver activity, can use it with no changes.

Furthermore, you don't need to store the picture as file. Keep the byte array in memory and convert it to bitmap (don't forget to scale it down to match the ImageView dimensions) with BitmapFactory.decodeByteArray().

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • thanks for helping me out. There was no error upon adding one line to the picture function. However, the picture is still not passed on receiver activity :/ Why is that? @alex – qwerty Jan 29 '18 at 01:58
  • You removed `bitmapPicture.compress(Bitmap.CompressFormat.JPEG, 50, stream)`, so now the **stream** is empty, so is the *bitmap.png* file. – Alex Cohn Jan 29 '18 at 08:42
  • Oh sorry. I tried keeping both 2 lines still receive NullPointerException "Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference" – qwerty Jan 29 '18 at 16:08
  • sure, first you should create the **bitmapPicture** through **BitmapFactory**, and only after that you can **compress()** it. – Alex Cohn Jan 29 '18 at 21:57
  • wow thank u :D May I ask how to properly manipulate or get the filename of the recent captured image? using the same code I did? – qwerty Jan 29 '18 at 22:45