2016-04-12 16 views
-1

ich weiß nicht, warum es nicht in meinem Fall funktioniert und funktionieren alle anderen einDont Bild in Bild Ansicht zu erhalten, nachdem Bild aus der Galerie Auswahl

public class MainActivity extends Activity { 
    private static final int imagess = 1; 
    Button uploadimage, loadimage; 
    ImageView selectimage, showimage; 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     uploadimage = (Button) findViewById(R.id.uploadimage); 
     loadimage = (Button) findViewById(R.id.loadimage); 
     selectimage = (ImageView) findViewById(R.id.imageUpToLoad); 
     showimage = (ImageView) findViewById(R.id.Imagedownload); 


     selectimage.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent galleryintent = new Intent(Intent.ACTION_PICK , MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
       startActivityForResult(galleryintent , imagess); 
      } 
     }); 
    } 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     if (requestCode == imagess && requestCode == RESULT_OK && data != null) { 
      Uri imageuri = data.getData(); 
      selectimage.setImageURI(imageuri); 

und dies ist xml-Code, der auch perfekt. Dies ermöglicht mir den Zugriff auf die Galerie, aber wenn ich das Bild aus der Galerie auswähle, wird es in der Bildansicht nicht angezeigt. weiß nicht, warum es nicht

<ImageView 
     android:layout_width="150dp" 
     android:layout_height="150dp" 
     android:id="@+id/imageUpToLoad" 
     android:layout_gravity="center_horizontal" 
     /> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="upload image" 
     android:id="@+id/uploadimage" 
     android:layout_below="@+id/selectimage" 
     android:layout_alignParentStart="true" 
     android:layout_alignEnd="@+id/selectimage" /> 

    <ImageView 
     android:layout_width="150dp" 
     android:layout_height="150dp" 
     android:id="@+id/Imagedownload" 
     android:layout_gravity="center_horizontal" /> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Load image" 
     android:id="@+id/loadimage" 
     android:layout_below="@+id/imageView2" 
     android:layout_alignParentStart="true" 
     android:layout_alignEnd="@+id/imageView2" /> 
+0

Welchen Wert erhalten Sie in 'imageuri'? –

+0

nichts in Bildansicht nach Auswahl Bild aus der Galerie. Ich folgte nur dem Video mit demselben Code. Arbeit in Video der obige Code, aber funktioniert nicht in meinem Fall –

+0

Drucken Sie den Wert von 'imageuri' in Protokollen und sagen Sie mir, was Sie darin bekommen. –

Antwort

0

Arbeits Bitte bearbeiten onActivityResult

if (requestCode == imagess && requestCode == RESULT_OK && data != null) { 
     Uri imageuri = data.getData(); 
     String[] projection = { MediaStore.Images.Media.DATA }; 
     Cursor cursor = managedQuery(imageuri, projection, null, null, null); 
     int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
     cursor.moveToFirst(); 
     String picturePath = cursor.getString(columnIndex); 
     cursor.close(); 
     selectimage.setImageBitmap(BitmapFactory.decodeFile(picturePath)); 
} 
+0

es immer noch nicht funktioniert. Gehe zur Galerie und wähle auch Arbeit, aber das Bild wird nicht in der Bildansicht nach der Auswahl des Bildes aus der Galerie angezeigt. Eine andere Sache ist, dass ich das vedio mit dem gleichen Code folgte, der es im Video arbeitete, aber nicht in meinem Fall –

+0

Ich redigierte meine Antwort, können Sie dieses versuchen? –

+0

dasselbe Ergebnis bedeutet nicht funktioniert –

0

bereits. Ändern Sie Ihre onActivityResult in diesem

protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{ 
    super.onActivityResult(requestCode, resultCode, data); 
    if (requestCode == imagess && requestCode == RESULT_OK && data != null) { 
     Uri imageuri = data.getData(); 
     try 
     { 
      Bitmap bitmapImage = decodeBitmap(imageuri); 
     } 
     catch (FileNotFoundException e) 
     { 
      e.printStackTrace(); 
     } 
     selectimage.setImageBitmap(bitmapImage); 
} 

Und fügen Sie diese Methode

public Bitmap decodeBitmap(Uri selectedImage) throws FileNotFoundException { 
    BitmapFactory.Options o = new BitmapFactory.Options(); 
    o.inJustDecodeBounds = true; 
    BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o); 

    final int REQUIRED_SIZE = 100; 

    int width_tmp = o.outWidth, height_tmp = o.outHeight; 
    int scale = 1; 
    while (true) { 
     if (width_tmp/2 < REQUIRED_SIZE || height_tmp/2 < REQUIRED_SIZE) { 
      break; 
     } 
     width_tmp /= 2; 
     height_tmp /= 2; 
     scale *= 2; 
    } 

    BitmapFactory.Options o2 = new BitmapFactory.Options(); 
    o2.inSampleSize = scale; 
    return BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o2); 
} 
Verwandte Themen