I want to transfer the image from one activity to another
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.
In your first Activity
Convert ImageView to Bitmap
imageView.buildDrawingCache();
Bitmap bitmap = imageView.getDrawingCache();
Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("BitmapImage", bitmap);
In second Activity
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");
Then display bitmap in ImageView.
Note: this is not recommended. Should actually save the image somewhere and pass the path instead and retrieve from second activity.