Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to transfer the image from one activity to another

I have to transfer the image from one activity to another. In first activity the user selects the image out of several images from scroll view and that image has to be displayed in the imageview of next activity. Help required.

like image 896
Arslan Ali Khan Avatar asked Nov 05 '12 05:11

Arslan Ali Khan


People also ask

How do you pass image from one activity to another activity Kotlin?

This example demonstrates how to pass an image from one activity to another activity in Android using Kotlin. Step 1 − Create a new project in Android Studio, go to File ⇒New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How do I add an image to an activity?

For adding an image from Android Studio, Drag the ImageView widget to the activity area of the application, a pop-up dialogue box will open choose from the wide range of drawable resources and click “OK“.

How do I transfer data between Android activities?

We can send the data using the putExtra() method from one activity and get the data from the second activity using the getStringExtra() method.


1 Answers

You can do this with many way. simple is with intent. but it may hang your device and it also give you out of memory Exception in many device like Galaxy S3.

so i ll give you very simple way see below.

you can create static variable in one class like :

public class ABC{

public static Bitmap PHOTO = null;

}

now when you get bitmap from gallery or any other way then you have to save bitmap in this PHOTO variable.(this is possible in only onActivityResult, am i right?)

if you get photo from camera then code is.

Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_PIC_REQUEST);

and,

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

            switch (requestCode) {
                case CAMERA_PIC_REQUEST:
                    Bitmap b = (Bitmap) data.getExtras().get("data");
                    if (b != null) {
                        ABC.PHOTO = b;
                    }
                    break;
}
}

and use this PHOTO variable in any other Activity.

you can use this same way when pick photo from gallery.


hi this is edited ans.

this is just sample example of grid view. here you get idea about how pass image from one activity to another.

this is your main Activity class:

package com.GridViewDemo;

import java.io.InputStream;
import java.net.URL;
import java.util.GregorianCalendar;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class GridViewDemoActivity extends Activity {
    /** Called when the activity is first created. */

    // String[] mArr =
    // {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
    String[] mArr = {
            "http://www.xda-developers.com/wp-content/uploads/2012/10/androidfigure.jpg?f39ce1",
            "http://1.bp.blogspot.com/-Ramp-o0Cp8s/T0VafXkE4uI/AAAAAAAAAQU/i703zg5MBgI/s1600/android-wallpaper5_1024x768.jpg",
            "http://www.thebiblescholar.com/android_awesome.jpg",
            "http://blogs-images.forbes.com/rogerkay/files/2011/07/Android1.jpg" };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        GridView gridView = (GridView) findViewById(R.id.gridView1);
        gridView.setAdapter(new ImageAdapter(this));

        gridView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub

                /** if you have bitmap here then you can use this way
                 * Bitmap bitmap = getBitmap();
                 * test.PHOTO = bitmap; 
                 * 
                 * */
                Intent i = new Intent(GridViewDemoActivity.this, newActiivty.class);
                i.putExtra("Image_Path", ""+mArr[arg2]);
                startActivity(i);
            }
        });
    }

    public class ImageAdapter extends BaseAdapter {

        private Context mContext;

        public ImageAdapter(Context c) {
            mContext = c;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return mArr.length;
        }

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            ImageView imgView;
            if (convertView == null) {
                imgView = new ImageView(mContext);
                imgView.setLayoutParams(new GridView.LayoutParams(85, 85));
                imgView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imgView.setPadding(8, 8, 8, 8);
            } else {
                imgView = (ImageView) convertView;
            }

            Drawable d = LoadImageFromWebOperations(mArr[position]);
            if (d == null) {
                imgView.setImageResource(R.drawable.icon);
            } else {
                imgView.setImageDrawable(d);
            }
            return imgView;
        }

    }

    public static Drawable LoadImageFromWebOperations(String url) {
        try {
            InputStream is = (InputStream) new URL(url).getContent();
            Drawable d = Drawable.createFromStream(is, "src name");
            return d;
        } catch (Exception e) {
            return null;
        }
    }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <GridView android:id="@+id/gridView1" android:layout_height="wrap_content"
        android:layout_width="fill_parent" android:numColumns="4"></GridView>
</LinearLayout>

newActivity.class

package com.GridViewDemo;

import java.io.InputStream;
import java.net.URL;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ImageView;

public class newActiivty extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.new_layout);

        String image_path = getIntent().getStringExtra("Image_Path");

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

        Drawable d = LoadImageFromWebOperations(image_path);
        if (d != null) {
            imageview.setImageDrawable(d);
        } else {
            imageview.setImageResource(R.drawable.icon);
        }



        /** if you have bitmap then
         * imageview.setImageBitmap(test.PHOTO);
         * */
    }

    public static Drawable LoadImageFromWebOperations(String url) {
        try {
            InputStream is = (InputStream) new URL(url).getContent();
            Drawable d = Drawable.createFromStream(is, "src name");
            return d;
        } catch (Exception e) {
            return null;
        }
    }

}

new_layout.xml

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout android:id="@+id/linearLayout1"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <ImageView android:src="@drawable/icon" android:id="@+id/imageView1"
        android:layout_width="fill_parent" android:layout_height="320dp"></ImageView>
</LinearLayout>

manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.GridViewDemo"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="3" />
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".GridViewDemoActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".newActiivty"></activity>

    </application>
</manifest>

this is extra class: if you have bitmap then use this way:

package com.GridViewDemo;

import android.graphics.Bitmap;

public class test {

    public static Bitmap PHOTO = null;

}

i comment in code so, check it and if you have query then comment below this ans.

like image 64
Dhaval Parmar Avatar answered Sep 29 '22 12:09

Dhaval Parmar