add image to imageview from Uri

All your problems with Audio, Video and Images.

add image to imageview from Uri

Postby raynor84 » Wed Oct 19, 2011 6:09 pm

Hi,

I'm trying to show an image in a imageView which I selected from the Gallery. But somehow the Method setImageBitmap throws a NullPointerException. even though the Uri is correct(I supose: content://media/external/images/media/9).

Here the code:
Code: Select all
package dig.example.selectpic;

import java.io.FileNotFoundException;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class SelectPicActivity extends Activity implements OnClickListener {
    private static final int IMAGE_SELECTOR = 1;
   /** Called when the activity is first created. */
   private ImageView iview;
   private Button addpic;
   @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        addpic = (Button) findViewById(R.id.button1);
        addpic.setOnClickListener(this);
    }
   @Override
   public void onClick(View v) {
      if(v==addpic) {
         Intent intent = new Intent(Intent.ACTION_PICK);
         intent.setType("image/*");
         startActivityForResult(intent, IMAGE_SELECTOR);


      }
   }
   public void onActivityResult(int requestCode, int resultCode, Intent intent) {
      super.onActivityResult(requestCode, resultCode, intent);
      
      Bitmap bitmap = null;
      if (requestCode == IMAGE_SELECTOR) {
         if (resultCode == RESULT_OK) {
            Log.e("IMAGE_SELECTOR", "IFs entered in onActivityResult");
            Uri selectedImageUri = intent.getData();
            Log.e("selectedImageUri", String.valueOf(selectedImageUri));

            try {
               bitmap = BitmapFactory.decodeStream(getContentResolver()
                     .openInputStream(selectedImageUri));
               iview.setImageBitmap(bitmap);

            } catch (FileNotFoundException e) {
               e.printStackTrace();
            }
         }
      }
   }

   private String getPath(Uri selectedImageUri) {
      //Which Columns to return
      String[] projection = {MediaStore.Images.Media.DATA };
      Cursor cursor = managedQuery(selectedImageUri, projection, null, null, null);
      //Verwaltet cursor je nach Zustand Pause, resume usw.
      startManagingCursor(cursor);
      if(cursor != null) {
         int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
         cursor.moveToFirst();
      //Gibt den String von dem Column mit dem Index.
         return cursor.getString(column_index);
      } else {
         return null;
      }
   }
}


Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
<ImageView android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="@drawable/icon" android:id="@+id/imageView"></ImageView>
<Button android:layout_width="wrap_content" android:text="@string/button" android:id="@+id/button1" android:layout_height="wrap_content"></Button>
</LinearLayout>



I used the ImageView but if there are alternatives, i wouldn't mind any other kind of view.
My Goal is to select a Picture/Pictures from the Gallery/Camera and display it in a Layout (e.g. Gridview). And Upload them over the Internet (e.g. FTP)

Would be nice, if anyone could help me... :)
Last edited by raynor84 on Mon Oct 24, 2011 7:50 am, edited 1 time in total.
raynor84
Freshman
Freshman
 
Posts: 6
Joined: Wed Oct 19, 2011 5:06 pm

Top

Re: add image to imageview from Uri

Postby raynor84 » Thu Oct 20, 2011 12:48 pm

any idea?
raynor84
Freshman
Freshman
 
Posts: 6
Joined: Wed Oct 19, 2011 5:06 pm

Re: add image to imageview from Uri

Postby redbulletnl » Thu Oct 20, 2011 1:34 pm

hmm posted a question in a tut topic, i wonder why noone replied.
redbulletnl
Experienced Developer
Experienced Developer
 
Posts: 84
Joined: Wed Sep 14, 2011 7:44 am

Re: add image to imageview from Uri

Postby raynor84 » Thu Oct 20, 2011 3:33 pm

yes, i already asked someone, to move the post^^
raynor84
Freshman
Freshman
 
Posts: 6
Joined: Wed Oct 19, 2011 5:06 pm

Re: add image to imageview from Uri

Postby redbulletnl » Fri Oct 21, 2011 9:58 am

the admins are death :(
redbulletnl
Experienced Developer
Experienced Developer
 
Posts: 84
Joined: Wed Sep 14, 2011 7:44 am

Re: add image to imageview from Uri

Postby raynor84 » Fri Oct 21, 2011 11:28 am

unfortunately, there are no options to move this thread...I could delete it optionally.
raynor84
Freshman
Freshman
 
Posts: 6
Joined: Wed Oct 19, 2011 5:06 pm

Top

Re: add image to imageview from Uri

Postby raynor84 » Wed Nov 02, 2011 8:49 am

I am able to add an image per Bitmapfactory... my last problem is to add a created ImageView to the GridView, after capturing/selecting a picture.
raynor84
Freshman
Freshman
 
Posts: 6
Joined: Wed Oct 19, 2011 5:06 pm

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 12 guests