在Android中的ImageView中捕获照片并将其显示为完整尺寸

时间:2016-06-10 13:15:06

标签: java android camera imageview

我正在尝试拍摄照片并将其显示在ImageView中。第一部分成功完成并保存图像:

    public void takePhoto (View view) {
    String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
    String fileName = "myPhoto.jpg";
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    imageFile = new File(baseDir + File.separator + fileName);
    Uri uri = Uri.fromFile(imageFile);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
    intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
    startActivityForResult(intent, 0);
}

但是,我无法在ImageView中以完整尺寸显示图像:

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

           if (requestCode == 0)
        {
            if(imageFile.exists())
                    {
                        Toast.makeText(this,"file saved!",Toast.LENGTH_SHORT).show();

                    ImageView myImage = (ImageView) findViewById(R.id.imageView1);
                        Bitmap bmImg = BitmapFactory.decodeFile("imageFile");
                        myImage.setImageBitmap(bmImg);
                    }

                    else
                    {
                        Toast.makeText(this,"file not saved!",Toast.LENGTH_SHORT).show();
                    }
        }
}

XML布局是:



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.example.android.trial3.MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Shoot"
        android:onClick="takePhoto"/>
   
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView1"/>

</LinearLayout>
&#13;
&#13;
&#13;

Manifest.XML文件:

&#13;
&#13;
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.android.trial3">

    <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">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

</manifest>
&#13;
&#13;
&#13;

我收到以下错误:

BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0

感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

查看decodeFile方法的pathName参数上的docs

  

String:要解码的文件的完整路径名。

您需要将图像文件的绝对路径传递给BitmapFactory.decodeFile()

替换以下行:

BitmapFactory.decodeFile("imageFile");

使用:

BitmapFactory.decodeFile(imageFile.getAbsolutePath());

此外,您似乎错过了Manifest.xml中的以下权限:

android.permission.WRITE_EXTERNAL_STORAGE

所以你的Manifest应该是这样的:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.android.trial3">

    <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">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

API级别23 + 上,您需要在运行时请求此权限。

检查this链接,了解如何请求运行时权限。