2016-06-10 3 views
0

사진을 캡처하여 ImageView에 표시하려고합니다.사진을 캡처하여 Android의 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 레이아웃은 다음과 같습니다 :

나는 이미지가 이미지 뷰에서 전체 크기로 표시되지 수

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

을하지만 : 첫 번째 부분은 성공적으로 수행되고 이미지가 저장됩니다

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

decodeFile 메서드의 pathName 매개 변수에서 docs을 확인하십시오.

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+ 런타임에이 권한을 요청해야합니다.

런타임 권한을 요청하는 방법에 대해서는 this 링크를 확인하십시오.

관련 문제