[英]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.
需要帮助。
In your first Activity在您的第一个活动中
Convert ImageView to Bitmap将 ImageView 转换为位图
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.然后在 ImageView 中显示位图。
Note: this is not recommended.注意:不推荐这样做。 Should actually save the image somewhere and pass the path instead and retrieve from second activity.
实际上应该将图像保存在某处并传递路径并从第二个活动中检索。
you can pass it as a byte array and build the bitmap for display in the next activity.您可以将其作为字节数组传递并构建位图以在下一个活动中显示。 For instance:
例如:
In your first activity:在您的第一个活动中:
Intent i = new Intent(this, NextActivity.class);
Bitmap b; // your bitmap
ByteArrayOutputStream bs = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.PNG, 50, bs);
i.putExtra("byteArray", bs.toByteArray());
startActivity(i);
In your second activity在你的第二个活动中
if(getIntent().hasExtra("byteArray")) {
ImageView previewThumbnail = new ImageView(this);
Bitmap b = BitmapFactory.decodeByteArray(
getIntent().getByteArrayExtra("byteArray"),0,getIntent().getByteArrayExtra("byteArray").length);
previewThumbnail.setImageBitmap(b);
}
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.
但它可能会挂起您的设备,并且还会在许多设备(如 Galaxy S3)中为您提供内存不足的异常。
so i ll give you very simple way see below.所以我会给你非常简单的方法,见下文。
you can create static
variable in one class like :您可以在一个类中创建
static
变量,例如:
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?)现在,当您从图库或任何其他方式获取位图时,您必须将位图保存在此 PHOTO 变量中。(这仅在 onActivityResult 中是可能的,对吗?)
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.并在任何其他活动中使用此 PHOTO 变量。
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新建Activity.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 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.我在代码中进行评论,因此请检查它,如果您有疑问,请在此 ans 下方评论。
in first Activity first Make Sure you instantiate the bitmap at the top like Bitmap bitmap;在第一个活动中,首先确保像位图位图一样在顶部实例化位图;
and then use this :- in the first Activity然后使用这个 :- 在第一个活动中
ByteArrayOutputStream bs = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, bs);
byte[] byteArray = bs.toByteArray();
intent.putExtra("PICTURE", byteArray);
startActivity(intent);
and in the second activity:-在第二项活动中:-
byte[] byteArray = extras.getByteArray("PICTURE");
bitmap = BitmapFactory.decodeByteArray(byteArray,0,byteArray.length);
imgViewResult.setImageBitmap(bitmap);
You can pass the URI of the Image to next Activity.您可以将图像的 URI 传递给下一个活动。
the URI which you get from onActivityResult()您从 onActivityResult() 获得的 URI
and in onCreate() of the next Activity.并在下一个活动的 onCreate() 中。
Decode the Bitmap again and set it to ImageView再次解码Bitmap并将其设置为ImageView
Use putExtra in first activity when you start second Activity.当您启动第二个活动时,在第一个活动中使用 putExtra。 And in second Activity use getExtra() as follow:
在第二个活动中使用 getExtra() 如下:
In First Activity在第一个活动中
Intent intent = new Intent(Activity1.this,Activity2.class);
intent.putExtra("bmp",selectedImage);
startActivity(intent);
In Second Activity在第二个活动中
Bitmap bmp = this.getIntent().getParcelableExtra("bmp");
将您的图像转换为base64
字符串格式并将其传递给下一个活动,然后再次将其解码为位图。
In first Activity :在第一个活动中:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK){
Intent showImageIntent = new Intent(this, ShowCameraPicture.class);
showImageIntent.fillIn(data,Intent.FILL_IN_DATA);
startActivity(showImageIntent);
}
}
In second Activity :在第二个活动中:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getIntent().getExtras();
if(bundle!=null){
Bitmap image = (Bitmap)bundle.get("data");
//do whatever you need to do with the bitmap here ...
}
}
//////////firstActivity.xml //////////第一个Activity.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.app.myapp.com.app44sendinginfofromoneactivitytoanother.MainActivity">
<RelativeLayout
android:layout_width="368dp"
android:layout_height="495dp"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:orientation="vertical">
<EditText
android:id="@+id/edtName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter NAme"
android:inputType="textPersonName" />
<EditText
android:id="@+id/edtLastName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter LastName"
android:inputType="textPersonName" />
<EditText
android:id="@+id/edtEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter Email"
android:inputType="textPersonName" />
<EditText
android:id="@+id/edtPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter Password"
android:inputType="textPassword" />
<ImageView
android:id="@+id/imgView"
android:layout_width="match_parent"
android:layout_height="200dp"
app:srcCompat="@android:drawable/ic_menu_report_image" />
<RadioGroup
android:id="@+id/rdoGroup"
android:layout_width="match_parent"
android:layout_height="50dp">
<RadioButton
android:id="@+id/rdoMale"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Male" />
<RadioButton
android:id="@+id/rdoFemale"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Female" />
</RadioGroup>
<Button
android:id="@+id/btnSend"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Send" />
</LinearLayout>
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
////////////// MainActivity.java ///////////// MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener,CompoundButton.OnCheckedChangeListener{
EditText edtName,edtLastName,edtEmail,edtPassword;
ImageView imgView;
RadioButton rdoMale,rdoFemale;
Button btnSend;
String genderType = "";
int CAMERA_PIC_REQUEST = 99;
Bitmap bitmap; // your bitmap
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtName = (EditText) findViewById(R.id.edtName);
edtLastName = (EditText) findViewById(R.id.edtLastName);
edtEmail = (EditText) findViewById(R.id.edtEmail);
edtPassword = (EditText) findViewById(R.id.edtPassword);
btnSend = (Button) findViewById(R.id.btnSend);
imgView = (ImageView) findViewById(R.id.imgView);
rdoMale = (RadioButton) findViewById(R.id.rdoMale);
rdoFemale = (RadioButton) findViewById(R.id.rdoFemale);
btnSend.setOnClickListener(MainActivity.this);
imgView.setOnClickListener(MainActivity.this);
rdoMale.setOnCheckedChangeListener(MainActivity.this);
rdoFemale.setOnCheckedChangeListener(MainActivity.this);
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch (buttonView.getId()){
case R.id.rdoMale:
if (isChecked){
genderType = "Male";
}else{
buttonView.setChecked(false);
}
break;
case R.id.rdoFemale:
if (isChecked){
genderType = "Female";
}else{
buttonView.setChecked(false);
}
break;
}
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.btnSend:
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra("NAME",edtName.getText().toString());
intent.putExtra("LASTNAME",edtLastName.getText().toString());
intent.putExtra("EMAIL",edtEmail.getText().toString());
intent.putExtra("PASSWORD",edtPassword.getText().toString());
intent.putExtra("GENDER",genderType);
//below is the code which you are looking for
--> ByteArrayOutputStream bs = new ByteArrayOutputStream();
--> bitmap.compress(Bitmap.CompressFormat.PNG, 100, bs);
byte[] byteArray = bs.toByteArray();
intent.putExtra("PICTURE", byteArray);
startActivity(intent);
break;
case R.id.imgView:
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
break;
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode,resultCode,data);
if (requestCode == CAMERA_PIC_REQUEST) {
bitmap = (Bitmap) data.getExtras().get("data");
imgView.setImageBitmap(bitmap);
}
}
}
//////////////////// SecondActivity.xml /////////////////// SecondActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.app.myapp.com.app44sendinginfofromoneactivitytoanother.ResultActivity">
<RelativeLayout
android:layout_width="368dp"
android:layout_height="495dp"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:orientation="vertical">
<TextView
android:id="@+id/txtName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="FirstName:"
android:textSize="24sp" />
<TextView
android:id="@+id/txtLast"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="LastName:"
android:textSize="24sp" />
<TextView
android:id="@+id/txtEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Email:"
android:textSize="24sp" />
<TextView
android:id="@+id/txtPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Password:"
android:textSize="24sp" />
<TextView
android:id="@+id/txtGender"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Gender:"
android:textSize="24sp" />
<ImageView
android:id="@+id/imgViewResult"
android:layout_width="match_parent"
android:layout_height="200dp"
app:srcCompat="@android:drawable/ic_menu_report_image" />
</LinearLayout>
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
///////// SecondActivity.Java ///////// SecondActivity.Java
public class SecondActivity extends AppCompatActivity {
Context ctx ;
TextView txtName,txtLast,txtEmail,txtPassword,txtGender;
ImageView imgViewResult;
Bitmap bitmap;
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
ctx = this;
setContentView(R.layout.activity_result);
txtName = (TextView) findViewById(R.id.txtName);
txtLast = (TextView) findViewById(R.id.txtLast);
txtEmail = (TextView) findViewById(R.id.txtEmail);
txtPassword = (TextView) findViewById(R.id.txtPassword);
txtGender = (TextView) findViewById(R.id.txtGender);
imgViewResult = (ImageView) findViewById(R.id.imgViewResult);
Bundle extras = getIntent().getExtras();
String Name = extras.getString("NAME");
String LastName = extras.getString("LASTNAME");
String Email = extras.getString("EMAIL");
String Password = extras.getString("PASSWORD");
String Gender = extras.getString("GENDER");
txtName.setText("Name: "+Name);
txtLast.setText("LastName: "+LastName);
txtEmail.setText("Email: "+Email);
txtPassword.setText("Password: "+Password);
txtGender.setText("Gender: "+Gender);
byte[] byteArray = extras.getByteArray("PICTURE");
bitmap = BitmapFactory.decodeByteArray(byteArray,0,byteArray.length);
imgViewResult.setImageBitmap(bitmap);
}
}
HappyCoding :)快乐编码:)
First Activity:第一个活动:
Intent intent = new Intent(CurrentActivity, secondActivity.class);
intent.putExtra("resId", R.drawable.your image);
startActivity(intent);
Then your Target Activity:然后你的目标活动:
Bundle bundle = getIntent().getExtras();
if(bundle!=null){
int image = bundle.getInt("resId");
ImageId.setImageResource(image);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.