1

In My app I am taking picture and I am successfully saving it in the gallery after compressing it. Now I want to show it into other activity, so that user can share it or view it at least. So How can I do that.

Following is my code which is saving picture and just after saving it, it shows ad , and on the adClosed event I want to send that taken picture to other activity , How Can I do that. My code just goes like this ..

 File storagePath = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+ File.separator + "MyAnimals");
        storagePath.mkdirs();
        String finalName = Long.toString(System.currentTimeMillis());

    //this snippet is saving image And I am showing ad after saving picture
  File myImage = new File(storagePath, finalName + ".jpg");

    String photoPath = Environment.getExternalStorageDirectory().getAbsolutePath() +"/" + finalName + ".jpg";

    try {
        FileOutputStream fos = new FileOutputStream(myImage);
        newImage.compress(Bitmap.CompressFormat.JPEG, 100, fos);

        fos.close();
        //refreshing gallery
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        mediaScanIntent.setData(Uri.fromFile(myImage));
        sendBroadcast(mediaScanIntent);
    } catch (IOException e) {
        Toast.makeText(this, "Pic not saved", Toast.LENGTH_SHORT).show();
        return;
    }
    Toast.makeText(this, "Pic saved in: " + photoPath, Toast.LENGTH_SHORT).show();

    displayInterstitial();


         interstitial.setAdListener(new AdListener() {
        @Override
        public void onAdClosed() {
            Log.v("Add time");

       Intent intent = new Intent(CameraActivity.this,ShowCapturedImage.class);

       //Now How to send the saved picture to the image view of other activity?
            startActivity(intent);

            super.onAdClosed();
        }
    });
ghost talker
  • 402
  • 5
  • 15

2 Answers2

2

1) put taken image path in intent

2) get path in other activity and set it in imageview

public static final int REQUEST_CODE_FROM_CAMERA = 112;
private Uri fileUri;
String image_path = "";

//Catch image from below function

 private void fromCamera() {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

            fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
            Log.d("FROM CAMERA CLICKED file uri", fileUri.getPath());
            intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

            // start the image capture Intent
            startActivityForResult(intent, REQUEST_CODE_FROM_CAMERA);
        }

//On Activity result store image path

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_FROM_CAMERA
                && resultCode == Activity.RESULT_OK) {
            try {


                image_path = fileUri.getPath();


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

        }
}

On Click of any button

Intent iSecond=new Intent(FirstActivity.this,SecondActivity.class);
iSecond.putExtra("image_path",image_path);
startActivity(iSecond);

In Second Activity onCreate()

 Bundle extras = intent.getExtras();
    if(extras != null)
    String image_path = extras.getString("image_path");

From this image path , You can get image and set to imageview

ImageView iv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        iv = (ImageView) findViewById(R.id.imageView1);

        File imgFile = new File("/storage/emulated/0/1426484497.png");

        if (imgFile.exists()) {

            Bitmap myBitmap = BitmapFactory.decodeFile(imgFile
                    .getAbsolutePath());

            iv.setImageBitmap(myBitmap);

        }

    }
Darsh Patel
  • 1,015
  • 9
  • 15
  • how to do that , please I know how to put extra with intent but on the other side I do not know what should I use, getIntExtra or what? – ghost talker Mar 27 '15 at 14:09
  • I am using custom camera.I Can not use your fromCamera () code – ghost talker Mar 27 '15 at 15:13
  • Did you get any image path in that? – Darsh Patel Mar 27 '15 at 17:18
  • Yes I am taking picture and then saving it after compressing it. Now I want to show a picture to a user after all functions (including effects and saving in a device) has been done once. – ghost talker Mar 30 '15 at 05:58
  • 1)Send photo path in other activity like : Intent intent = new Intent(CameraActivity.this,ShowCapturedImage.class); intent.putExtra("photoPath", photoPath ) startActivity(intent); 2)You can get your image path in other activity from below code : Bundle b = getIntent().getExtras(); String photoPath = b.getInt("photoPath"); 3) From image path you can display image like below : http://stackoverflow.com/questions/4181774/show-image-view-from-file-path-in-android – Darsh Patel Mar 30 '15 at 06:29
  • I have this path /storage/emulated/0/1427698562951.jpg , ,its not in the Sd card so Can I show this image – ghost talker Mar 30 '15 at 06:57
  • How, can you please share me link , bcz the previous link that you have send it to me , It is loading from the external storage. – ghost talker Mar 30 '15 at 07:14
  • Please help me I am not getting it – ghost talker Mar 30 '15 at 07:50
  • from your this answer , I think I have to put image_path into the follwoing line File imgFile = new File("/storage/emulated/0/1426484497.png"); RIGHT?? – ghost talker Mar 30 '15 at 08:45
  • Yes put your image path in my static path – Darsh Patel Mar 30 '15 at 08:50
  • Vipul I am getting this exception Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/1427705287880.jpg: open failed: ENOENT (No such file or directory) – ghost talker Mar 30 '15 at 08:52
  • Please check image is exist in that path or not – Darsh Patel Mar 30 '15 at 09:02
  • Vipul wait I am adding some lines above the code which are making the directory with the name of MyAnimals – ghost talker Mar 30 '15 at 09:05
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/74082/discussion-between-ghost-talker-and-vipul-patel). – ghost talker Mar 30 '15 at 09:08
0

You can do this by adding a ImageView to your Activity.

A simple ImageView should look like this:

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView" />

Then you capture it in your Activity class

ImageView imageView = (ImageView) this.findViewById(R.id.imageView);

Now the fun part. We'll capture your image using the image URI and parse it in a bitmap.

Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath()); //Here goes your image path
imageView.setImageBitmap(Bitmap.createScaledBitmap(bitmap,imageView.getWidth(), imageView.getHeight(), false)); //I scale the bitmap so it show properly. If the image is too big, it wont show on the ImageView

That should do the trick, tell me if it works!

  • that will create problem , As it would also be unable to show current taken picture. – ghost talker Mar 27 '15 at 13:52
  • To get the current taken picture you need to change your intent call to startActivityForResult(intent, request_code). Then you override the onActivityResult method and get the image path from there. This links shows how it can be done http://developer.android.com/guide/topics/media/camera.html – Thyen Hong Guedes Chang Mar 27 '15 at 14:04
  • I am using custom camera – ghost talker Mar 27 '15 at 14:10