2017-11-15 14 views
0

ギャラリーから画像を選択してアクティビティに表示したいしかし、コードは正しく動作していません。電話帳からイメージを選択できますが、選択したイメージは表示されません。 logcatを確認すると、これらのエラーが発生しました。私はjavaファイル、xmlファイル、およびマニフェストを以下に提供しています。 Androidの上でギャラリーから選択した画像をアンドロイドで表示できません

11-15 16:31:23.795 18921-18921/com.example.emma.gallaryview E/MultiWindowProxy: getServiceInstance failed! 
11-15 16:31:34.970 18921-18921/com.example.emma.gallaryview E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/DCIM/Camera/IMG_20171114_202150.jpg: open failed: EACCES (Permission denied) 

javaファイル

public class MainActivity extends AppCompatActivity { 
    private static int RESULT_LOAD_IMG = 1; 
    String imgDecodableString; 

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

    public void loadImagefromGallery(View view) { 
     // Create intent to Open Image applications like Gallery, Google Photos 
     Intent galleryIntent = new Intent(Intent.ACTION_PICK, 
       android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
     // Start the Intent 
     startActivityForResult(galleryIntent, RESULT_LOAD_IMG); 
    } 


    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     try { 
      // When an Image is picked 
      if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK 
        && null != data) { 
       // Get the Image from data 

       Uri selectedImage = data.getData(); 
       String[] filePathColumn = {MediaStore.Images.Media.DATA}; 

       // Get the cursor 
       Cursor cursor = getContentResolver().query(selectedImage, 
         filePathColumn, null, null, null); 
       // Move to first row 
       cursor.moveToFirst(); 

       int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
       imgDecodableString = cursor.getString(columnIndex); 
       cursor.close(); 
       ImageView imgView = (ImageView) findViewById(R.id.imgView); 
       // Set the Image in ImageView after decoding the String 
       imgView.setImageBitmap(BitmapFactory 
         .decodeFile(imgDecodableString)); 

      } else { 
       Toast.makeText(this, "You haven't picked Image", 
         Toast.LENGTH_LONG).show(); 
      } 
     } catch (Exception e) { 
      Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG) 
        .show(); 
     } 

    } 

} 

xmlファイル

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <ImageView 
     android:id="@+id/imgView" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" > 
    </ImageView> 

    <Button 
     android:id="@+id/buttonLoadPicture" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_weight="0" 
     android:onClick="loadImagefromGallery" 
     android:text="Load Picture" > 
    </Button> 

</LinearLayout> 

とマニフェスト

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.emma.gallaryview"> 
    <uses-permission android:name="android.permission.CAMERA"/> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
    <uses-feature android:name="android.hardware.camera"/> 
    <uses-feature android:name="android.hardware.camera.autofocus"/> 
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:roundIcon="@mipmap/ic_launcher_round" 
     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> 

答えて

1

あなたのユーザーに確認するためのコードを追加する必要が6+ appを使用して、マニフェストでリクエストした権限を確認します。

実行時のアクセス許可。

さらにコードは複雑です。取得したURIの入力ストリームを開いて、ストリームからビットマップファクトリをデコードする必要があります。

InputStream is = getContentResolver().openInputStream(data.getData()); 

そして、最後に

BitmapFactory.decodeStream(is). 

を使用します。decodeStreamは、デバイスの多くにはnullを返した場合のdontはびっくりします。デコードファイルと同じように。

+0

さらに、PicassoやGlideなどの画像読み込みライブラリを使用することをお勧めします。 – CommonsWare

関連する問題