Structure Query Language, C programming, Java, Servlet, Jsp, Unix

Thursday 18 October 2012

Create an application to pick up any image from the native application gallery and display it on the screen.

Pro23Activity.java
Note :  Drop Images on Sdcard then go to menus open "Dev-Tools" -> "Media Scanner"
package ps.pro23;

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

public class Pro23Activity extends Activity implements OnClickListener {
    /**
     *  www.master-gtu.blogspot.com
     *  pankaj sharma(8460479175),
     *  chavda vijay(8460420769) 
     */
 
 ImageView img;
 Button btnext, btprevious;
 Cursor cur;
 int total,current;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        img=(ImageView) findViewById(R.id.imageView1);
        btnext=(Button) findViewById(R.id.buttonnext);
        btprevious=(Button) findViewById(R.id.buttonprevious);
        btprevious.setEnabled(false);
        
        btnext.setOnClickListener(this);
        btprevious.setOnClickListener(this);

//Note Media Class is inside MediaStore.Images for images [see 8th line]
        
        //Uri umedia=Uri.parse(Media.EXTERNAL_CONTENT_URI);
        Uri umedia=Uri.parse("content://media/external/images/media");
        
        cur=managedQuery(umedia, null, null,null,null);
        total=cur.getCount();
        
        current=1;
        cur.moveToNext();
        img.setImageBitmap(BitmapFactory.decodeFile(cur.getString(1)));       
    }
 @Override
 public void onClick(View v) {
  // TODO Auto-generated method stub
  if(v.getId()==btnext.getId())
  {
   current++;
   cur.moveToNext();
   img.setImageBitmap(BitmapFactory.decodeFile(cur.getString(1)));
   if(current==total)
   {
    btnext.setEnabled(false);
   }
   btprevious.setEnabled(true);
  }
  else if(v.getId()==btprevious.getId())
  {
   current--;
   cur.moveToPrevious();
   img.setImageBitmap(BitmapFactory.decodeFile(cur.getString(1)));
   if(current==1)
   {
    btprevious.setEnabled(false);
   }
   btnext.setEnabled(true);
  }
 }
}

No comments:

Post a Comment