stuck on image capture screen ,can't get back to onActivityResult (Android) - android

I Have a activity that opens the Camera by starting ACTION_IMAGE_CAPTURE Intent:
Intent intent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
SimpleDateFormat dateformat = new SimpleDateFormat("ddMMyy");
File photo1 = new File(Environment
.getExternalStorageDirectory(), imageName);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo1));
startActivityForResult(intent, 5);
After starting this intent it opens the image capture screen and sometimes after clicking on the capture button it doesn't return to my app (onActivityResult) it enforce me to take an image another time again, and it doen't close this screen only if i hit the back button.
I put a break point in OnActivityResult when debugging and it doen't stop in this method.

Here is the sample application which gives option to select a image from either using camera or gallery and its also allows the option of cropping image. I hope this is what you exactly want.
Please see this https://github.com/lorensiuswlt/AndroidImageCrop
Its opensource download it and explore.

Here is my code for camera intent .It works perfectly ,you can try using it .
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;
public class PlayMenuActivity extends Activity implements OnClickListener {
Intent intent;
ImageButton cameraBtn, galleryBtn, maleBtn, femaleBtn;
static Bitmap photo;
Dialog dialog;
static String gender;
MediaPlayer mp;
protected static final int PHOTO_PICKED = 0;
private static final int CAMERA_REQUEST = 1337;
private static final int SELECT_PHOTO = 100;
private static final String TEMP_PHOTO_FILE = "tempPhoto.jpg";
protected boolean circleCrop = true;
private final static String TAG = "GetImageFromGalleryActivity";
// values for scaling image
protected int outputX = 320;
protected int outputY = 480;
protected int aspectX = 2;
protected boolean scale = true;
protected int aspectY = 3;
protected boolean return_data = false;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("hello", "oncreate PlayMenu");
setContentView(R.layout.play_menu_screen);//setting layout
//Image Buttons on activity screen
cameraBtn = (ImageButton) findViewById(R.id.cameraBtn_id);
galleryBtn = (ImageButton) findViewById(R.id.galleryBtn_id);
cameraBtn.setOnClickListener(this);
galleryBtn.setOnClickListener(this);
// sound played when button clicked
mp = MediaPlayer.create(this, R.raw.click);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.cameraBtn_id:
// camera intent for starting camera
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra("crop", "true");
cameraIntent.putExtra("aspectX", aspectX);
cameraIntent.putExtra("aspectY", aspectY);
cameraIntent.putExtra("outputX", outputX);
cameraIntent.putExtra("outputY", outputY);
cameraIntent.putExtra("scale", scale);
cameraIntent.putExtra("category", "camera");
startActivityForResult(cameraIntent, CAMERA_REQUEST);
break;
case R.id.galleryBtn_id:
// calling GalleryActivity for picking image from gallery
intent = new Intent(PlayMenuActivity.this, GalleryActivity.class);
startActivity(intent);
break;
default:
break;
}
}
/* creates bitmap of the captured photo */
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Log.i("hello", "REQUEST cALL");
//if a camera request is made and resultcode matches then bitmap is created
if (requestCode == CAMERA_REQUEST || resultCode == Activity.RESULT_OK) {
Log.i("hello", "REQUEST cALL");
try {
Log.i("hello", "Try Call");
Bitmap bMap = (Bitmap) data.getExtras().get("data");//creating bitmap
photo = bMap;
Intent intent = new Intent(PlayMenuActivity.this,
ShowActivity.class);
intent.putExtra("category", "camera");//adding category selected ie camera
startActivity(intent);
} catch (Exception e) {
Log.i("hello", "Exception" + e.getMessage());
}
} else {
// Log.i("hello", "Else call");
Toast.makeText(PlayMenuActivity.this, "Picture NOt taken",
Toast.LENGTH_LONG).show();
}
} // fn
}// class
Hope this helps .

this is very late, but the issue here could be missing read/write permissions. I had this very issue myself and it had to do with me wanting to put the image file into my own app folder, to which the camera app had no access to.
To be safe, when sharing files betweens apps, consider using Environment.getExternalStoragePublicDirectory(), instead of Environment.getExternalStorageDirectory().

Related

How to save picture to gallery in Android [duplicate]

This question already has an answer here:
Android camera Intent not saving in gallery [duplicate]
(1 answer)
Closed 5 years ago.
I am new to programming and especially Android. I am making a simple application where I am invoking the camera and taking a picture, the picture is then displayed in an ImageView but I also want it to be stored in the gallery when I click the button to view the stored pictures.
My Code:
package com.example.picture.app;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import java.io.File;
public class MainActivity extends AppCompatActivity {
public static final int CAMERA_REQUEST = 10;
private ImageView imgView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//get access to the image view
imgView = findViewById(R.id.imgView);
}
//this method will be called when the take photo button is clicked
public void btnTakePhotoClicked(View v) {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
public void onImageGalleryClicked(View v) {
//invoke the image gallery using n implicit intent
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
//where do we want to find the data
File pictureDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
String pictureDirectoryPath = pictureDirectory.getPath();
//get a uri representation
Uri data = Uri.parse(pictureDirectoryPath);
//set the data and the type. Get all image types
photoPickerIntent.setDataAndType(data, "image/*");
startActivityForResult(photoPickerIntent, 20);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//DID THE USER CHOOSE OK? If so code inside this code will execute
if (resultCode == RESULT_OK) {
if (requestCode == CAMERA_REQUEST) {
//WE ARE HEARING BACK FROM THE CAMERA
Bitmap cameraImage = (Bitmap) data.getExtras().get("data");
//at this point we have the image from the camera
imgView.setImageBitmap(cameraImage);
}
}
}
You can use the compress() method to save it in External storage.
You can try this
try
{
FileOutputStream out = new FileOutputStream(new File(Environment.getExternalStorageDirectory().toString() + "/bmp.png"));
cameraImage.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
}
catch (Exception e)
{
Toast.makeText(getBaseContext(),e.getMessage(), Toast.LENGTH_SHORT);
}
Also, you have to add uses permission <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

anddoid-camera-onActivityResult-Intent null

I am trying to develop an app which involves capturing an image, cropping it and saving it to Application database. In this process, I encountered an error, which seemed quite common but unclear:
'Intent' parameter in the 'onActivityResult' method is null.
Below is the activity code:
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ScanActivity extends Activity {
ImageView imgFavorite;
//keep track of camera capture intent
final int CAMERA_CAPTURE = 1;
//captured picture uri
private Uri picUri;
//keep track of cropping intent
final int PIC_CROP = 2;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scan);
imgFavorite = (ImageView)findViewById(R.id.imageView1);
try {
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_CAPTURE);
}
catch(Exception e){
String errormesage="Oops-your device doesnt support capturing images!";
Toast.makeText(ScanActivity.this,errormesage,Toast.LENGTH_SHORT).show();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
ContentResolver cr=getContentResolver();
String imageName = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date());
File photo=new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),imageName);
Uri selectedImage=Uri.fromFile(photo);
Bitmap bp ;
try{
//user is returning from capturing an image using the camera
if (resultCode == RESULT_OK && requestCode == CAMERA_CAPTURE) {
picUri=data.getData();
//carry out the crop operation
performCrop(picUri);
bp = MediaStore.Images.Media.getBitmap(cr, selectedImage);
imgFavorite.setImageBitmap(bp);
Toast.makeText(ScanActivity.this,"Captured Successfully",Toast.LENGTH_SHORT).show();
}
else if (requestCode==PIC_CROP)
{
if(data!=null) {
//get the returned data
Bundle extras = data.getExtras();
//get the cropped bitmap
Bitmap thePic = extras.getParcelable("data");
//retrieve a reference to the ImageView
ImageView picView = (ImageView) findViewById(R.id.imageView1);
//display the returned cropped image
picView.setImageBitmap(thePic);
// open();
//1 Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
//startActivityForResult(intent, 0);
//1 startActivityForResult(intent,1);
}
}
else if(resultCode == RESULT_CANCELED){
Toast.makeText(getApplicationContext(),
"User cancelled image capture", Toast.LENGTH_SHORT).show();
}
}catch(Exception e){
Log.e(logtag,e.toString());
String errormesage="scan failed";
Toast.makeText(ScanActivity.this,errormesage,Toast.LENGTH_SHORT).show();
}
}
private void performCrop(Uri picUri){
try {
//call the standard crop action intent (the user device may not support it)
Intent cropIntent = new Intent("com.android.camera.action.CROP");
System.out.println("Past intent");
//indicate image type and Uri
cropIntent.setDataAndType(picUri, "image/*");
//set crop properties
cropIntent.putExtra("crop", "true");
//indicate aspect of desired crop
cropIntent.putExtra("aspectX", 2);
cropIntent.putExtra("aspectY", 1);
//indicate output X and Y
cropIntent.putExtra("outputX", 128);
cropIntent.putExtra("outputY", 128);
//retrieve data on return
cropIntent.putExtra("return-data", true);
//start the activity - we handle returning in onActivityResult
startActivityForResult(cropIntent, PIC_CROP);
System.out.println("Past start activity for intent");
}
catch(Exception e){
//display an error message
String errorMessage = "oops - your device doesn't support the crop action!";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
System.out.println("exception message is :"+e.getMessage());
}
}

Taking a picture via camera intent in Android

I am using the following code for clicking a photo on Button click.
package com.example.clickpic;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
int TAKE_PHOTO_CODE = 0;
public static int count=0;
private ImageView imageView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//here,we are making a folder named picFolder to store pics taken by the camera using this application
final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/picFolder/";
File newdir = new File(dir);
newdir.mkdirs();
this.imageView = (ImageView)this.findViewById(R.id.imageView1);
Button capture = (Button) findViewById(R.id.btnCapture);
capture.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// here,counter will be incremented each time,and the picture taken by camera will be stored as 1.jpg,2.jpg and likewise.
count++;
String file = dir+count+".jpg";
File newfile = new File(file);
try {
newfile.createNewFile();
} catch (IOException e) {}
Uri outputFileUri = Uri.fromFile(newfile);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 1888);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1888 && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
}
The code works perfectly well but it just opens up the camera activity. What must I do so that on button click, it clicks a picture and saves it as well?
Right now, this just opens the camera app of the phone, and expects me to click the picture and click on OK. I don't want these things.
All I want is, when I click on the button from my app it should click a picture without expecting any additional input from me.
Edit
I have already tried to create a camera app of my own, but I ran into some issues.
That's why I am trying this approach. Camera API not working on KITKAT
All I want is, when I click on the button from my app it should click a picture without expecting any additional input from me. -
Its not achieved by Camera Intent like android.provider.MediaStore.ACTION_IMAGE_CAPTURE. You have to implement camera interface portion in your code without using native camera application of Device.
Look at portion of Building Camera App
I have written a camera class which takes picture, arranges the orientation (some devices takes photo horizontal as default) and saves the photo taken. You can check it from the link below:
Camera capture orientation on samsung devices in android
Edit: Sorry, savePhoto functions are not written in my example. Adding them now.
savePhoto function:
public void savePhoto(Bitmap bmp) {
imageFileFolder = new File(Environment.getExternalStorageDirectory(),
cc.getDirectoryName());
imageFileFolder.mkdir();
FileOutputStream out = null;
Calendar c = Calendar.getInstance();
String date = fromInt(c.get(Calendar.MONTH))
+ fromInt(c.get(Calendar.DAY_OF_MONTH))
+ fromInt(c.get(Calendar.YEAR))
+ fromInt(c.get(Calendar.HOUR_OF_DAY))
+ fromInt(c.get(Calendar.MINUTE))
+ fromInt(c.get(Calendar.SECOND));
imageFileName = new File(imageFileFolder, date.toString() + ".jpg");
try {
out = new FileOutputStream(imageFileName);
bmp.compress(Bitmap.CompressFormat.JPEG, 70, out);
out.flush();
out.close();
scanPhoto(imageFileName.toString());
out = null;
} catch (Exception e) {
e.printStackTrace();
}
}
scanPhoto function:
public void scanPhoto(final String imageFileName) {
geniusPath = imageFileName;
msConn = new MediaScannerConnection(MyClass.this,
new MediaScannerConnectionClient() {
public void onMediaScannerConnected() {
msConn.scanFile(imageFileName, null);
}
#Override
public void onScanCompleted(String path, Uri uri) {
msConn.disconnect();
}
});
msConn.connect();
}
SavePhotoTask class:
class SavePhotoTask extends AsyncTask<byte[], String, String> {
#Override
protected String doInBackground(byte[]... jpeg) {
File photo = new File(Environment.getExternalStorageDirectory(),
"photo.jpg");
if (photo.exists()) {
photo.delete();
}
try {
FileOutputStream fos = new FileOutputStream(photo.getPath());
fos.write(jpeg[0]);
fos.close();
} catch (java.io.IOException e) {
}
return (null);
}
}

Why crop is not running?

why the camera launches but the crop crash?
I am not getting any error message. We followed http://mobile.tutsplus.com/tutorials/android/capture-and-crop-an-image-with-the-device-camera/
package com.example.background;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
//keep track of camera capture intent
final int CAMERA_CAPTURE = 1;
//keep track of cropping intent
final int PIC_CROP = 2;
//captured picture
private Uri picUri;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//retrieve a reference to the UI button
Button captureBtn = (Button)findViewById(R.id.capture_btn);
//handle button clicks
captureBtn.setOnClickListener(this);
}
public void onClick(View v) {
if (v.getId() == R.id.capture_btn) {
try {
//use standard intent to capture an image
Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//we will handle the returned data in onActivityResult
startActivityForResult(captureIntent, CAMERA_CAPTURE);
}catch(ActivityNotFoundException anfe){
//display an error message
String errorMessage = "Whoops - your device doesn't support capturing images!";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
//user is returning from capturing an image using the camera
if(requestCode == CAMERA_CAPTURE){
picUri = data.getData();
performCrop();
}
//user is returning from cropping the image
else if(requestCode == PIC_CROP){
//get the returned data
Bundle extras = data.getExtras();
//get the cropped bitmap
Bitmap thePic = extras.getParcelable("data");
//retrieve a reference to the ImageView
ImageView picView = (ImageView)findViewById(R.id.picture);
//display the returned cropped image
picView.setImageBitmap(thePic);
}
}
}
is the problem here?
private void performCrop(){
try {
//call the standard crop action intent (the user device may not support it)
Intent cropIntent = new Intent("com.android.camera.action.CROP");
//indicate image type and Uri
cropIntent.setDataAndType(picUri, "image/*");
//set crop properties
cropIntent.putExtra("crop", "true");
//indicate aspect of desired crop
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
//indicate output X and Y
cropIntent.putExtra("outputX", 256);
cropIntent.putExtra("outputY", 256);
//retrieve data on return
cropIntent.putExtra("return-data", true);
//start the activity - we handle returning in onActivityResult
startActivityForResult(cropIntent, PIC_CROP);
}
catch(ActivityNotFoundException anfe){
//display an error message
String errorMessage = "Whoops - your device doesn't support the crop action!";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
Your picUri is getting null. thats why its giving an error while cropping an captured image, So, refer this link to crop an images. its worked for me,
Hope this will work for you.
I don't know for a fact that this is what you are seeing, but unfortunately this Intent goes to the AOSP Camera app. Not all devices have it, and worse, it isn't in lollipop anymore.
I'm currently trying to figure out if there is an equivalent in the new picture editor app from google or if I really have to start using an external library for that...
A pity not to have such a standard feature available anymore.

Variable types of videos and images

Am working on some application. However, i have finished two modules which invoke the phones' native camera to take a snapshot and an also record a video. I intend to use the phone application to send the image and the video taken and recorded by the phone to a website i intend to create. However, for textual information, i could store the information as strings, for the image and the video, i am not sure if i should leave them as Uris upon submission. Below is my picture, and video programs respectively. Thanx
Picture code:
package com.project;
import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MyPicture extends Activity {
/** Called when the activity is first created. */
/*constant and variable created so as to work with the taken pictures*/
private static int TAKE_PICTURE = 1;
private Uri outputFileUri;
Uri imageUri;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pic);
Button pictureButton=(Button) findViewById(R.id.pictureButton);
pictureButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment.getExternalStorageDirectory(),"test.jpg");
outputFileUri = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, TAKE_PICTURE);
}
});
}
#Override
protected void onActivityResult(int requestCode,int resultCode, Intent data){
if (requestCode == TAKE_PICTURE){
imageUri = data.getData();
//do something about the image in the in outputFileUri
Toast.makeText(MyPicture.this,
"Picture successfully taken",
Toast.LENGTH_SHORT).show();
Intent i = new Intent(MyPicture.this,/*program execution proceeds back to MyPicture, our start page after success of image takin*/
Myindex.class);
startActivity(i);
}else{
Toast.makeText(MyPicture.this,
"Picture Unsuccessfully taken",
Toast.LENGTH_SHORT).show();
Intent i = new Intent(MyPicture.this,/*program execution proceeds back to MyPicture, so we can redo the recording*/
MyPicture.class);
startActivity(i);
}
}
}
Video code:
package com.project;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MyVideo extends Activity {
/*program for the vid button*/
private static int RECORD_VIDEO = 1;
private static int HIGH_VIDEO_QUALITY = 1;
//private static int MMS_VIDEO_QUALITY = 0;
Uri recordedVideo;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.vid);
Button videoButton=(Button) findViewById(R.id.videoButton);
videoButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
//intent.putExtra(MediaStore.EXTRA_OUTPUT, output);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, HIGH_VIDEO_QUALITY);
startActivityForResult(intent, RECORD_VIDEO);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if (requestCode == RECORD_VIDEO){
recordedVideo = data.getData();
//to do something with the recorded video
//we shall insert this information in the database
Toast.makeText(MyVideo.this,
"Video successfully recorded",
Toast.LENGTH_SHORT).show();
Intent i = new Intent(MyVideo.this,/*program execution proceeds back to Myindex, our start page*/
Myindex.class);
startActivity(i);
}
else{
/*Happens after unsuccessfull recording of video*/
Toast.makeText(MyVideo.this,
"Video Unsuccessfully recorded",
Toast.LENGTH_SHORT).show();
Intent i = new Intent(MyVideo.this,/*program execution proceeds back to MyVideo, so we can redo the recording*/
MyVideo.class);
startActivity(i);
}
}
}
For images what I usually do is to decode the data as a Bitmapand then I send it via Http Post using the multipart content-type.
You can decode the image file as a Bitmap using: BitmapFactory.decodeFile.
Here is an example of how I send the Bitmap with multipart using the Apache library:
public String doHttpMultipart(String url,
List<NameValuePair> pairs,
Bitmap bitmap,
String fileName) throws IOException,
ClientProtocolException,
UnsupportedEncodingException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 100, bos);
byte[] imageData = bos.toByteArray();
ByteArrayBody byteArrayBody = new ByteArrayBody(imageData, fileName);
MultipartEntity reqEntity =
new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("image", byteArrayBody);
for(NameValuePair p : pairs) {
reqEntity.addPart(p.getName(), new StringBody(p.getValue()));
}
HttpPost request = new HttpPost(url);
request.setEntity(reqEntity);
HttpClient client = new DefaultHttpClient();
HttpResponse httpResponse = client.execute(request);
String response = "";
BufferedReader in = null;
try {
response = super.readHttpStream(response, in, httpResponse);
} catch(IllegalStateException e) {
throw new IllegalStateException();
} catch(IOException e) {
throw new IOException();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return response;
}
For a video file, it should be the same you should see how to decode it as an array of bytes and send it with multipart.

Categories

Resources