2014-02-18 17 views
2

這是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); 

      } 
     } 


    } 

,這是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> 

,這是清單文件

<?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> 

問題是,當我在我的手機上運行應用程序時, 它會打開相機並拍攝照片,但它不會在應用程序中顯示在圖像視圖中拍攝的圖像。 從相機返回後,拍攝的圖像會丟失。 所以,當我按下設置壁紙按鈕,它只是設置的默認圖標,壁紙

回答

1

在你onActivityResult,你就把:

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

你應該把:

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

這是怎麼了知道結果來自相機意圖...

編輯: 嘗試使用此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); 
     } 
    } 

希望這有助於!

+0

它仍然無法正常工作。應用程序從相機返回後,圖像視圖不會被拍攝的圖片取代。設置壁紙按鈕將默認圖片設置爲壁紙 – bhanu

+0

請參閱我的編輯是答案 – gilonm

+0

圖片視圖在拍攝圖片後仍未更新。 – bhanu