473,383 Members | 1,896 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

convert image into byte

1
hi all of u ,

in my project i want to transfer an image from one moblie to another mobile using bluetooth. i have written a code but giving null pointer exception at
int length = in.read(buff);

my code is :

Expand|Select|Wrap|Line Numbers
  1. public void imageTransfer()
  2.     {
  3.         System.err.println(" From image transfer ()");
  4.         String imgName = "/Sunset.png";
  5.         imgData = getImageData(imgName);
  6.         sendImageData(imgData);
  7.  
  8.     }
  9.  
  10.      public byte[]  getImageData(String imgName)
  11.     {
  12.  
  13.  
  14.             in = getClass().getResourceAsStream(imgName);
  15.  
  16.             buff = new byte[1024];
  17.             baos = new ByteArrayOutputStream(1024);
  18.  
  19.          try
  20.         {   System.err.println(" From getImageData() try block :    ");
  21.             int length = in.read(buff);
  22.         }
  23.         catch(Exception e)
  24.         {
  25.              e.printStackTrace();
  26.         }
  27.  
  28.  
  29.  
  30.        try
  31.        {
  32.  
  33.          while(true)
  34.          {
  35.  
  36.              if(length == -1)
  37.              {
  38.                  break;
  39.              }
  40.  
  41.              baos.write(buff,0,length);
  42.          }
  43.        }
  44.         catch(Exception e)
  45.         {
  46.              e.printStackTrace();
  47.         }
  48.          return baos.toByteArray();
  49.  
  50.       }
  51.  
  52.      public void sendImageData(byte[] imgData)
  53.       {
  54.  
  55.          System.err.println(" From sendImageData()");
  56.         if(imgData == null)
  57.         {
  58.             return ;
  59.         }
  60.  
  61.         OutputStream out = null;
  62.  
  63.         try
  64.         {
  65.             System.err.println(" From getImageData() try ");
  66.             out = i.openOutputStream();
  67.             out.write(imgData);
  68.             out.flush();
  69.  
  70.         }
  71.         catch(IOException e)
  72.          {
  73.              System.out.println(""+e.getMessage());
  74.          }
  75.  
  76.          if (out != null)
  77.          {
  78.             try
  79.             {
  80.                 out.close();
  81.             }
  82.             catch (IOException e)
  83.             {
  84.             }
  85.          }
  86.       }
  87.  
Dec 3 '07 #1
2 2047
epots9
1,351 Expert 1GB
Moved to the JAVA Forums where the resident experts can better assist you, and please use code tags.

**Moved from Programming Challenges
Dec 3 '07 #2
JosAH
11,448 Expert 8TB
hi all of u ,

in my project i want to transfer an image from one moblie to another mobile using bluetooth. i have written a code but giving null pointer exception at
int length = in.read(buff);
No need to look at the code any further: if you receive that exception on that
line of your code than 'in' equals null; no doubt about it.

kind regards,

Jos
Dec 3 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Fritz Switzer | last post by:
With two controls a PictureBox and another "PictureBox like" , Pic2, a control from a dll, I'm stuck on cannot implicity convert System.Drawing.Image to System.Drawing.Bitmap error. How do I...
3
by: dale zhang | last post by:
Hi, I am trying to read an image from MS Access DB based on the following article: http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp The article author is using PictureBox for windows...
4
by: dale zhang | last post by:
Hi, I am trying to save and read an image from MS Access DB based on the following article: http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp Right now, I saved images without any...
7
by: Rob T | last post by:
Hi, I'm trying to convert his line from C# to VB: System.Drawing.Image _img= System.Drawing.Image.FromStream(new System.IO.MemoryStream((byte)SqlReader); I have this so far: dim Img as...
1
by: Daniel | last post by:
I have looked everywhere on the web for an answer to this and the only thing I can find is converting the image format when the file is present on the local filesystem. What I want to do is use a...
3
by: Dennis | last post by:
I am trying to convert a bitmap to a JPEG MemoryStream and return a Byte array containing the resulting JPEG Image as follows: Public Function BmpToJPEG(ByVal BitMapIn As Bitmap, ByVal Quality As...
5
by: Trammel | last post by:
Hi, I have a vb.net program made to grab screenshots and then store them in image objects. The image is displayed in a pictureBox atm but I want to store the data in a String only. (Without...
8
by: piggy | last post by:
I am trying to convert an image to an byte array (tobytes() method) and from byte array back to an image (frombytes() method). The problem i have here is say the image size is 285 bytes but the...
0
by: velmani | last post by:
hi , i have problem with Retrieving image from DB i have a table with image datatype i store a file by using code as follows -------------------------------------------------- string strType; ...
0
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.