2015-03-09 3 views
0

Я уже сделал операцию TakePicture, которая захватила бы изображение, а затем сохранила его в моей телефонной галерее. Моя проблема заключается в том, что я хочу сохранить каждую снимок с помощью приложения в новой андроидной деятельности под названием «Галерея», но я не знаю, как это сделать.Android Studio - сделайте снимок и сохраните его в новом действии

Вот код для съемки изображения:

import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.app.Activity; 
import android.content.ActivityNotFoundException; 
import android.content.Intent; 
import android.graphics.Bitmap; 
import android.net.Uri; 
import android.os.Bundle; 
import android.provider.MediaStore; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.ImageView; 
import android.widget.Toast; 


public class TakePicture extends ActionBarActivity implements OnClickListener { 
    //keep track of camera capture intent 
    final int CAMERA_CAPTURE = 1; 
    //captured picture uri 
    private Uri picUri; 
    final int PIC_CROP = 2; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     Button btn6 = (Button)findViewById(R.id.button240); 

     btn6.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       startActivity(new Intent(TakePicture.this, YesTracker.class)); 
      } 
     }); 


     Button btn8 = (Button)findViewById(R.id.button241); 

     btn8.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       startActivity(new Intent(TakePicture.this, Home.class)); 
      } 
     }); 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_take_picture); 
     //retrieve a reference to the UI button 
     Button captureBtn = (Button)findViewById(R.id.capture_btn); 
     //handle button clicks 
     captureBtn.setOnClickListener(this); 
    } 
    public void onClick(View v) { 
     if (v.getId() == R.id.capture_btn) { 
      try { 
       //use standard intent to capture an image 
       Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
       //we will handle the returned data in onActivityResult 
       startActivityForResult(captureIntent, CAMERA_CAPTURE); 
      } catch(ActivityNotFoundException anfe){ 
       //display an error message 
       String errorMessage = "Whoops - your device doesn't support capturing images!"; 
       Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT); 
       toast.show(); 
      } 
     } 
    } 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (resultCode == RESULT_OK) { 
      if(requestCode == CAMERA_CAPTURE){ 
       picUri = data.getData(); 
       performCrop(); 
      } //user is returning from cropping the image 
      else if(requestCode == PIC_CROP){ 
       //get the returned data 
       Bundle extras = data.getExtras(); 
       //get the cropped bitmap 
       Bitmap thePic = extras.getParcelable("data"); 
       //retrieve a reference to the ImageView 
       ImageView picView = (ImageView)findViewById(R.id.picture); 
       //display the returned cropped image 
       picView.setImageBitmap(thePic); 
      } 
     } 
    } 
    private void performCrop(){ 
     try { 
      //call the standard crop action intent (the user device may not support it) 
      Intent cropIntent = new Intent("com.android.camera.action.CROP"); 
      //indicate image type and Uri 
      cropIntent.setDataAndType(picUri, "image/*"); 
      //set crop properties 
      cropIntent.putExtra("crop", "true"); 
      //indicate aspect of desired crop 
      cropIntent.putExtra("aspectX", 1); 
      cropIntent.putExtra("aspectY", 1); 
      //indicate output X and Y 
      cropIntent.putExtra("outputX", 256); 
      cropIntent.putExtra("outputY", 256); 
      //retrieve data on return 
      cropIntent.putExtra("return-data", true); 
      //start the activity - we handle returning in onActivityResult 
      startActivityForResult(cropIntent, PIC_CROP); 
     } 
     catch(ActivityNotFoundException anfe){ 
      //display an error message 
      String errorMessage = "Whoops - your device doesn't support the crop action!"; 
      Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT); 
      toast.show(); 
     } 
    } 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_take_picture, menu); 
     return true; 
    } 
    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 
} 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="BACK" 
    android:id="@+id/button240" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentStart="true" /> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="HOME" 
    android:id="@+id/button241" 
    android:layout_alignBottom="@+id/button240" 
    android:layout_alignParentEnd="true" /> 

<Button 
    android:id="@+id/capture_btn" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/capture" 
    android:layout_marginTop="77dp" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" /> 

<ImageView 
    android:id="@+id/picture" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:contentDescription="@string/picture" 
    android:layout_centerVertical="true" 
    android:layout_centerHorizontal="true" 
    android:background="@drawable/pic_border" /> 

Любые идеи, как я могу это сделать?

+0

Вы можете указать путь, по которому будет сохранена фотография. На сайте разработчика есть учебник по тому же. Взгляните сюда - http://developer.android.com/training/camera/photobasics.html#TaskPath – Varun

ответ

0

Вы не можете «сохранить» что-либо в Activity, Activity - это экран, который отображает данные и позволяет пользователю взаимодействовать с вашим приложением - это очень грубое определение. Но вы можете хранить пути к фотографиям в базе данных SQLite или в файле JSON. Таким образом, когда вы открываете активность «Галерея», вы можете отображать фотографии, читая пути из хранилища и выполняя необходимую логику для этих путей.

Смежные вопросы