2016-06-10 13 views
0

Ich versuche, ein Foto zu erfassen und es in einem ImageView anzuzeigen. Der erste Teil ist erfolgreich durchgeführt und das Bild wird gespeichert:Erfassen Sie ein Foto und zeigen Sie es in voller Größe in ImageView in Android

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

Ich kann aber nicht das Bild in voller Größe in der Image angezeigt bekommen:

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

Das XML-Layout ist:

<?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>

Und die Manifes t.XML Datei:

<?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>

Ich erhalte die folgende Fehlermeldung:

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

Jede Hilfe sehr geschätzt wird.

Antwort

1

Überprüfen Sie die docs auf der decodeFile Methode pathName Parameter.

String: complete path name for the file to be decoded.

Sie müssen den absoluten Pfad der Bilddatei zu BitmapFactory.decodeFile() passieren.

Ersetzen Sie die folgende Zeile:

BitmapFactory.decodeFile("imageFile"); 

mit:

BitmapFactory.decodeFile(imageFile.getAbsolutePath()); 

Auch scheinen Sie die folgenden Berechtigungen in Ihrem Manifest.xml zu fehlen:

android.permission.WRITE_EXTERNAL_STORAGE 

Also Ihr Manifest sollte wie folgt aussehen:

<?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> 

Unter API-Level 23+ müssen Sie diese Berechtigung zur Laufzeit anfordern.

Überprüfen Sie this Link auf wie Laufzeit Berechtigungen anfordern.