2016-06-10 2 views
0

Я пытаюсь захватить фотографию и отобразить ее в ImageView. Первая часть выполнена успешно, и изображение будет сохранено:Захват фотографии и отображение ее полного размера в ImageView в 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); 
} 

Однако, я не могу получить изображение, отображаемое в полном размере в 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>

И Manifes t.XML файл:

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

Я получаю следующее сообщение об ошибке:

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

Любая помощь приветствуется.

ответ

1

Проверьте docs на параметр decodeFile метода pathName.

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

Вам необходимо пройти полный путь файла образа в BitmapFactory.decodeFile().

Замените следующую строку:

BitmapFactory.decodeFile("imageFile"); 

с:

BitmapFactory.decodeFile(imageFile.getAbsolutePath()); 

Кроме того, вы, кажется, отсутствуют следующие разрешения в вашем Manifest.xml:

android.permission.WRITE_EXTERNAL_STORAGE 

Так манифеста следует выглядят так:

<?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+ необходимо запросить это разрешение во время выполнения.

Check this ссылка на вопрос о том, как запросить разрешения во время выполнения.

Смежные вопросы