Frage

this is camera.java file

    package com.newpackage.myapp;

    import java.io.IOException;
    import java.io.InputStream;

    import android.app.Activity;
    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.ImageButton;
    import android.widget.ImageView;

    public class Camera extends Activity implements View.OnClickListener{


        ImageButton ib;
        Button b;
        ImageView iv;
        Intent i;
        int cameraResults;
        final static int cameraData = 0;
        Bitmap bmp;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.photo);
            initialize();
            InputStream is = getResources().openRawResource(R.drawable.ic_launcher);
            bmp = BitmapFactory.decodeStream(is);
        }
        private void initialize(){
            iv = (ImageView) findViewById (R.id.ivReturnPic);
            ib = (ImageButton) findViewById(R.id.ibTakePic);
            b = (Button) findViewById(R.id.bSetWall);
            b.setOnClickListener(this);
            ib.setOnClickListener(this);
        }
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch (v.getId()){
            case R.id.bSetWall:
                try {
                    getApplicationContext().setWallpaper(bmp);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            break;
            case R.id.ibTakePic:
                i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(i, cameraData);
            break;

            }
        }
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            // TODO Auto-generated method stub
            super.onActivityResult(requestCode, resultCode, data);
            if (resultCode == RESULT_OK){
                Bundle extras = data.getExtras();
                bmp = (Bitmap) extras.get("data"); 
                iv.setImageBitmap(bmp);

            }
        }


    }

and this is the photo.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/ivReturnPic"
            android:layout_gravity="center"
            android:layout_width="250dp"
            android:layout_height="250dp"
            android:src="@drawable/ic_launcher" />

        <ImageButton
            android:id="@+id/ibTakePic"
                    android:layout_gravity="center"

            android:layout_width="125dp"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />

        <Button
            android:id="@+id/bSetWall"
                    android:layout_gravity="center"

            android:layout_width="125dp"
            android:layout_height="wrap_content"
            android:text="Set Wallpaper" />

    </LinearLayout>

and this is manifest file

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.newpackage.myapp"
        android:versionCode="1"
        android:versionName="1.0" >

        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="8" />
        <uses-permission android:name="android.permission.SET_WALLPAPER"></uses-permission>
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.CAMERA"/>


        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.newpackage.myapp.Splash"
                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="com.newpackage.myapp.Startingpoint"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="com.newpackage.myapp.STARTINGPOINT" />

                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
            </activity>
            <activity
                android:name="com.newpackage.myapp.Menu"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="com.newpackage.myapp.MENU" />

                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
            </activity>
             <activity
                android:name="com.newpackage.myapp.TextPlay"
                android:label="@string/app_name" >

            </activity>
            <activity
                android:name="com.newpackage.myapp.Email"
                android:label="@string/app_name" >

            </activity>
             <activity
                android:name="com.newpackage.myapp.Camera"
                android:label="Camera Application" 
                android:screenOrientation="portrait"
                >


            </activity>


        </application>

    </manifest>

The problem is that when I run the app on my mobile, it opens the camera and takes picture but it doesn't show the image taken in image view in the app. After returning from camera the Image taken is lost. So when I press set wallpaper button it just sets the default icon as the wallpaper

War es hilfreich?

Lösung

In your onActivityResult, you put:

 if (resultCode == RESULT_OK){
...
}

you should put:

 if (resultCode == RESULT_OK && requestCode==cameraData){
    ...
    }

That is how you know the result is coming from the Camera Intent...

EDIT: Try using this onActivityResult instead of yours:

   protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
        if (requestCode == cameraData && resultCode == RESULT_OK) {  
            Bitmap photo = (Bitmap) data.getExtras().get("data"); 
            iv.setImageBitmap(photo);
        }  
    }

Hope this helps!

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top