Android 单击图片后,我无法在ImageView中看到它

Android 单击图片后,我无法在ImageView中看到它,android,Android,我可以连接到相机,并拍照。我可以看到我的文件被加载的地方,目录。但是,我无法在我的应用程序中查看它。 这是我的密码: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_add_photo); takePhoto = (Button) findViewById(

我可以连接到相机,并拍照。我可以看到我的文件被加载的地方,目录。但是,我无法在我的应用程序中查看它。 这是我的密码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_photo);

    takePhoto = (Button) findViewById(R.id.click_photo_button);
    image= (ImageView) findViewById(imageView);
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
        takePhoto.setEnabled(false);
        ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE }, 0);
    }

}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    if (requestCode == 0) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED
                && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
            takePhoto.setEnabled(true);
        }
    }
}
我还添加了摄像头和写入外部存储权限

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="edu.android.notetakingapplication">
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".AddPhotoActivity"
        android:label="@string/addphoto">

    </activity>
    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="${applicationId}.provider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>
    </provider>
</application>


提前谢谢

问题将出现在onActivityResult中。如果你看一看,你可以看到一个预览图像返回的意图数据,你可以使用如果这是所有你需要的是一个微小的图像

否则,您缺少的步骤是转换为内容URI(这就是gallery的工作原理)。当你拍照时(如我链接的页面所述):

或者稍后,在您的onActivityResult中(我还没有测试过这是否有效):


谢谢你,巴德!真是太棒了。我在获取文件的URI时弄乱了包名。太好了!仅供参考-仅从onActivityResult执行该操作是否有效?我通过手动添加包名而不是file=FileProvider.getUriForFile(这是getApplicationContext().getPackageName()+“.provider”,createFileDir());我将通过onActivityResult进行检查并通知您。setImageURI行出现错误:W/ImageView:无法打开内容:content://edu.android.notetakingapplication.provider/external_files/Pictures/NoteTaking/IMG_20170226_142846.jpg -----W/ImageView:resolveUri在错误的位图uri上失败:content://edu.android.notetakingapplication.provider/external_files/Pictures/NoteTaking/IMG_20170226_142846.jpg (很抱歉,格式不适合在注释中添加代码片段)与我的预期相符。我想,当您保存文件时,该函数会在providerdb中创建一个新行,感谢您的尝试
Uri photoURI = FileProvider.getUriForFile(this, "com.example.android.fileprovider", file);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
//remember to change the package
image.setImageURI(FileProvider.getUriForFile(this, "com.example.android.fileprovider", file));