Show captured image in new view

I am new to android and am creating a small application that takes pictures from the camera and saves it in the gallery.

Here is the function that captures the image.

private void onCaptureImageResult(Intent data) { Bitmap thumbnail = (Bitmap) data.getExtras().get("data"); ByteArrayOutputStream bytes = new ByteArrayOutputStream(); thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes); File destination = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg"); FileOutputStream fo; try { destination.createNewFile(); fo = new FileOutputStream(destination); fo.write(bytes.toByteArray()); fo.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } 

This is activity_main.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="10dp" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:padding="5dp" > <Button android:id="@+id/btnSelectPhoto" android:background="#149F82" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Select Photo" /> </LinearLayout> </LinearLayout> 

What I want to do when capturing an image, I want to display the image on a different activity (page), but not on the same action that has a button for capturing the image. How to do it.

Thank you in advance

+5
source share
5 answers

You just need to pass the path to the new activity from your method.

 Intent intent = new Intent(this, NewActivity.class); intent.putExtra("MyImagePath", destination.getAbsoluteFile()); startActivity(intent); 

And in a new activity

 File imgFile = new File(filePath); if(imgFile.exists()){ Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath()); ImageView myImage = (ImageView) findViewById(R.id.imageviewTest); myImage.setImageBitmap(myBitmap); } 
+2
source

Simple, after taking a photo, your photo will be saved. Now, with the Result of Activity method, you just need to use the intention to proceed to the second action.

In your second Office, simply enter the Path of the saved image and set it to your ImageView.

+1
source

Mainactivity

  String filePath; @Override protected void onCreate(Bundle savedInstanceState) { ... } private void onCaptureImageResult(Intent data) { Bitmap thumbnail = (Bitmap) data.getExtras().get("data"); ByteArrayOutputStream bytes = new ByteArrayOutputStream(); thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes); String fileName = System.currentTimeMillis() + ".jpg" filePath = Environment.getExternalStorageDirectory().getAbsolutePath() + fileName; File destination = new File(filePath); ... } yourButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(getApplicationContext(), AnotherActivity.class); intent.putExtra("filePath", filePath) startActivity(intent); } }); 

AnotherActivity

 String filePath; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent intent = this.getIntent(); filePath = intent.getStringExtra("filePath"); File imgFile = new File(filePath); if(imgFile.exists()){ Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath()); ImageView myImage = (ImageView) findViewById(R.id.imageviewTest); myImage.setImageBitmap(myBitmap); } } 
+1
source

I assume that you know how to get information about captured images from intent.

in case you don't know. google and you will find many solutions for this.

check this

You can use two ways to display a capture image with the next activity.

1 => start a new activity in which you want to display the image. and open the camera in the onCreate method and get the result from the camera in onActivityResult and show the image.

2 => open the camera data and and getIntent in onActivityResult and pass this information to your next action and show the image in the onCreate method.

0
source

Below is the solution. You can transfer the captured bitmap into an intention, and then start your new activity. As shown below. You can implement this logic inside your onCaptureImageResult () as soon as you get the image as a capture.

  Intent intent = new Intent(this, NewActivity.class); intent.putExtra("MyImage", bitmap); startActivity(intent); 

Then you can get the bitmap from the intent following the step below. And set the image to ImageView or do something else with it.

  ImageView imgView = (ImageView) img.findViewById(R.id.myIMageView); /* Called inside OnCreate() using the parent View */ Intent intent = getIntent(); Bitmap bitmap = (Bitmap) intent.getParcelableExtra("MyImage"); 

Hope this helps you. Enjoy it.

0
source

Source: https://habr.com/ru/post/1242515/


All Articles