2015-02-24 35 views
0

所以我想構建一個Android應用程序,它使用相機並在內部(在應用程序內)保存圖像,然後顯示您剛剛在下一個活動中拍攝的圖像...但我無法輸出圖像。我相信我已經保存了正確的圖片...但我不確定如何讀取數據。我看了很多教程,但無法找到我應該在下一個活動中顯示圖像的方法。下面是主要活動代碼:Android相機應用程序:如何在內部保存後顯示圖片?

import android.hardware.Camera; 
import android.net.Uri; 
import android.os.Environment; 
import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 
import android.widget.FrameLayout; 
import android.widget.Toast; 

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.text.SimpleDateFormat; 
import java.util.Date; 


public class MainActivity extends ActionBarActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    // Create an instance of Camera 
    Camera mCamera = getCameraInstance(); 

    // Create our Preview view and set it as the content of our activity. 
    CameraPreview mPreview = new CameraPreview(this, mCamera); 
    FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview); 
    preview.addView(mPreview); 

    // Add a listener to the Capture button 
    Button captureButton = (Button) findViewById(R.id.button_capture); 

    captureButton.setOnClickListener(new View.OnClickListener() { 

             @Override 
             public void onClick(View v) { 
              Camera mCamera = getCameraInstance(); 
              // get an image from the camera 
              mCamera.takePicture(null, null, mPicture); 
             } 
            } 
    ); 





} 
private Camera.PictureCallback mPicture = new Camera.PictureCallback() { 

    @Override 
    public void onPictureTaken(byte[] data, Camera camera) { 

     File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE); 
     if (pictureFile == null){ 
      Log.d("Logtag:", "Error creating media file, check storage permissions: " 
        ); 
      return; 
     } 

     try { 
      FileOutputStream fos = new FileOutputStream(pictureFile); 
      fos.write(data); 
      fos.close(); 
     } catch (FileNotFoundException e) { 
      Log.d("Logtag:", "File not found: " + e.getMessage()); 
     } catch (IOException e) { 
      Log.d("Logtag:", "Error accessing file: " + e.getMessage()); 
     } 
    } 
}; 


@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_main, 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); 
} 
/** A safe way to get an instance of the Camera object. */ 
public Camera getCameraInstance(){ 
    Camera c = null; 
    try { 
     c = Camera.open(); // attempt to get a Camera instance 
    } 
    catch (Exception e){ 
     // Camera is not available (in use or does not exist) 
     Toast.makeText(getApplicationContext(), "Camera is not available (in use or does not exist)", 
       Toast.LENGTH_LONG).show(); 
    } 
    return c; // returns null if camera is unavailable 
} 





public static final int MEDIA_TYPE_IMAGE = 1; 
public static final int MEDIA_TYPE_VIDEO = 2; 

/** Create a file Uri for saving an image or video */ 
private static Uri getOutputMediaFileUri(int type){ 
    return Uri.fromFile(getOutputMediaFile(type)); 
} 

/** Create a File for saving an image or video */ 
private static File getOutputMediaFile(int type){ 
    // To be safe, you should check that the SDCard is mounted 
    // using Environment.getExternalStorageState() before doing this. 

    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
      Environment.DIRECTORY_PICTURES), "MyCameraApp"); 
    // This locat ion works best if you want the created images to be shared 
    // between applications and persist after your app has been uninstalled. 

    // Create the storage directory if it does not exist 
    if (! mediaStorageDir.exists()){ 
     if (! mediaStorageDir.mkdirs()){ 
      Log.d("Logtag", "failed to create directory"); 
      return null; 
     } 
    } 

    // Create a media file name 
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
    File mediaFile; 
    if (type == MEDIA_TYPE_IMAGE){ 
     mediaFile = new File(mediaStorageDir.getPath() + File.separator + 
       "IMG_"+ timeStamp + ".jpg"); 
    } else if(type == MEDIA_TYPE_VIDEO) { 
     mediaFile = new File(mediaStorageDir.getPath() + File.separator + 
       "VID_"+ timeStamp + ".mp4"); 
    } else { 
     return null; 
    } 

    return mediaFile; 
} 

}

回答

0

傳遞給在意向的其他活動圖像文件的路徑或URI。使包含ImageView的佈局膨脹。使用BitmapFactory解碼圖像文件。在ImageView上設置結果位圖。

0

我知道這可能不是您正在尋找的答案,但您是否考慮過使用圖庫來顯示您剛剛拍攝的照片?

我通常使用畢加索,我很滿意它。

Picasso.with(context).load(pictureFile.getAbsolutePath()).into(view); 
0

您需要將文件中的數據進行解碼: http://square.github.io/picasso/

如果你有一個形象的路徑和視圖來顯示圖像,你基本上可以在一行代碼像顯示圖像。爲了節省內存,位圖縮小到適合您的顯示器的東西(MAXSIZE)

public static Bitmap decodeFile(File f, final int maxSize) { 

    Bitmap b = null; 
    // Decode image size 
    BitmapFactory.Options o = new BitmapFactory.Options(); 
    o.inJustDecodeBounds = true; 

    FileInputStream fis = null; 
    try { 
     fis = new FileInputStream(f); 
     BitmapFactory.decodeStream(fis, null, o); 
     fis.close(); 

     int scale = 1; 
     if (o.outHeight > maxSize || o.outWidth > maxSize) { 
      scale = (int) Math.pow(2, (int) Math.round(Math.log(maxSize/(double) Math.max(o.outHeight, o.outWidth))/Math.log(0.5))); 
     } 

     // Decode with inSampleSize 
     BitmapFactory.Options o2 = new BitmapFactory.Options(); 
     o2.inSampleSize = scale; 

     fis = new FileInputStream(f); 
     b = BitmapFactory.decodeStream(fis, null, o2); 

    } catch (Exception e) { 
     Log.e(ImageUtils.TAG, "Error processing bitmap", e); 
    } finally { 
     FileUtil.closeQuietly(fis); 
    } 


    return b; 
} 

位圖然後可以在ImageView的使用

mImageView.setImageBitmap(bitmap); 
+0

對不起,我這對我來說真的很新鮮。位圖解碼器放在第二個活動中,是否正確?我如何將相機活動中的文件傳遞給第二個將顯示它的活動? – nnnnnnitters 2015-02-24 22:09:14

+0

當您開始新的活動時,您可以將'額外'添加到'意圖'。在那裏你可以指定文件名。例如。 'intent.putExtra(「THE_FILE」,文件名)' – 2015-02-24 22:16:20

相關問題