Datenübergabe zwischen Activities

N

NewPerson1

Neues Mitglied
0
Hallo,
ich habe eine Kamera-App gemacht.
Mein Problem: ich möchte das geschossene Bild auf eine andere Activity(Layout) anzeigen lassen. Leider weiß ich nicht, wie man das mit Bilder umsetzt (intent, putExtra, getExtra)!!!!!

Also es gibt eine MainActivity + main(Layout) & eine ZweiteActivity + zweite(Layout)

Das bedeutet, zuerst wird die main-layout angezeigt. Danach macht man ein Foto und dieses Bild soll dann auf zweite-layout angezeigt werden. Ich hoffe ihr könnt mich helfen. :)

//MainActivity
Code:
public class MainActivity extends Activity implements OnClickListener{
	
	Button bSnap;
	ImageView ivPic;
	Intent iBild;
	Bitmap bmp;
	public static final int KamerCode = 15;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		initialize();
		InputStream is = getResources().openRawResource(R.drawable.ic_launcher);
		bmp = BitmapFactory.decodeStream(is);
	}

	private void initialize() {
		ivPic = (ImageView) findViewById(R.id.ivPic);
		bSnap = (Button) findViewById(R.id.bSnap);
		bSnap.setOnClickListener(this);
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()){
		case R.id.bSnap:
			iBild = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
			//iBild = new Intent(MainActivity.this, ZweiteActivity.class);
            //startActivityForResult(iBild, KamerCode);
			startActivityForResult(iBild, KamerCode);
			break;
		}		
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == KamerCode){
			if (resultCode == RESULT_OK){
				Bundle extras = data.getExtras();
				bmp = (Bitmap) extras.get("data");
				ivPic.setImageBitmap(bmp);	
			}
		}
	}	
}

//Layout für MainActivity
Code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="20dp"
    tools:context="com.picchat.paier.MainActivity" >  

        <ImageView
            android:id="@+id/ivPic"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:src="@drawable/ic_launcher"
            android:layout_marginTop="250dp"
            android:layout_marginLeft="70dp"/>

        <Button
            android:id="@+id/bSnap"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"      
            android:text="Snap"
            android:layout_marginLeft="20dp" />

        <TextView
            android:id="@+id/tvAppName"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="17dp"
            android:text="PicChat"
            android:textStyle="bold"
            android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

//Layout für ZweiteActivity
Code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="20dp"
    tools:context="com.picchat.paier.MainActivity" >  

        <ImageView
            android:id="@+id/ivPic2"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:src="@drawable/ic_launcher"
            android:layout_marginTop="250dp"
            android:layout_marginLeft="70dp" />

        <Button
            android:id="@+id/bSnap"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"      
            android:text="Fertig"
            android:layout_marginLeft="20dp" />

        <TextView
            android:id="@+id/tvAppName"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="17dp"
            android:text="PicChat"
            android:textStyle="bold"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <Button
            android:id="@+id/bNeu"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/bSnap"
            android:layout_centerHorizontal="true"
            android:text="New"
            android:layout_marginLeft="20dp" />

        <EditText
            android:id="@+id/etText"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/bNeu"
            android:layout_alignParentLeft="true"
            android:ems="10" />

</RelativeLayout>
 
Du könntest eine extra static Klasse (z.b Memory) machen in der du das Bitmap in der 1. Activity setzt und in der 2. Activity dann holst. So in etwa:

public class Memory{

static Bitmap image;

}

Activity 1:
Memory.image=deinBild;

Activity 2:
imageView.setBitmap(Memory.image);
 
Zuletzt bearbeitet:
Ihhhh gittt man macht das über Internets und nicht so ein scheiß...

Lg. Dagobert
 
du meinst intents sicherlich ;)
 
Ja die gute Handy Tastatur versteht mich nicht immer xd
 
Über nen Intent würd ichs vll so lösen:

In der MainActivity:

Code:
Intent intent = new Intent(MainActivity.this, zweiteActivity.class);
        Bundle b = new Bundle();
        bundle.putParcelable("image", bitmap);
        intent.putExtras(b);
        startActivity(intent);
und in der zweiten so:

Code:
onCreate.....
Bitmap bitmap = getIntent().getParcelableExtra("image");
dient jetzt nur als Beispiel zum verdeutlichen was mit den intents gemeint ist.

Prob.: Das "putParcelable" ist soweit ich weiß auf 1Mb Dateigröße beschränkt und geht auf die performance. Besser wäre es das Bild irgendwo abzuspeichern und in der zweiten activity zu laden.

Gruß Cynob
 
Danke für die Antworten.
Wie kann man das über Intents lösen, also bei Bildern?

Bei strings kann man das so schreiben und wie geht das bei Bildern????
Intent i = new Intent(MainActivity.this, ZweitesActivity.class);
i.putExtra(KEY, edit.getText().toString());
startActivity(i);

Oder liege ich da FALSCH?

Der ursprüngliche Beitrag von 10:53 Uhr wurde um 10:58 Uhr ergänzt:

Danke Cynob :)
Ich probier es mal aus.
 
Hallo mach das doch mit

Code:
startActivityForResult(new Intent(.....));


mit freundlichen grüßen
 
mmmmh speciherst dud ie bilder in bitmaps dann geht das doch eigentlich ganz einfach das du das bitmap objekt übergibtst
 
Danke für die Hilfe: :)
Ich habs hinbekommen.
 

Ähnliche Themen

M
Antworten
3
Aufrufe
169
moin
M
M
Antworten
3
Aufrufe
855
maksimilian
M
W
Antworten
1
Aufrufe
850
jogimuc
J
Zurück
Oben Unten