2013-04-25 109 views
0

現在我已經有了一個通過意圖拍照的應用程序。拍完照片後,我希望用戶能夠確認並將照片推送到可以添加詳細信息的其他活動。我正在努力尋找將照片從活動傳遞到活動的正確方法。拍照,成功後在新活動中顯示照片縮略圖

現在,確認照片會將用戶帶回主要活動。

這裏是我的相關代碼:

主要活動

public void takePhoto(View view) { 
    // create Intent to take a picture and return control to the calling application 
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 

    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); 
    Log.d("MySecondApp",fileUri.toString());// create a file to save the image 
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name 

    // start the image capture Intent 
    startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) { 
     if (resultCode == RESULT_OK) { 
      if (data!=null) { 
      // Image captured and saved to fileUri specified in the Intent 
      Toast.makeText(this, "Image saved to:\n" + 
        data.getData(), Toast.LENGTH_LONG).show(); 
      } 
     } else if (resultCode == RESULT_CANCELED) { 
      // User cancelled the image capture 
     } else { 
      // Image capture failed, advise user 
     } 
    } 
} 

/** Create a File for saving an image or video */ 
private static File getOutputMediaFile(int type){ 

    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
       Environment.DIRECTORY_PICTURES), "MySecondApp"); 

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

    // Create a media file name 
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new  java.util.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

我會嘗試像

if(resultCode == Activity.RESULT_OK) 
    handleCameraPhoto(data); 

處理方法:

private void handleCameraPhoto(Intent intent) { 
    Bundle extras = intent.getExtras(); 
    Bitmap mImageBitmap = (Bitmap) extras.get("data"); 
    Intent intent = new Intent(this, NewActivity.class); 
    intent.putExtra("BitmapImage", mImageBitmap); 
    startActivity(intent); 
} 

,然後在你的其他活動

Intent intent = getIntent(); 
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage"); 

檢索它並將其分配給您的ImageView

ImageView iv = (ImageView)findViewById(R.id.myImageView); 
iv.setImageBitmap(bitmap); 
+0

只是修復它,現在工作。 – armandooj 2013-04-26 16:47:14

+0

這幾乎是解決方案,我只需要讓'ImageView'成爲一個成員字段。所以,'mImageView iv =(ImageView)...'。然後你需要聲明常量'private ImageView mImageView'。謝謝你讓我到那裏! – 2013-05-02 16:53:20

0

請在您的新類的構造函數,它接受一個意圖,甚至是圖像:

class newClass { 
    public newClass(Intent intent){ 
     ... 
    } 
    ... 
} 

然後只是使意圖在這個新類的縮略圖的大小,你可以在彈出窗口或類似的東西做確認,然後在這個類的頁面上填寫信息。這也會更好,因爲如果他們想重拍照片,你可以在照片拍攝後拍攝這個課程,如果不接受,可以拍攝另一張照片。

0

看看這個博客文章中,我就如何採取一個全尺寸的圖像和縮略圖版本,以及寫道:

Use Camera Activity for Thumbnail and Full Size Image

你做了之後,你可以提取保存的文件路徑文件從文件實例中使用g etAbsolutePath()方法並將其傳遞給以下Activity

+0

感謝您的指導!你能否告訴我如何實現'getAbsolutePath()'和其餘的縮略圖代碼? – 2013-04-25 21:47:37

+0

我不明白你的問題,你有什麼麻煩? – 2013-04-26 10:39:16

+0

對不起,我試圖在照片保存後將所有照片傳遞給另一個視圖。我猜我應該使用'getAbsolutePath'來做到這一點,但我不知道如何將它的其餘部分放到代碼中,因爲我仍然吮吸java。你知道如何寫出來嗎? – 2013-04-26 16:26:40

相關問題