Android Image upload

Source: Internet
Author: User

Package com.example.center;

Import Java.io.ByteArrayOutputStream;
Import Java.io.InputStream;

Import COM.EXAMPLE.ANUOC.R;

Import android.app.Activity;
Import Android.app.AlertDialog;
Import Android.content.ContentResolver;
Import Android.content.DialogInterface;
Import android.content.Intent;
Import Android.graphics.Bitmap;
Import Android.graphics.BitmapFactory;
Import Android.net.Uri;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.ImageView;

public class Testcarema extends Activity
{
/** called when the activity is first created. */
Private ImageView ImageView;
Private Onclicklistener Imgviewlistener;
Private Bitmap Mybitmap;
Private byte[] mcontent;

@ Override
public void OnCreate (Bundle savedinstancestate)
{
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.testcamera_main);
ImageView = (ImageView) Findviewbyid (R.id.imageview);
Imgviewlistener = new Onclicklistener ()
{
public void OnClick (View v)
{
Final charsequence[] items =
{"Photo album", "Photo"};
Alertdialog dlg = new Alertdialog.builder (testcarema.this). Settitle ("select Picture"). Setitems (Items,
New Dialoginterface.onclicklistener ()
{
public void OnClick (dialoginterface dialog, int item)
{
The item here is based on the chosen way,
In the items array, two methods are defined, and the picture is labeled as 1, so the camera method is called.
if (item = = 1)
{
Intent Getimagebycamera = new Intent ("Android.media.action.IMAGE_CAPTURE");
Startactivityforresult (Getimagebycamera, 1);
} else
{
Intent getImage = new Intent (intent.action_get_content);
Getimage.addcategory (intent.category_openable);
Getimage.settype ("Image/jpeg");
Startactivityforresult (getImage, 0);
}
}
}). Create ();
Dlg.show ();
}
};
To bind a point to a ImageView control click Listener
Imageview.setonclicklistener (Imgviewlistener);

}

@ Override
protected void Onactivityresult (int requestcode, int resultcode, Intent data)
{
TODO auto-generated Method Stub
Super.onactivityresult (Requestcode, ResultCode, data);

Contentresolver resolver = Getcontentresolver ();
/**
* Because the Startactivityforresult method is used in both of these ways,
* This method executes the Onactivityresult method after execution, so in order to distinguish between the choice of the way to get the picture to be judged,
* The Requestcode here corresponds to the second parameter inside the Startactivityforresult
*/
if (Requestcode = = 0)
{
Try
{
Get the URI of the picture
Uri Originaluri = Data.getdata ();
Parsing the contents of a picture into a byte array
Mcontent = Readstream (Resolver.openinputstream (Uri.parse (originaluri.tostring ()));
Converts a byte array to a ImageView callable bitmap object
Mybitmap = getpicfrombytes (mcontent, NULL);
Bind the resulting picture to display on the control
Imageview.setimagebitmap (MYBITMAP);
} catch (Exception e)
{
System.out.println (E.getmessage ());
}

} else if (Requestcode = = 1)
{
Try
{
Super.onactivityresult (Requestcode, ResultCode, data);
Bundle extras = Data.getextras ();
Mybitmap = (Bitmap) extras.get ("Data");
Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
Mybitmap.compress (Bitmap.CompressFormat.JPEG, BAOs);
Mcontent = Baos.tobytearray ();
} catch (Exception e)
{
TODO auto-generated Catch block
E.printstacktrace ();
}
Bind the resulting picture to display on the control
Imageview.setimagebitmap (MYBITMAP);
}
}

public static Bitmap getpicfrombytes (byte[] bytes, bitmapfactory.options opts)
{
if (bytes! = null)
if (opts! = null)
Return Bitmapfactory.decodebytearray (bytes, 0, Bytes.length, opts);
Else
Return Bitmapfactory.decodebytearray (bytes, 0, bytes.length);
return null;
}

public static byte[] Readstream (InputStream instream) throws Exception
{
byte[] buffer = new byte[1024];
int len =-1;
Bytearrayoutputstream OutStream = new Bytearrayoutputstream ();
while (len = instream.read (buffer))! =-1)
{
Outstream.write (buffer, 0, Len);
}
byte[] data = Outstream.tobytearray ();
Outstream.close ();
Instream.close ();
return data;

}

}

Layout file ....

<?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"
>
<imageview
Android:background= "#ff0000"
Android:id= "@+id/imageview"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
/>
</LinearLayout>

Android Image upload

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.