我必須將圖像從一個活動轉移到另一個活動。在第一次活動中,用戶從滾動視圖中的幾張圖像中選擇圖像,並且該圖像必須顯示在下一活動的圖像視圖中。需要幫助。我想將圖像從一個活動轉移到另一個活動
回答
在您的第一活動
轉換的ImageView爲位圖
imageView.buildDrawingCache();
Bitmap bitmap = imageView.getDrawingCache();
Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("BitmapImage", bitmap);
在第二活動
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");
然後在ImageView的顯示位圖。
注意:不建議這樣做。實際上應該將圖像保存在某個地方,然後傳遞路徑並從第二個活動中檢索。
它沒有工作,因爲它提供了putExtra命令的錯誤,並且我必須將具有id「imageview」的圖像視圖的圖像傳送到具有id「圖像」的下一個活動的圖像視圖。 –
更新了我的答案,它應該可以幫助你。 – Raghunandan
謝謝@Raghunandan:我已經搜索了很多這個問題,他們都沒有幫助,你的答案最後爲我工作..謝謝你 –
您可以將圖像的URI傳遞給下一個活動。
你從onActivityResult得到()的URI
,並在接下來的活動中的onCreate()。
解碼位圖再次將其設置爲ImageView的
可以將它作爲一個字節數組,並建立用於顯示該位圖中的下一個活動。例如:
在您的第一項活動:
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);
在你的第二個活動
if(getIntent().hasExtra("byteArray")) {
ImageView previewThumbnail = new ImageView(this);
Bitmap b = BitmapFactory.decodeByteArray(
getIntent().getByteArrayExtra("byteArray"),0,getIntent().getByteArrayExtra("byteArray").length);
previewThumbnail.setImageBitmap(b);
}
putextra無法正常工作 –
見this崗位。
當您啓動第二個活動時,在第一個活動中使用putExtra。而在第二活動使用getExtra()如下:
在第一項活動
Intent intent = new Intent(Activity1.this,Activity2.class);
intent.putExtra("bmp",selectedImage);
startActivity(intent);
在第二個活動
Bitmap bmp = this.getIntent().getParcelableExtra("bmp");
其實putextra s無法正常工作 –
然後使用 ByteArrayOutputStream bs = new ByteArrayOutputStream(); b.compress(Bitmap.CompressFormat.PNG,50,bs); putExtras()之前的 。 –
你可以用很多方式做到這一點。簡單是有意圖的。但它可能會掛起您的設備,並且它也會讓您在Galaxy S3等許多設備中出現內存不足的情況。
所以我會給你很簡單的方式見下文。
您可以創建static
變量在一個類中,如:。
public class ABC{
public static Bitmap PHOTO = null;
}
現在,當你從畫廊或則其他任何方式,你必須保存的位圖在這張照片中的可變位(這是可能只在onActivityResult ,我吧?)
如果你從相機得到的照片,然後代碼是。
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_PIC_REQUEST);
和,
@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;
}
}
並且在任何其它活動使用此PHOTO變量。
您可以使用相同的方式從圖庫中挑選照片。
嗨,這是編輯ans。
這只是網格視圖的示例。在這裏您可以瞭解如何將圖像從一個活動傳遞到另一個活動。
這是你的主Activity類:
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>
清單文件
<?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>
這是額外的類:如果你有位圖,然後用這樣的方式:
package com.GridViewDemo;
import android.graphics.Bitmap;
public class test {
public static Bitmap PHOTO = null;
}
代碼我評論的話,檢查它,如果你有查詢則低於這個ANS評論。
這是當用戶在第一個活動中按下ok按鈕時調用的函數代碼public void ok(View view){ \t Intent intent = new Intent(this,Data.class); EditText editText =(EditText)findViewById(R.id.text); String message = editText.getText()。toString(); intent.putExtra(EXTRA_MESSAGE,message); startActivity(intent); Intent intent1 = new Intent(Template.this,Data.class); ImageView image =(ImageView)findViewById(R.id.imageview); intent1.putExtra(「bmp」,image); startActivity(intent); } –
這是錯誤的。把你的所有代碼放在你的問題中。你正在傳遞imageView。您必須傳遞位圖或圖像路徑,無論您使用哪種方式在滾動視圖中顯示圖像。 –
我必須通過第一個活動的imageview形象imageview imageview好心建議代碼 –
將圖像轉換爲base64
字符串格式,並將其傳遞給下一個活動,並再次將其解碼爲位圖。
在第一活動:
@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);
}
}
在第二活動:
@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
<?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
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
<?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的
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 :)
在第一個活動首先確保你在實例像 位圖位圖上的位圖;
,然後使用此: - 在第一活動
ByteArrayOutputStream bs = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, bs);
byte[] byteArray = bs.toByteArray();
intent.putExtra("PICTURE", byteArray);
startActivity(intent);
,並在第二個活動: -
byte[] byteArray = extras.getByteArray("PICTURE");
bitmap = BitmapFactory.decodeByteArray(byteArray,0,byteArray.length);
imgViewResult.setImageBitmap(bitmap);
- 1. 將圖像從一個活動移動到另一個android
- 2. 想要將圖像從一個活動傳遞到另一個活動
- 3. 從一個活動(數據庫)轉移到另一個活動
- 4. 從一個活動轉移到另一個活動
- 5. 我無法將對象從一個活動轉移到另一個活動
- 6. 如何將圖像從一項活動轉移到另一項活動?
- 7. 如何將Uri圖像從一項活動轉移到另一項活動?
- 8. 如何將對象的ArrayList從一個活動轉移到另一個活動
- 9. Android - 如何將ImageView從一個活動轉移到另一個活動?
- 10. 如何將ListView數據從一個活動轉移到另一個活動
- 11. 將位圖圖像從一個活動傳遞到另一個活動
- 12. 將一個圖像從一個活動移動到android上的另一個活動選擇gridview
- 13. 如何將光標從一個活動移動到另一個活動
- 14. 將spinner項目從一個活動移動到另一個活動
- 15. 將文本從一個活動移動到另一個
- 16. 將圖像從一個活動傳遞到另一個在android
- 17. 將指針從一個活動傳遞到另一個活動
- 18. 將值從一個活動發送到另一個活動
- 19. 將數據從一個活動傳遞到另一個活動
- 20. 將數據從一個活動設置到另一個活動
- 21. 將值從一個活動傳遞到另一個活動
- 22. 將佈局從一個活動傳遞到另一個活動
- 23. 將stringbuilder從一個活動傳遞到另一個活動
- 24. 將數據從一個活動傳遞到另一個活動
- 25. 傳遞圖片從一個活動到另一個活動
- 26. 從一個活動轉到另一個活動Android中的活動
- 27. 我如何拍攝一個活動的圖像並將其移動到另一個活動中
- 28. 如何從一項活動轉移到另一項活動android
- 29. 在Android上從一項活動轉移到另一項活動
- 30. Android共享元素從一個活動轉移到另一個
從一個活動傳遞的位圖到另一個是一個壞主意。 你應該考慮傳遞路徑或URI。 – minhaz