2016-05-20 151 views
0

我想用我的相機拍攝的照片在我的Android應用程序上創建一個畫廊。我已經有了意圖和Manifest許可的相機活動,但我只能拍攝1張照片。這裏是我的代碼:如何在Android上創建照片庫?

MyCameraAction.java

package com.example.android.navigationdrawer; 

import android.content.Intent; 
import android.graphics.Bitmap; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.ImageView; 

public class MyCameraActivity extends AppCompatActivity { 


    private static final int CAMERA_REQUEST = 1888; 
    private ImageView imageView; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.content_my_camera); 
     this.imageView = (ImageView)this.findViewById(R.id.imageView1); 
     ImageView photoButton = (ImageView) this.findViewById(R.id.button1); 
     photoButton.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
       startActivityForResult(cameraIntent, CAMERA_REQUEST); 
      } 
     }); 
    } 

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

} 

content_my_camera.xml(很簡單,只有一個按鈕和一個ImageView的)

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

    <ImageView android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/ic_menu_camera" 
     android:layout_gravity="center_horizontal" 
     android:clickable="true" 
     android:contentDescription="@string/photoButtonUse" 
     /> 

    <ImageView android:id="@+id/imageView1" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:contentDescription="@string/ImageViewGallery" 
     /> 

</LinearLayout> 
+0

您正在設置您的圖像視圖爲可繪製返回,但您只有1個imageview。您需要動態地將圖像視圖添加到屏幕上。 –

+0

我該怎麼做? @paul_hundal – Adir

+0

另外,當我重新打開應用程序時,我希望照片在那裏,我該怎麼做? @paul_hundal – Adir

回答

0

我已經找到了解決辦法,我張貼代碼:

MyCameraActivity

import android.content.Intent; 
import android.graphics.Bitmap; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 

public class MyCameraActivity extends AppCompatActivity { 


    private static final int CAMERA_REQUEST = 1888; 



    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.content_my_camera); 
     ImageView photoButton = (ImageView) this.findViewById(R.id.button1); 


     //OnClick camera button 
     photoButton.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
       startActivityForResult(cameraIntent, CAMERA_REQUEST); 
      } 
     }); 
    } 

    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) { 

      //Cast LinearLayout of .xml 
      LinearLayout CamaraLayout = (LinearLayout) findViewById(R.id.CameraLayout); 

      //Create imageView to put Bitmap 
      ImageView imageView = new ImageView(this); 

      //Adding the Bitmap 
      Bitmap photo = (Bitmap) data.getExtras().get("data"); 
      imageView.setImageBitmap(photo); 

      //Set the imageView to layout 
      CamaraLayout.addView(imageView); 

      //Show layout 

     } 
    } 

} 

當你的圖像,它創建一個新的ImageView並將其位圖設置爲拍攝的圖像