1

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

我看过的很多答案都建议使用 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);

    }
}
4

1 回答 1

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

(来自这个示例应用程序

从长远来看,这不是一个很好的解决方案,因为file Uri价值观正在消失官方培训页面,以及我的示例应用程序的这个改版版本,改为使用FileProvider

于 2016-07-08T17:17:53.217 回答