Upload image to server from gallary or camera android - android

i have a Activity for Upload image from gallary or camera to server.image upload from camera is fine but upload from gallary is not done.i have a error showing
BitmapFactory﹕ Unable to decode stream: FileNotFoundException
i want to do when i pick up the image from gallary it is shown in other Activity on image view.
i don't know how to get fileuri for gallary please help me.
my code:
loadimage = (ImageView) findViewById(R.id.profilpic);
loadimage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
/* Intent i = new Intent(Tab1.this, ImageMain.class);
startActivity(i);*/
// selectImageFromGallery();
AlertDialog.Builder builder = new AlertDialog.Builder(Tab1.this);
builder.setMessage("Select Image From")
.setCancelable(true)
.setPositiveButton("camera", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
// start the image capture Intent
startActivityForResult(intent, CAMERA_REQUEST);
}
})
.setNegativeButton("gallary", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RESULT_LOAD_IMAGE);
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
Hear is my onActiivityResult
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// if the result is capturing Image
if (requestCode == CAMERA_REQUEST) {
if (resultCode == RESULT_OK) {
launchUploadActivity(true);
} else if (resultCode == RESULT_CANCELED) {
// user cancelled Image capture
Toast.makeText(getApplicationContext(),
"User cancelled image capture", Toast.LENGTH_SHORT)
.show();
} else {
// failed to capture image
Toast.makeText(getApplicationContext(),
"Sorry! Failed to capture image", Toast.LENGTH_SHORT)
.show();
}
}
else if(requestCode==RESULT_LOAD_IMAGE){
if (resultCode==RESULT_OK){
launchUploadActivity(true);
}
}
}
private void launchUploadActivity(boolean isImage){
Intent i = new Intent(Tab1.this,UploadAvtivity.class);
i.putExtra("filePath", fileUri.getPath());
i.putExtra("isImage", isImage);
startActivity(i);
}

Here is the Code piece for Taking a Picture through Default Camera (here I implemented Intent to to fetch the image). After that store it to SD card(here a new file will be created and the newly taken image will be stored ); and if you don't want to store then remove the saving part from code. After that you can use the file path for your upload purpose. You can then refer it and change to get the path as your wish.
In the class area put these lines
final int TAKE_PHOTO_REQ = 100;
String file_path = Environment.getExternalStorageDirectory()
+ "/recent.jpg";//Here recent.jpg is your image name which will going to take
After that invoke the camera by putting these line in calling method.
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, TAKE_PHOTO_REQ);
then add this method in your Activity to get the picture and save it to sd card and you can invoke your upload method from here to upload the image by knowing its path.
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case TAKE_PHOTO_REQ: {
if (resultCode == TakePicture.RESULT_OK && data != null) {
Bitmap srcBmp = (Bitmap) data.getExtras().get("data");
// ... (process image if necesary)
imageView.setImageBitmap(srcBmp);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
srcBmp.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
// you can create a new file name "test.jpg" in sdcard folder.
File f = new File(file_path);
try {
f.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// write the bytes in file
FileOutputStream fo = null;
try {
fo = new FileOutputStream(f);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
fo.write(bytes.toByteArray());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// remember close de FileOutput
try {
fo.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.e("take-img", "Image Saved to sd card...");
// Toast.makeText(getApplicationContext(),
// "Image Saved to sd card...", Toast.LENGTH_SHORT).show();
break;
}
}
}
}
Hope this will be helpful for you and others too .. thanks

Related

Attempting to implement a feature where a user can use their phone camera to take a picture and then it will be uploaded as their profile pic

I'm currently running into an issue with implementing a feature on my app where a user can take a picture using their phone's camera and then have it display as a bitmap image as well as be uploaded to Firebase.
I've seen several similar questions answered on here but nothing seems to be working for me, so I was hoping if someone could give my code a look and have insight for what I might be doing incorrectly.
My general process as of right now is that my "Choose from Gallery" option is working, but my "Take Photo" is not. When clicked in selectProfilePicOption(), I am able to take a picture and confirm it, but the data sent to onActivityResult() continues to get null. I want to be able to pass the taken photo and upload it as a Bitmap to my uploadImageToFirebase() function.
Thanks to all in advance for any assistance!
Here's my current code:
private void selectProfilePicOption(Context context){
final CharSequence[] options = {"Take Photo", "Choose from Gallery", "Cancel"};
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Choose your profile picture");
builder.setItems(options, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
if(options[which].equals("Take Photo")){
Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePicture, TAKE_IMAGE_REQUEST);
}else if(options[which].equals("Choose from Gallery")){
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select image..."), PICK_IMAGE_REQUEST);
}else if(options[which].equals("Cancel")){
dialog.dismiss();
}
}
});
builder.show();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != RESULT_CANCELED) {
switch (requestCode) {
case TAKE_IMAGE_REQUEST:
if(resultCode == RESULT_OK){
data.getExtras().get("data");
File file = new File(Environment.getExternalStorageDirectory().getPath());
Uri uri = Uri.fromFile(file);
try{
System.out.println("attempting to store bitmap");
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
uploadImageToFirebase(bitmap);
} catch (FileNotFoundException e){
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
}
break;
case PICK_IMAGE_REQUEST:
if (resultCode == RESULT_OK && data != null && data.getData() != null) {
filePath = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
uploadImageToFirebase(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
break;
}
}
}
private void uploadImageToFirebase(final Bitmap bitmap){
if(filePath != null){
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setTitle("Uploading...");
progressDialog.show();
StorageReference ref = mStorageRef.child("images/ProfilePics/" + mUser.getUid());
ref.putFile(filePath).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
progressDialog.dismiss();
profilePicture.setImageBitmap(bitmap);
Toast.makeText(SetProfileData.this, "Image Uploaded", Toast.LENGTH_SHORT).show();
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
progressDialog.dismiss();
Toast.makeText(SetProfileData.this, "Failed " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
})
.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
#Override
public void onProgress(#NonNull UploadTask.TaskSnapshot taskSnapshot) {
double progress = (100.0 * taskSnapshot.getBytesTransferred() / taskSnapshot.getTotalByteCount());
progressDialog.setMessage("Uploaded " + (int)progress + "%");
}
});
}
}
The line data.getExtras().get("data"); contains the thumbnail of your captured image, but for some reasons, you are completely ignoring it.
Do it this way,
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != RESULT_CANCELED) {
switch (requestCode) {
case TAKE_IMAGE_REQUEST:
if(resultCode == RESULT_OK){
Bitmap bitmap = (Bitmap) intent.getExtras().get("data") //this line is important
uploadImageToFirebase(bitmap);
} catch (FileNotFoundException e){
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
}
break;
case PICK_IMAGE_REQUEST:
if (resultCode == RESULT_OK && data != null && data.getData() != null) {
filePath = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
uploadImageToFirebase(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
break;
}
}
But beware the image thumbnail you received on onActivityResult() and uploaded to FireBase is not the full resolution image but just a compressed version of it. In order to get the full-size image, you can check out this

Fetching Captured Image sent through Intent

I'm working on a simple project, where I'm suppose to create an application for taking notes. I've sucessfully created the app but now I want to make it possible for the user to take a photo and save it with the note.
I can take the picture, preview it, and then click save.
But the problem is that I'm not able to get the image sent through the intent to my MainActivity class, where the note is suppose to be saved.
So my question is how do I save the note with the picture, please look at the picture that represents how I want my customRow to look like
I have no idea why this happends beacuse I get no errors... And its been driving me nuts.
Any ideas??
Image.class
public void capturePhoto(View view){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri();
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
// start the image capture Intent
startActivityForResult(intent, CAPTURE_IMAGE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
switch (requestCode){
case CAPTURE_IMAGE:
previewCapturedImage();
break;
}
}
else if (resultCode == RESULT_CANCELED) {
// user cancelled Image capture
Toast.makeText(getApplicationContext(),
"User cancelled image capture", Toast.LENGTH_SHORT)
.show();
} else {
// failed to capture image
Toast.makeText(getApplicationContext(),
"Sorry! Failed to capture image", Toast.LENGTH_SHORT)
.show();
}
}
private void previewCapturedImage() {
try {
// bitmap factory
BitmapFactory.Options options = new BitmapFactory.Options();
// downsizing image as it throws OutOfMemory Exception for larger
// images
options.inSampleSize = 2;
bitmap = BitmapFactory.decodeFile(fileUri.getPath(),
options);
//Set image here
imageView.setImageBitmap(bitmap);
} catch (NullPointerException e) {
e.printStackTrace();
}
}
public void sendImageAsIntent(Boolean isSending){
if(isSending = false){
isSending = true;
}
if(isSending = true) {
Intent i = new Intent(CheckOutMemo.this, MainActivity.class);
i.putExtra("filePath", fileUri.getPath());
setResult(RESULT_OK,i);
finish();
}
}
MainActivity.class
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != Activity.RESULT_OK) {
return;
}
if (requestCode == Image.ADD_REQUEST_CODE) {
String header = data.getStringExtra("header");
String bodyText = data.getStringExtra("bodyText");
filePath = data.getStringExtra("filePath");
sourceFile = new File(filePath);
fileName = data.getStringExtra("filePath");
Uri imageUri = data.getData();
if(filePath!=null) {
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),imageUri);
imageIcon.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
Toast.makeText(MainActivity.this, "FileNoutFoundException MainActivity", Toast.LENGTH_SHORT).show();
e.printStackTrace();
} catch (IOException e) {
Toast.makeText(MainActivity.this, "IOException MainActivity", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
Memo memo = new Memo(header, bodyText,imageIcon);
customAdapter.add(memo);
customAdapter.notifyDataSetChanged();
} else if (requestCode == Image.EDIT_REQUEST_CODE) {
int position = data.getIntExtra("position", 0);
Memo memo = customAdapter.getItem(position);
memo.header = data.getStringExtra("header");
memo.bodyText = data.getStringExtra("bodyText");
customAdapter.notifyDataSetChanged();
}
}
When you do:
Intent i = new Intent(CheckOutMemo.this, MainActivity.class);
i.putExtra("filePath", fileUri.getPath());
startActivity(i);
You are basically asking to create a new instance of MainActivity. To return to the previous activity while setting the result do this instead:
setResult(RESULT_OK, i);
finish();//finishing activity

Android: Resolution issue while saving image into gallery

I've managed to programmatically take a photo from the camera, then display it on an imageview, and then after pressing a button, saving it on the Gallery.
It works, but the problem is that the saved photo are low resolution.. WHY?!
I took the photo with this code:
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
Then I save the photo on a var using :
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_PIC_REQUEST) {
thumbnail = (Bitmap) data.getExtras().get("data");
}
}
and then after displaying it on an imageview, I save it on the gallery with this function:
public void SavePicToGallery(Bitmap picToSave, File savePath){
String JPEG_FILE_PREFIX= "PIC";
String JPEG_FILE_SUFFIX= ".JPG";
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = JPEG_FILE_PREFIX + timeStamp + "_";
File filePath = null;
try {
filePath = File.createTempFile(imageFileName, JPEG_FILE_SUFFIX, savePath);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
FileOutputStream out = null;
try {
out = new FileOutputStream(filePath);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
picToSave.compress(CompressFormat.JPEG, 100, out);
try {
out.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Add the pic to Android Gallery
String mCurrentPhotoPath = filePath.getAbsolutePath();
MediaScannerConnection.scanFile(this,
new String[] { mCurrentPhotoPath }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
}
});
}
I really can't figure out why it lose so much quality while saved..
Any help please ? Thanks..
The photo you are displaying in the ImageView is a thumbnail :
data.getExtras().get("data");
Are you calling the method :
public void SavePicToGallery(Bitmap picToSave, File savePath)
with the thumbnail ?
Here you have all the steps describe to do what you want :
http://developer.android.com/training/camera/photobasics.html
data.getExtras().get("data") only get thumbnail.
To do it properly, you have to declare global uri variable
Uri imageCapturedUri ;
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
imageCaptureUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageCaptureUri);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_PIC_REQUEST) {
// mImageCaptureUri is your Uri
}
}

Using phones camera , after capturing image activity doesn't return back to particular activity

I am capturing an image from my application using phone's camera and using android.provider.MediaStore.ACTION_IMAGE_CAPTURE.
After clicking capture button it shows save or discard. Clicking save button it returns back to camera not to application. and the image is saved in gallary not to the output file which i have declared.
Below is my code
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Intent in=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(in, 0);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode==RESULT_OK) {
Bitmap bm=(Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
//you can create a new file name "test.jpg" in sdcard folder.
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator + "player1.jpg");
try {
f.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//write the bytes in file
FileOutputStream fo = null;
try {
fo = new FileOutputStream(f);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fo.write(bytes.toByteArray());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
Intent pic=new Intent(pic1.this, GameMenu.class);
startActivity(pic);
finish();
}
}
else {
Toast.makeText(getApplicationContext(), "Canceled", Toast.LENGTH_LONG).show();
Intent pic=new Intent(pic1.this, Menu.class);
startActivity(pic);
finish();
}
Kindly suggest if any solution..
Thanks in advance...
EDIT 1: I have checked this code on samsung galaxy y duos (android 2.3.6) its not working properly. And checked same on galaxy pop(2.2.1) and HTC wildfire(2.3.5) its working perfect.
Use below intent to capture the image and getTempFile() is where you mention the storage path
Intent photoPickerIntent= new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT, getTempFile());
photoPickerIntent.putExtra("outputFormat",Bitmap.CompressFormat.JPEG.toString());
photoPickerIntent.putExtra("return-data", true);
startActivityForResult(photoPickerIntent,TAKE_PICTURE);
getTempFile:
private Uri getTempFile() {
File root = new File(Environment.getExternalStorageDirectory(), "Directory");
if (!root.exists()) {
root.mkdirs();
}
File file;
file = new File(root,filename+".jpeg" );
muri = Uri.fromFile(file);
photopath=muri.getPath();
Log.e("getpath",muri.getPath());
return muri;
}
Read here for more threads
If this is your whole code you have omitted setContentView(); Could be the reason it is not returning.
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// setContentView() here
Intent in=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(in, 0);
}
Is the onActivityResult() method called?
I got the same problem, I have restart my device and test my application it works fine now. It might be possible with different code implementation application not responding. Try to reboot your device and test and let me know its working or not
you should creat your file opening your camera. you give the file URI as extra to the camera intent:
/**
* Capturing Camera Image will lunch camera app request image capture
*/
private void captureImage() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri();
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
// start the image capture Intent
startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
}
this is how I creat the file :
/**
* Creating file uri to store image
*
*
* #return Uri
*/
public Uri getOutputMediaFileUri() {
return Uri.fromFile(getTempOutputMediaFile());
}
/**
* returning File
*
*
* #return File
*/
private File getTempOutputMediaFile() {
File appDirectory = this.getFilesDir();
File mediaStorageDir = new File(APP_NAME,
YOUR_IMAGE_DIRECTORY_NAME);
Log.i("mediaStorageDir", mediaStorageDir.getAbsolutePath());
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.i(YOUR_IMAGE_DIRECTORY_NAME,
"Oops! Failed create "
+ YOUR_IMAGE_DIRECTORY_NAME
+ " directory");
return null;
}
}
// Create a media file name
UUID temporaryImageUuid = UUID.randomUUID();
File mediaFile;
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ temporaryImageUuid + "." + YOUR_EXTENSION);
return mediaFile;
}
hi check the following code.after capturing image use startPreview() to release.it works for me
captureButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mCamera.autoFocus(null);
mCamera.takePicture(null, null, mPicture);
Log.e("in capture button","onclick of this button"+ mCamera);
mCamera.startPreview();
}
});

camera activity gives null pointer exception

in my camera intent:
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
this part gives me a null pointer exception.
can anyone explain why and what need to be changed??
button_1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
startActivityForResult(intent, TAKE_PICTURE);
}
});
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Image captured and saved to fileUri specified in the Intent
Toast.makeText(this, "Image saved to:\n" +
data.getData(), Toast.LENGTH_LONG).show();
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
} else {
// Image capture failed, advise user
}
}
}
Try the following,
public class Camera extends Activity
{
private static final int CAMERA_REQUEST = 1888;
private String selectedImagePath;
WebView webview;
String fileName = "capturedImage.jpg";
private static Uri mCapturedImageURI;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Intent cameraIntent = new Intent(ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK) {
if (requestCode == CAMERA_REQUEST)
{
Bitmap photo = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
Random randomGenerator = new Random();randomGenerator.nextInt();
String newimagename=randomGenerator.toString()+".jpg";
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator + newimagename);
try {
f.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//write the bytes in file
try {
fo = new FileOutputStream(f.getAbsoluteFile());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fo.write(bytes.toByteArray());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
uri=f.getAbsolutePath();
//this is the url that where you are saved the image
}
}

Categories

Resources