Android:如何使用相机拍照并将全分辨率图像存储在imageview中?

时间:2016-07-08 17:13:56

标签: android android-image-capture

我正在尝试创建一个应用程序,用户可以在其中拍照并将该图片存储在imageview中。我已经尝试了很多方法来实现这一点。

我查看的很多答案建议使用data.getExtra(" data")使用MediaStore意图并传入ACTION_CAMERA_CAPTURE。这将返回较小的图像,我尝试显示的图像很大,因此使用此方法图像显示模糊。

另一种选择是使用相同的方法并传入EXTRA_OUTPUT。但是,这需要放入存储位置,我看到的每个答案都告诉我放入Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)。但是,当我尝试在onActivityResult中执行data.getData()时,我得到null。我该怎么做?

alert.setPositiveButton("Take Photo", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        profileFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "image_" +
                                String.valueOf(System.currentTimeMillis()) + ".jpg");

        profile = Uri.fromFile(profileFile);
                        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        intent.putExtra(MediaStore.EXTRA_OUTPUT, profile);


        startActivityForResult(intent, 1);
    }
});


protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 1 && resultCode == RESULT_OK) {

        ImageView imageView = (ImageView) findViewById(R.id.ProfileImage);

        //This returns null. 
        //I am guessing that the Environment.getExternalSt...() method is getting a storage 
        //location that does not exist. 
        //I don't know what the correct storage location is and I have not 
        //been able to find it.
        Uri u = data.getData();

        File finalFile = new File(getRealPathFromURI(u));

        Picasso.with(this).load(finalFile).into(imageView);

    }
}

1 个答案:

答案 0 :(得分:0)

  

我猜测Environment.getExternalSt ...()方法正在获取一个不存在的存储位置。

可能不是。但是,您假设相机应用程序会将EXTRA_OUTPUT值返回给您。 ACTION_IMAGE_CAPTURE文档中没有任何内容表明会发生这种情况。

  

我不知道正确的存储位置是什么,我找不到它。

你知道图像应该在哪里。它应该位于您在EXTRA_OUTPUT中提供的位置。所以,去那里看看图像是否存在。但是,请确保将profileImage置于已保存的实例状态Bundle中,因为无法保证在摄像头应用程序位于前台时您的进程将继续运行。另外,请记住,有许多错误的ACTION_IMAGE_CAPTURE实现,因此无法保证图像将在您请求的位置,尽管它应该是。

例如,以下是使用ACTION_IMAGE_CAPTURE拍摄全分辨率照片的活动,然后使用ACTION_VIEW让某些图像查看器显示结果:

/***
 Copyright (c) 2008-2016 CommonsWare, LLC
 Licensed under the Apache License, Version 2.0 (the "License"); you may not
 use this file except in compliance with the License. You may obtain a copy
 of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
 by applicable law or agreed to in writing, software distributed under the
 License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
 OF ANY KIND, either express or implied. See the License for the specific
 language governing permissions and limitations under the License.

 From _The Busy Coder's Guide to Android Development_
 https://commonsware.com/Android
 */

package com.commonsware.android.camcon;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import java.io.File;

public class CameraContentDemoActivity extends Activity {
  private static final String EXTRA_FILENAME=
    "com.commonsware.android.camcon.EXTRA_FILENAME";
  private static final String FILENAME="CameraContentDemo.jpeg";
  private static final int CONTENT_REQUEST=1337;
  private File output=null;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    if (savedInstanceState==null) {
      File dir=
        Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);

      dir.mkdirs();
      output=new File(dir, FILENAME);
    }
    else {
      output=(File)savedInstanceState.getSerializable(EXTRA_FILENAME);
    }

    if (output.exists()) {
      output.delete();
    }

    i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(output));

    startActivityForResult(i, CONTENT_REQUEST);
  }

  @Override
  protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    outState.putSerializable(EXTRA_FILENAME, output);
  }

  @Override
  protected void onActivityResult(int requestCode, int resultCode,
                                  Intent data) {
    if (requestCode == CONTENT_REQUEST) {
      if (resultCode == RESULT_OK) {
        Intent i=new Intent(Intent.ACTION_VIEW);

        i.setDataAndType(Uri.fromFile(output), "image/jpeg");
        startActivity(i);
        finish();
      }
    }
  }
}

(来自this sample app

从长远来看,这不是一个很好的解决方案,如file Uri values are going awayofficial training pagethis revamped edition of my sample app显示使用FileProvider代替。