NullPointerException on getContentResolver().query

All your problems with Audio, Video and Images.

NullPointerException on getContentResolver().query

Postby spengilley » Thu Nov 11, 2010 2:54 pm

Hi there,


*edit* I have added the entire code for the class now rather than just the method where the error was found. I am hoping someone can help!!!

I have the following bit of code and I cannot work out why I am getting NullPointerException when calling getContentResolver().query. I have also tryed managedQuery with the same result. It is probably something obvious (I hope!)

Here is the code
Code: Select all
package com.pengilleys.fishingsnapz;

import java.io.FileNotFoundException;
import java.io.IOException;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.ImageView;

public class ImagePreview extends Activity{
   static final int PROGRESS_DIALOG = 0;
   ProgressDialog progressDialog;
   private Bundle extras;
   protected static Bitmap bm;
   protected DBAdapter db = new DBAdapter(ImagePreview.this);
   Long id;
   GetHandler handler = new GetHandler();
   
   
   
   @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.image_view);
        try{
           
           //start a progressdialog
           showDialog(PROGRESS_DIALOG);
           extras = getIntent().getExtras();
           id = (Long) extras.get(DBAdapter.KEY_ROWID);
                  
           db.open();
           
           bm = getImage();          
           
           ImageView imageV = (ImageView)findViewById(R.id.image_preview);
           //Log.e("ONCREATE","setImageBitmap="+bm.toString());
           imageV.setImageBitmap(bm);
           dismissDialog(PROGRESS_DIALOG);
           
           
        }catch(Exception e){
           Log.e("ONCREATE",e.toString());
        }
       
   }
   
   //setup the progressdialog
   protected Dialog onCreateDialog(int id) {
      switch(id) {
        case PROGRESS_DIALOG:
           try{
            progressDialog = new ProgressDialog(this);
            progressDialog.setMessage("Loading snap...");
            progressDialog.setOwnerActivity(this);
            return progressDialog;
        }catch(Exception e){
         Log.e("ONCREATEDIALOG",e.toString());
      }
        default:
            return null;
        }
      
    }
   
   private class GetHandler extends Handler{
      public void handleMessage(Message message){
         ImagePreview.this.getImage();
      }
   }

   public Bitmap getImage(){
      try {
         Uri uri;
         
         Cursor c = db.fetchUri(id);
         uri = Uri.parse(c.getString(c.getColumnIndexOrThrow(DBAdapter.KEY_URI)));
         
         ContentResolver cr = getContentResolver();
            
         Bitmap bitmap = MediaStore.Images.Media.getBitmap(cr,uri);
                     
         //Get orientation details for current image
         String [] cols={MediaStore.Images.ImageColumns.ORIENTATION};
         Cursor info = cr.query(uri,cols,null,null,null);
         Log.e("GETIMAGE","info="+info.toString());
         int orientation = info.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.ORIENTATION);
         Log.e("GETIMAGE","orientation="+orientation);
         //Rotate bitmap if it is portrait
         if(orientation==-90){
            int w = bitmap.getWidth();
            int h = bitmap.getHeight();
               
              Log.e("ONCREATE","Matrix mx=");
              Matrix mx = new Matrix();
              Log.e("ONCREATE","ms.postRotate");
              mx.postRotate(90);
              
              Log.e("ONCREATE","bm=Bitmap");
              bitmap = Bitmap.createBitmap(bitmap,0,0,w,h,mx,true);
         }
         Log.e("ONCREATE",bitmap.toString());
         return bitmap;
            } catch (FileNotFoundException e) {
               // TODO Auto-generated catch block
               Log.e("GETIMAGE",e.toString());
               return null;
            } catch (IOException e) {
               // TODO Auto-generated catch block
               Log.e("GETIMAGE",e.toString());
               return null;
            }catch(Exception e){
               Log.e("GETIMAGE",e.toString());
               return null;
            }
         
   };
      

}
Last edited by spengilley on Fri Nov 26, 2010 7:47 pm, edited 1 time in total.
spengilley
Junior Developer
Junior Developer
 
Posts: 18
Joined: Tue Sep 07, 2010 9:14 pm

Top

Re: NullPointerException on getContentResolver().query

Postby spengilley » Fri Nov 19, 2010 8:03 pm

*nudge* someone must have an idea :(
spengilley
Junior Developer
Junior Developer
 
Posts: 18
Joined: Tue Sep 07, 2010 9:14 pm

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: Google [Bot] and 16 guests