简体   繁体   English

将相机拍摄的图像设置为墙纸的相机应用程序无法获得理想的结果

[英]Not getting desired result for a camera app that sets the image taken as wallpaper

this is camera.java file 这是camera.java文件

    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 这是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 因此,当我按下“设置墙纸”按钮时,它只是将默认图标设置为墙纸

In your onActivityResult , you put: onActivityResult ,您放入:

 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: 编辑:尝试使用此onActivityResult而不是您的:

   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! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM