Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Help with App

Options
  • 09-06-2014 12:31pm
    #1
    Registered Users Posts: 72 ✭✭


    Hi Folks,

    PLease see attachment of part of my app - a DrawingCanvas - My problem lies with a couple of the buttons on the top - namely the 1st one (Gallery) and the last one (Email)....

    .... What i would like to do is when user clicks the Gallery button - it will open their gallery on their device and let the user select a picture/image which then will re-open on the drawing canvas to let the user draw over this image/picture...

    ...The email button i would like the user when pressed, will attach the current picture/image to the email....

    I can do the intents for both the above but after alot of googling, i can't find out exactly how to attach pictures to both the email and through the gallery to the DrawingCanvas.....

    Any assistance or advice would be much appreciated....

    Thanks in advance....


Comments

  • Closed Accounts Posts: 19,777 ✭✭✭✭The Corinthian


    shanard wrote: »
    .... What i would like to do is when user clicks the Gallery button - it will open their gallery on their device and let the user select a picture/image which then will re-open on the drawing canvas to let the user draw over this image/picture...
    http://stackoverflow.com/questions/2507898/how-to-pick-an-image-from-gallery-sd-card-for-my-app-in-android
    ...The email button i would like the user when pressed, will attach the current picture/image to the email....
    http://stackoverflow.com/questions/1247983/problem-sending-an-email-with-an-attachment-programmatically

    STFW.


  • Registered Users Posts: 72 ✭✭shanard


    HI Folks,

    Thanks for that - i had already tried this code but the problem i think i am having is when i select the image from the gallery, i am not trying to load it onto another ImageView but onto a canvas (I think..... The code for where i want the image from the gallery is as per below...)
    <!--  The following is adding an instance of our custom class, DrawingView -->
    <com.kennshanny.childsplay.DrawingView
        android:id="@+id/drawing"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_marginBottom="3dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="3dp"
        android:layout_weight="1"
        android:background="#FFFFFFFF" />
    


    Thanks in advance!

    K


  • Closed Accounts Posts: 19,777 ✭✭✭✭The Corinthian


    shanard wrote: »
    Thanks for that - i had already tried this code but the problem i think i am having is when i select the image from the gallery, i am not trying to load it onto another ImageView but onto a canvas (I think..... The code for where i want the image from the gallery is as per below...)
    That's the XML of the canvas in your layout, so it's not much use to us; what we'd want to see is what Java code you're using to load the image from your SD card to it.

    There are two steps; getting the file path of the image and loading it into the canvas. The latter part that you claim is causing you trouble, took me 20 seconds to find a solution for online...

    So either you're not explaining your problem here or you need to STFW.


  • Registered Users Posts: 72 ✭✭shanard


    Hi,

    Thanks - here is my java code - now when i press the gallery button it brings me to my gallery no problem and when i select an image, it just returns me to the canvas but no image is loaded......

    Any ideas?
    package com.kennshanny.childsplay;
    
    import java.io.FileNotFoundException;
    import java.io.InputStream;
    import java.util.UUID;
    
    import android.net.Uri;
    import android.os.Bundle;
    import android.provider.MediaStore;
    import android.annotation.SuppressLint;
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.app.Dialog;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.database.Cursor;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.ImageButton;
    import android.widget.ImageView;
    import android.widget.LinearLayout;
    import android.widget.Toast;
    
     
    public class DrawingCanvas extends Activity implements OnClickListener {
    
    	private static final int SELECT_IMAGE = 1;
    	//custom drawing view
    	private DrawingView drawView;
    	//buttons
    	private ImageButton currPaint, drawBtn, eraseBtn, newBtn, saveBtn;
    	//sizes
    	private float smallBrush, mediumBrush, largeBrush;
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_drawing_canvas);
    
    		//get drawing view
    		drawView = (DrawingView)findViewById(R.id.drawing);
    
    		//get the palette and first color button
    		LinearLayout paintLayout = (LinearLayout)findViewById(R.id.paint_colors);
    		currPaint = (ImageButton)paintLayout.getChildAt(0);
    		currPaint.setImageDrawable(getResources().getDrawable(R.drawable.paint_pressed));
    
    		//sizes from dimensions
    		smallBrush = getResources().getInteger(R.integer.small_size);
    		mediumBrush = getResources().getInteger(R.integer.medium_size);
    		largeBrush = getResources().getInteger(R.integer.large_size);
    
    		//draw button
    		drawBtn = (ImageButton)findViewById(R.id.draw_btn);
    		drawBtn.setOnClickListener(this);
    
    		//set initial size
    		drawView.setBrushSize(mediumBrush);
    
    		//erase button
    		eraseBtn = (ImageButton)findViewById(R.id.erase_btn);
    		eraseBtn.setOnClickListener(this);
    
    		//new button
    		newBtn = (ImageButton)findViewById(R.id.new_btn);
    		newBtn.setOnClickListener(this);
    
    		//save button
    		saveBtn = (ImageButton)findViewById(R.id.save_btn);
    		saveBtn.setOnClickListener(this);
    	}
    
    	@Override
    	public boolean onCreateOptionsMenu(Menu menu) {
    		// Inflate the menu; this adds items to the action bar if it is present.
    		getMenuInflater().inflate(R.menu.drawing_canvas, menu);
    		return true;
    	}
    
    	//user clicked paint
    	public void paintClicked(View view){
    		//use chosen color
    
    		//set erase false
    		drawView.setErase(false);
    		drawView.setBrushSize(drawView.getLastBrushSize());
    
    		if(view!=currPaint){
    			ImageButton imgView = (ImageButton)view;
    			String color = view.getTag().toString();
    			drawView.setColor(color);
    			//update ui
    			imgView.setImageDrawable(getResources().getDrawable(R.drawable.paint_pressed));
    			currPaint.setImageDrawable(getResources().getDrawable(R.drawable.paint));
    			currPaint=(ImageButton)view;
    		}
    	}
    
    	@Override
    	public void onClick(View view){
    
    		if(view.getId()==R.id.draw_btn){
    			//draw button clicked
    			final Dialog brushDialog = new Dialog(this);
    			brushDialog.setTitle("Brush size:");
    			brushDialog.setContentView(R.layout.brush_chooser);
    			//listen for clicks on size buttons
    			ImageButton smallBtn = (ImageButton)brushDialog.findViewById(R.id.small_brush);
    			smallBtn.setOnClickListener(new OnClickListener(){
    				@Override
    				public void onClick(View v) {
    					drawView.setErase(false);
    					drawView.setBrushSize(smallBrush);
    					drawView.setLastBrushSize(smallBrush);
    					brushDialog.dismiss();
    				}
    			});
    			ImageButton mediumBtn = (ImageButton)brushDialog.findViewById(R.id.medium_brush);
    			mediumBtn.setOnClickListener(new OnClickListener(){
    				@Override
    				public void onClick(View v) {
    					drawView.setErase(false);
    					drawView.setBrushSize(mediumBrush);
    					drawView.setLastBrushSize(mediumBrush);
    					brushDialog.dismiss();
    				}
    			});
    			ImageButton largeBtn = (ImageButton)brushDialog.findViewById(R.id.large_brush);
    			largeBtn.setOnClickListener(new OnClickListener(){
    				@Override
    				public void onClick(View v) {
    					drawView.setErase(false);
    					drawView.setBrushSize(largeBrush);
    					drawView.setLastBrushSize(largeBrush);
    					brushDialog.dismiss();
    				}
    			});
    			//show and wait for user interaction
    			brushDialog.show();
    		}
    		else if(view.getId()==R.id.erase_btn){
    			//switch to erase - choose size
    			final Dialog brushDialog = new Dialog(this);
    			brushDialog.setTitle("Eraser size:");
    			brushDialog.setContentView(R.layout.brush_chooser);
    			//size buttons
    			ImageButton smallBtn = (ImageButton)brushDialog.findViewById(R.id.small_brush);
    			smallBtn.setOnClickListener(new OnClickListener(){
    				@Override
    				public void onClick(View v) {
    					drawView.setErase(true);
    					drawView.setBrushSize(smallBrush);
    					brushDialog.dismiss();
    				}
    			});
    			ImageButton mediumBtn = (ImageButton)brushDialog.findViewById(R.id.medium_brush);
    			mediumBtn.setOnClickListener(new OnClickListener(){
    				@Override
    				public void onClick(View v) {
    					drawView.setErase(true);
    					drawView.setBrushSize(mediumBrush);
    					brushDialog.dismiss();
    				}
    			});
    			ImageButton largeBtn = (ImageButton)brushDialog.findViewById(R.id.large_brush);
    			largeBtn.setOnClickListener(new OnClickListener(){
    				@Override
    				public void onClick(View v) {
    					drawView.setErase(true);
    					drawView.setBrushSize(largeBrush);
    					brushDialog.dismiss();
    				}
    			});
    			brushDialog.show();
    		}
    		else if(view.getId()==R.id.new_btn){
    			//new button
    			AlertDialog.Builder newDialog = new AlertDialog.Builder(this);
    			newDialog.setTitle("New drawing");
    			newDialog.setMessage("Start new drawing (you will lose the current drawing)?");
    			newDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener(){
    				public void onClick(DialogInterface dialog, int which){
    					drawView.startNew();
    					dialog.dismiss();
    				}
    			});
    			newDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
    				public void onClick(DialogInterface dialog, int which){
    					dialog.cancel();
    				}
    			});
    			newDialog.show();
    		}
    		else if(view.getId()==R.id.save_btn){
    			//save drawing
    			AlertDialog.Builder saveDialog = new AlertDialog.Builder(this);
    			saveDialog.setTitle("Save drawing");
    			saveDialog.setMessage("Save drawing to device Gallery?");
    			saveDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener(){
    				public void onClick(DialogInterface dialog, int which){
    					//save drawing
    					drawView.setDrawingCacheEnabled(true);
    					//attempt to save
    					String imgSaved = MediaStore.Images.Media.insertImage(
    							getContentResolver(), drawView.getDrawingCache(),
    							UUID.randomUUID().toString()+".png", "drawing");
    					//feedback
    					if(imgSaved!=null){
    						Toast savedToast = Toast.makeText(getApplicationContext(), 
    								"Drawing saved to Gallery!", Toast.LENGTH_SHORT);
    						savedToast.show();
    					}
    					else{
    						Toast unsavedToast = Toast.makeText(getApplicationContext(), 
    								"Oops! Image could not be saved.", Toast.LENGTH_SHORT);
    						unsavedToast.show();
    					}
    					drawView.destroyDrawingCache();
    				}
    			});
    			saveDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
    				public void onClick(DialogInterface dialog, int which){
    					dialog.cancel();
    				}
    			});
    			saveDialog.show();
    		}
    	}
    	
    	public void emailPicture(View view) {
    		ImageButton email = (ImageButton) findViewById(R.id.email_btn);
    		if (view.getId() == R.id.email_btn){
    			Toast emailToast = Toast.makeText(getApplicationContext(), "This will be available in later version!", Toast.LENGTH_SHORT);
    			emailToast.show();
    			email.setVisibility(email.INVISIBLE);
    		}
    	}
    	
    	@SuppressLint("NewApi")
    	public void galleryImport(View view) {
    		if(view.getId()==R.id.galleryImage){
    			Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    			intent.setType("image/*");
    			//intent.setAction(Intent.ACTION_GET_CONTENT);
    			startActivityForResult(intent, SELECT_IMAGE); 
    				
    		//	startActivity(intent);
    		//	ImageView pictures = (ImageView) findViewById(R.id.drawing);
    			//pictures.buildLayer();
    //			Toast.makeText(getApplicationContext(), "This will be fully functional in version 2", 
    //					Toast.LENGTH_SHORT).show();
    		}
    	}
    	@Override
    	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    		super.onActivityResult(requestCode, resultCode, data);
    		
    		switch(resultCode){
    		case SELECT_IMAGE:
    		
    		if (requestCode == RESULT_OK){
    			Uri selectedImage = data.getData();
    			InputStream imageStream = null;
    			try {
    				imageStream = getContentResolver().openInputStream(selectedImage);
    			} catch (FileNotFoundException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    			Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);
    		}
                Uri selectedImage = data.getData();
                String[] filePathColumn = { MediaStore.Images.Media.DATA };
    
                ImageView imageView = (ImageView) findViewById(R.id.drawing);
                //imageView.setImageBitmap(BitmapFactory.decodeFileDescriptor(filePathColumn));
                imageView.setImageURI(selectedImage);
            }
    	}
    	
    
    }
    
    


  • Closed Accounts Posts: 19,777 ✭✭✭✭The Corinthian


    shanard wrote: »
    Any ideas?
    Sure, like what error are you getting? Are you checking what file path is being returned? Have you run it through the debugger and found at what line it's failing?

    TBH, these are pretty basic questions for any kind of bug hunting.

    Also there's no need to play around with Input or Output streams. You can load the file directly using BitmapFactory.decodeFile.


  • Advertisement
  • Registered Users Posts: 2,024 ✭✭✭Colonel Panic


    Sure, like what error are you getting? Are you checking what file path is being returned? Have you run it through the debugger and found at what line it's failing?

    TBH, these are pretty basic questions for any kind of bug hunting.

    Also there's no need to play around with Input or Output streams. You can load the file directly using BitmapFactory.decodeFile.

    I remember the OP using toasts as a form of printf debugging in the past. Your suggestion of using a debugger is one I need to +1.

    It's an absolutely essential skill to develop.


  • Registered Users Posts: 72 ✭✭shanard


    Sorry folks but i absolutely stuck........ I have tried so many things and i not offay with the debugger (Its next on my list after this app to learn)..

    I am getting as far as clicking button to go into the gallery and as soon as i select an image the app crashes.

    I attach a copy of the logcat if anyone can help me as well as my java code below.

    Thanks in advance!

    K.
    package com.kennshanny.childsplay;
    
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.UUID;
    
    import android.net.Uri;
    import android.os.Bundle;
    import android.provider.MediaStore;
    import android.annotation.SuppressLint;
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.app.Dialog;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.database.Cursor;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.util.Log;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.ImageButton;
    import android.widget.ImageView;
    import android.widget.LinearLayout;
    import android.widget.Toast;
    
     
    public class DrawingCanvas extends Activity implements OnClickListener {
    
    	private static final int SELECT_IMAGE = 1;
    	//custom drawing view
    	private DrawingView drawView;
    	//buttons
    	private ImageButton currPaint, drawBtn, eraseBtn, newBtn, saveBtn;
    	//sizes
    	private float smallBrush, mediumBrush, largeBrush;
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_drawing_canvas);
    
    		//get drawing view
    		drawView = (DrawingView)findViewById(R.id.drawing);
    
    		//get the palette and first color button
    		LinearLayout paintLayout = (LinearLayout)findViewById(R.id.paint_colors);
    		currPaint = (ImageButton)paintLayout.getChildAt(0);
    		currPaint.setImageDrawable(getResources().getDrawable(R.drawable.paint_pressed));
    
    		//sizes from dimensions
    		smallBrush = getResources().getInteger(R.integer.small_size);
    		mediumBrush = getResources().getInteger(R.integer.medium_size);
    		largeBrush = getResources().getInteger(R.integer.large_size);
    
    		//draw button
    		drawBtn = (ImageButton)findViewById(R.id.draw_btn);
    		drawBtn.setOnClickListener(this);
    
    		//set initial size
    		drawView.setBrushSize(mediumBrush);
    
    		//erase button
    		eraseBtn = (ImageButton)findViewById(R.id.erase_btn);
    		eraseBtn.setOnClickListener(this);
    
    		//new button
    		newBtn = (ImageButton)findViewById(R.id.new_btn);
    		newBtn.setOnClickListener(this);
    
    		//save button
    		saveBtn = (ImageButton)findViewById(R.id.save_btn);
    		saveBtn.setOnClickListener(this);
    	}
    
    	@Override
    	public boolean onCreateOptionsMenu(Menu menu) {
    		// Inflate the menu; this adds items to the action bar if it is present.
    		getMenuInflater().inflate(R.menu.drawing_canvas, menu);
    		return true;
    	}
    
    	//user clicked paint
    	public void paintClicked(View view){
    		//use chosen color
    
    		//set erase false
    		drawView.setErase(false);
    		drawView.setBrushSize(drawView.getLastBrushSize());
    
    		if(view!=currPaint){
    			ImageButton imgView = (ImageButton)view;
    			String color = view.getTag().toString();
    			drawView.setColor(color);
    			//update ui
    			imgView.setImageDrawable(getResources().getDrawable(R.drawable.paint_pressed));
    			currPaint.setImageDrawable(getResources().getDrawable(R.drawable.paint));
    			currPaint=(ImageButton)view;
    		}
    	}
    
    	@Override
    	public void onClick(View view){
    
    		if(view.getId()==R.id.draw_btn){
    			//draw button clicked
    			final Dialog brushDialog = new Dialog(this);
    			brushDialog.setTitle("Brush size:");
    			brushDialog.setContentView(R.layout.brush_chooser);
    			//listen for clicks on size buttons
    			ImageButton smallBtn = (ImageButton)brushDialog.findViewById(R.id.small_brush);
    			smallBtn.setOnClickListener(new OnClickListener(){
    				@Override
    				public void onClick(View v) {
    					drawView.setErase(false);
    					drawView.setBrushSize(smallBrush);
    					drawView.setLastBrushSize(smallBrush);
    					brushDialog.dismiss();
    				}
    			});
    			ImageButton mediumBtn = (ImageButton)brushDialog.findViewById(R.id.medium_brush);
    			mediumBtn.setOnClickListener(new OnClickListener(){
    				@Override
    				public void onClick(View v) {
    					drawView.setErase(false);
    					drawView.setBrushSize(mediumBrush);
    					drawView.setLastBrushSize(mediumBrush);
    					brushDialog.dismiss();
    				}
    			});
    			ImageButton largeBtn = (ImageButton)brushDialog.findViewById(R.id.large_brush);
    			largeBtn.setOnClickListener(new OnClickListener(){
    				@Override
    				public void onClick(View v) {
    					drawView.setErase(false);
    					drawView.setBrushSize(largeBrush);
    					drawView.setLastBrushSize(largeBrush);
    					brushDialog.dismiss();
    				}
    			});
    			//show and wait for user interaction
    			brushDialog.show();
    		}
    		else if(view.getId()==R.id.erase_btn){
    			//switch to erase - choose size
    			final Dialog brushDialog = new Dialog(this);
    			brushDialog.setTitle("Eraser size:");
    			brushDialog.setContentView(R.layout.brush_chooser);
    			//size buttons
    			ImageButton smallBtn = (ImageButton)brushDialog.findViewById(R.id.small_brush);
    			smallBtn.setOnClickListener(new OnClickListener(){
    				@Override
    				public void onClick(View v) {
    					drawView.setErase(true);
    					drawView.setBrushSize(smallBrush);
    					brushDialog.dismiss();
    				}
    			});
    			ImageButton mediumBtn = (ImageButton)brushDialog.findViewById(R.id.medium_brush);
    			mediumBtn.setOnClickListener(new OnClickListener(){
    				@Override
    				public void onClick(View v) {
    					drawView.setErase(true);
    					drawView.setBrushSize(mediumBrush);
    					brushDialog.dismiss();
    				}
    			});
    			ImageButton largeBtn = (ImageButton)brushDialog.findViewById(R.id.large_brush);
    			largeBtn.setOnClickListener(new OnClickListener(){
    				@Override
    				public void onClick(View v) {
    					drawView.setErase(true);
    					drawView.setBrushSize(largeBrush);
    					brushDialog.dismiss();
    				}
    			});
    			brushDialog.show();
    		}
    		else if(view.getId()==R.id.new_btn){
    			//new button
    			AlertDialog.Builder newDialog = new AlertDialog.Builder(this);
    			newDialog.setTitle("New drawing");
    			newDialog.setMessage("Start new drawing (you will lose the current drawing)?");
    			newDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener(){
    				public void onClick(DialogInterface dialog, int which){
    					drawView.startNew();
    					dialog.dismiss();
    				}
    			});
    			newDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
    				public void onClick(DialogInterface dialog, int which){
    					dialog.cancel();
    				}
    			});
    			newDialog.show();
    		}
    		else if(view.getId()==R.id.save_btn){
    			//save drawing
    			AlertDialog.Builder saveDialog = new AlertDialog.Builder(this);
    			saveDialog.setTitle("Save drawing");
    			saveDialog.setMessage("Save drawing to device Gallery?");
    			saveDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener(){
    				public void onClick(DialogInterface dialog, int which){
    					//save drawing
    					drawView.setDrawingCacheEnabled(true);
    					//attempt to save
    					String imgSaved = MediaStore.Images.Media.insertImage(
    							getContentResolver(), drawView.getDrawingCache(),
    							UUID.randomUUID().toString()+".png", "drawing");
    					//feedback
    					if(imgSaved!=null){
    						Toast savedToast = Toast.makeText(getApplicationContext(), 
    								"Drawing saved to Gallery!", Toast.LENGTH_SHORT);
    						savedToast.show();
    					}
    					else{
    						Toast unsavedToast = Toast.makeText(getApplicationContext(), 
    								"Oops! Image could not be saved.", Toast.LENGTH_SHORT);
    						unsavedToast.show();
    					}
    					drawView.destroyDrawingCache();
    				}
    			});
    			saveDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
    				public void onClick(DialogInterface dialog, int which){
    					dialog.cancel();
    				}
    			});
    			saveDialog.show();
    		}
    	}
    	
    	public void emailPicture(View view) {
    		ImageButton email = (ImageButton) findViewById(R.id.email_btn);
    		if (view.getId() == R.id.email_btn){
    //			Toast emailToast = Toast.makeText(getApplicationContext(), "This will be available in later version!", Toast.LENGTH_SHORT);
    //			emailToast.show();
    //			email.setVisibility(email.INVISIBLE);
    			Intent intent = new Intent(Intent.ACTION_SEND);
    			//intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    			intent.setType("image/jpg");
    			intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///mnt/sdcard/image.jpg/"));
    			startActivity(intent);
    			
    		}
    	}
    	
    	//@SuppressLint("NewApi")
    	public void galleryImport(View view) {
    		if(view.getId()==R.id.galleryImage){
    			Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    			intent.setType("image/*");
    			startActivityForResult(intent, SELECT_IMAGE); 
    		//	startActivity(intent);
    		//	ImageView pictures = (ImageView) findViewById(R.id.drawing);
    			//pictures.buildLayer();
    //			Toast.makeText(getApplicationContext(), "This will be fully functional in version 2", 
    //					Toast.LENGTH_SHORT).show();
    		}
    	}
    	@Override
    	protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedData) {
    		super.onActivityResult(requestCode, resultCode, imageReturnedData);
    		
    		switch(requestCode){
    		case SELECT_IMAGE:
    		
    		if (resultCode == RESULT_OK){
    			Uri selectedImage = imageReturnedData.getData();
                String[] filePathColumn = { MediaStore.Images.Media.DATA };
    
    //            Cursor cursor = getContentResolver().query(selectedImage, 
    //            		filePathColumn, null, null, null);
    //            		cursor.moveToFirst();
    //
    //            		int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
    //            		String filePath = cursor.getString(columnIndex);
    //            		cursor.close();
    //
    //           // The selected Image should be stored in the following
    //            Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
               try {
    			Bitmap b = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage);
    		} catch (FileNotFoundException e) {
    			Log.d("Bitmap b Tag", e.toString());
    
    				e.printStackTrace();
    		} catch (IOException e) {
    
    				e.printStackTrace();
    		}
               
                ImageView imageView = (ImageView) findViewById(R.id.drawing);
                imageView.setImageURI(selectedImage);
            }
    	}
    	
    
    	}}
    
    


  • Closed Accounts Posts: 19,777 ✭✭✭✭The Corinthian


    shanard wrote: »
    Sorry folks but i absolutely stuck........ I have tried so many things and i not offay with the debugger (Its next on my list after this app to learn)..
    I suggest you push it up your priority list as you need it now.


  • Registered Users Posts: 7,868 ✭✭✭The_B_Man


    Logcat says you have a ClassCastException.trying to cast an ImageView to your drawView.
    Its one of the first lines in your OnCreate().
    I suspect that all will become clear if you were to wrap a try/catch around that line.

    I don't want to hop on any bandwagon here, but there's a general idea on here not to spoon feed people in order to have them learn themselves, so I'd echo the lads here and say you need to learn to debug properly. The issue seems to be clearly there in the highlighted red part of your logcat (or at least its where I'd start), you just have to read it. That can be seen without even running the debugger.


  • Registered Users Posts: 72 ✭✭shanard


    Sorry folks for this, but i can only complete this part time as busy doing other things, but can anyone please have a look at the following snippet of code.

    What i am trying to achieve is when user presses Gallery Button it brings them to their gallery to select an image which will then display on the DrawingCanvas i have...

    It lets me do everything but just will not display on the canvas if someone can guide me in the right direction, be much appreciated....
    public void galleryImport(View view) {
    		Intent intent = new Intent(Intent.ACTION_PICK, 
    				android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    		intent.setType("image/*");
    //		intent.setAction(Intent.ACTION_GET_CONTENT);
    //		intent.addCategory(Intent.CATEGORY_OPENABLE);
    		startActivityForResult(intent, SELECT_IMAGE);
    		Log.d("GALLERY IMPORT INTENT", intent.getAction());
    	}
    	
    	
    	
    	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    		super.onActivityResult(requestCode, resultCode, data);
    		Log.d("onActivityResult Before if", data.getAction());
    		
    		
    	switch(requestCode){
    		case(SELECT_IMAGE):
    			if(resultCode == Activity.RESULT_OK){
    				Uri selectedImage = data.getData();
    				String[] filePathColumn = {MediaStore.Images.Media.DATA};
    				
    				Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
    				cursor.moveToFirst();
    
    				
    				int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
    				String filePath = cursor.getString(columnIndex);
    				cursor.close();
    				
    				ImageView image = (ImageView) findViewById(R.id.drawing);
    				Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
    				image.setImageBitmap(yourSelectedImage);
    				Log.d("YOUR SELECTED IMAGE SAVED HERE!", yourSelectedImage.toString());
    			}
    		}
    

    The logs are my way of trying to debug it, but the logcat doesnt seem to get as far as onActivityResult().....

    please help!!


  • Advertisement
  • Registered Users Posts: 2,345 ✭✭✭Kavrocks


    It's good to see that you've at least tried to debug this yourself.
    shanard wrote: »
    the logcat doesnt seem to get as far as onActivityResult().....
    Do you actually call 'startActivityForResult(intent, SELECT_IMAGE);' and does it launch correctly? What happens after you select an image via this?

    If yes then more than likely your Intent is wrong. It appears to me as if you've mashed stuff together from one of the stackoverflow posts The Corinthian linked without knowing what the code actually does.

    My advice would be to read up on intents at Android Developers and understand what you are trying to do with your intent. There are loads of examples there for calling common intents for stuff similar to what you are trying to do. You'll just need to make the leap from their examples to your specific use case (and the stackoverflow question should have all you need). I'd also be surprised if there wasn't something similar to this in the SDK samples.


Advertisement