2017-11-15 12 views
0

Ich möchte Bild aus der Galerie auswählen und laden oder in der Aktivität zeigen. Aber der Code funktioniert nicht richtig. Es kann nur Bilder aus der Telefongalerie auswählen, zeigt jedoch das ausgewählte Bild nicht an. Das Überprüfen des Logcat hat diese Fehler bekommen. Ich stelle die Java-Datei, die XML-Datei und das Manifest zur Verfügung.Fehler beim Anzeigen des ausgewählten Bildes aus der Galerie in 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-Datei

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

    } 

} 

Die XML-Datei

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

und manifestieren

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

Antwort

1

Auf Android 6+ Sie Code hinzufügen müssen, um den Benutzer fragen Ihrer App, um die Berechtigungen zu bestätigen, die Sie im Manifest angefordert haben.

Google für Laufzeitberechtigungen.

Weiter ist Ihr Code viel zu kompliziert. Sie sollten einfach einen Eingabestream für das erhaltene URI öffnen und dann Bitmap-Factory-Dekodierung aus dem Dampf lassen.

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

Dann

verwenden
BitmapFactory.decodeStream(is). 

Schließlich: dont überrascht, wenn decodeStream auf eine Menge von Geräten null zurückgibt. So wie decodeFile.

+0

Oder, noch besser, verwenden Sie eine Bildlade-Bibliothek wie Picasso oder Glide. – CommonsWare

Verwandte Themen