【问题标题】:Take photo, display photo thumbnail in new activity upon success拍照,成功后在新活动中显示照片缩略图
【发布时间】:2013-04-25 18:10:58
【问题描述】:

现在我有一个应用程序可以通过意图拍照。拍完照片后,我希望用户能够确认它并将照片推送到另一个他们可以添加详细信息的活动。我正在努力寻找将照片从活动传递到活动的正确方法。

现在,确认照片会将用户带回到主要活动。

这是我的相关代码:

主要活动

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;
}

【问题讨论】:

    标签: java android android-intent camera


    【解决方案1】:

    我会尝试类似的东西

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

    【讨论】:

    • 这几乎是解决方案,我只需要将ImageView 设为成员字段。所以,mImageView iv = (ImageView)...。然后你需要声明常量private ImageView mImageView。谢谢你让我到那里!
    【解决方案2】:

    在你的新类中创建一个构造函数,它接受一个意图甚至图像:

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

    然后在这个新类中将意图设为缩略图大小,您可以在弹出窗口或类似内容中进行确认,然后在该类的页面上填写信息。这也会更好,因为如果他们想重拍照片,你可以上这个班,如果不接受,他们在拍完照片后会被带到另一张照片。

    【讨论】:

      【解决方案3】:

      看看我写的这篇关于如何拍摄全尺寸图像和缩略图版本的博文:

      Use Camera Activity for Thumbnail and Full Size Image

      完成此操作后,您可以使用 getAbsolutePath() 方法从文件实例中提取已保存文件的文件路径并将其传递给以下Activity

      【讨论】:

      • 感谢您的指导!你能告诉我如何实现getAbsolutePath() 以及其余的缩略图代码吗?
      • 我不明白你的问题,你有什么问题?
      • 抱歉,我正试图在保存照片后将照片传递到另一个视图。我猜我应该使用getAbsolutePath 来做到这一点,但我不确定如何将其余部分放入代码中,因为我仍然不喜欢java。你知道如何写出来吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-12
      相关资源
      最近更新 更多