【问题标题】:Android: How do I take a picture with the camera and store the full resolution image in imageview?Android:如何用相机拍照并将全分辨率图像存储在 imageview 中?
【发布时间】:2016-07-08 17:13:55
【问题描述】:

我正在尝试制作一个应用程序,用户可以在其中拍照并将该照片存储在图像视图中。我已经尝试了几十种方法来让它发挥作用。

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

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

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

    }
}

【问题讨论】:

    标签: android android-image-capture


    【解决方案1】:

    我猜测 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 代替显示。

    【讨论】:

    • EXTRA_OUTPUT 没有返回是什么意思?如果这是错误的,我如何获取意图数据?另外,我在手机上查找了存储位置,但找不到。我确实通过在我的电脑上查看我的手机存储找到了它。这是位置 [链接] (i.imgur.com/MgAYa9U.png)(不知道是否有帮助)。你知道我怎样才能把它存放在画廊的位置吗? [链接] (i.imgur.com/GhdIWB8.png)
    • 实际上它试图获取的文件夹是错误的。它给了我这个不存在的位置/storage/emulated/0/Pictures/。它应该在最后一张图片中给我图片目录。
    • @kindOfABigDeal:“EXTRA_OUTPUT 没有返回是什么意思?” -- 我的意思是你需要保留你在EXTRA_OUTPUT 中输入的值。该值不会在结果Intent 中返回给您。 “实际上它试图获取的文件夹是错误的”——很可能是external storage 的正确位置。
    • @kindOfABigDeal:我用一些示例代码更新了答案,以说明如何使用EXTRA_OUTPUT
    • 它仍然给我存储位置。它不去电话 DCIM。另外,有没有办法不用去图库选择就可以得到图片?
    猜你喜欢
    • 1970-01-01
    • 2016-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-17
    • 1970-01-01
    • 2012-10-26
    相关资源
    最近更新 更多