android - 如何让Android Camera Intent返回全尺寸图片

标签 android android-intent camera android-camera

我正在使用这篇文章中的 jengelsma 代码 Capture Image from Camera and Display in Activity从相机获取图像。

当我得到返回的图像时,它很小,当我试图扩大它时,我立即失去了图像质量。

我假设这只是预览尺寸,但是我无法找到返回大图像的方法,因为这是我需要的才能对其进行操作。

供引用的代码是:

package edu.gvsu.cis.masl.camerademo;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MyCameraActivity extends Activity {
private static final int CAMERA_REQUEST = 1888; 
private ImageView imageView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    this.imageView = (ImageView)this.findViewById(R.id.imageView1);
    Button photoButton = (Button) 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);
    }  
} 
}

非常感谢任何帮助。

最佳答案

你必须做一些额外的事情来获得全尺寸图片,它不会在 Intent 中传递它,而是将它存储在一个文件中。

要为此启动相机,请在您的 Intent 中添加一个额外的内容:

Intent intent = new Intent( MediaStore.ACTION_IMAGE_CAPTURE );

fileUri = getOutputMediaFileUri();
intent.putExtra( MediaStore.EXTRA_OUTPUT, fileUri );

getOutputMediaFileUri 看起来像:

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

/** Create a File for saving an image or video */
private static File getOutputMediaFile(){
    // 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 location 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("MyCameraApp", "failed to create directory");
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile = new File(mediaStorageDir.getPath() + File.separator +
        "IMG_"+ timeStamp + ".jpg");

    return mediaFile;
}

当然,您可以修改创建文件的方法,但是您喜欢修改命名。

然后在您的 onActivityResult 中,您只需在 Intent 上调用 getData() 即可获取图片的 URI。

关于android - 如何让Android Camera Intent返回全尺寸图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13000651/

相关文章:

java - 使用 Intent 选择家庭启动器应用程序

video - kivy视频流

java - Android相机应用程序: Capture button not working?

javascript - phonegap应用多语言

java - startActivityForResult 包含三个 Activity

java - 如何从android中的语音合成器保存音频文件(android.speech.tts)?

android - 如何关闭所有 Activity 并退出应用程序

camera - Three.js 相机向上或向下倾斜并保持水平水平

android - 如何对 Android 的 backendless 数据进行排序并隐藏(不删除)重复项?

android - 为免费 Android 应用程序提供高级更新的正确方法