Camera Intent - Storage full - android

When I press the photoButton, the default Camera App starts.
My Problem is that it says, that the storage is full.
When I start the Camera app seperate the Error didn't show up.
Here is the Code for the Camera Intent:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case CAMERA_PIC_REQUEST: {
try {
Bitmap image = (Bitmap) data.getExtras().get("data");
ImageView imageView = (ImageView) findViewById(R.id.taskPhotoImage);
imageView.setImageBitmap(image);
} catch (NullPointerException e) {
e.printStackTrace();
}
}
case REQ_CODE_SPEECH_INPUT: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
textField.setText(textField.getText() + " " + result.get(0));
}
break;
}
}
}
in onCreate() method:
photoButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
});
I don't know if it's a problem within the code. But it looks like that something goes wrong
Kind Regards

Try This:-
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// After camera screen this code will excuted
if (requestCode == CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
output.setText("Video File : " +data.getData());
// Video captured and saved to fileUri specified in the Intent
Toast.makeText(this, "Video saved to:" + data.getData(), Toast.LENGTH_LONG).show();
} else if (resultCode == RESULT_CANCELED) {
output.setText("User cancelled the video capture.");
// User cancelled the video capture
Toast.makeText(this, "User cancelled the video capture.",
Toast.LENGTH_LONG).show();
} else {
output.setText("Video capture failed.");
// Video capture failed, advise user
Toast.makeText(this, "Video capture failed.",
Toast.LENGTH_LONG).show();
}
}
}

try this
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case TAKE_PICTURE:
if (resultCode == Activity.RESULT_OK) {
Uri selectedImage = imageUri;
getContentResolver().notifyChange(selectedImage, null);
ImageView imageView = (ImageView) findViewById(R.id.ImageView);
ContentResolver cr = getContentResolver();
Bitmap bitmap;
try {
bitmap = android.provider.MediaStore.Images.Media
.getBitmap(cr, selectedImage);
imageView.setImageBitmap(bitmap);
Toast.makeText(this, selectedImage.toString(),
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
.show();
Log.e("Camera", e.toString());
}
}
}}
And if you want camera intent
private static final int TAKE_PICTURE = 1;
private Uri imageUri;
public void takePhoto(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File photo = new File(Environment.getExternalStorageDirectory(), "picture.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photo));
imageUri = Uri.fromFile(photo);
startActivityForResult(intent, TAKE_PICTURE);
}

Related

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

capture image from camera and do fixed cropping in android

I am trying to crop the image as we are doing in facebook. I have used this link in my app: https://github.com/oginotihiro/cropview This is working fine for me.But here when I click on a button, its directly going to gallery and cropping the selected image.Instead of that I want to open camera in my device and click the image and I want to do cropping.How can I do this?Can someone help me.
I have tried this code.But I am not able to implement.
imageBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
reset();
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_PICK);
}
});
doneBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final ProgressDialog dialog = ProgressDialog.show(StudentDetails.this, null, "Please wait…", true, false);
cropView.setVisibility(View.GONE);
layout4.setVisibility(View.GONE);
resultIv.setVisibility(View.VISIBLE);
layout3.setVisibility(View.GONE);
layoutUpload.setVisibility(View.VISIBLE);
// editTextName.setVisibility(View.GONE);
buttonUpload.setVisibility(View.VISIBLE);
new Thread() {
public void run() {
croppedBitmap = cropView.getOutput();
runOnUiThread(new Runnable() {
#Override
public void run() {
// cropped image set
resultIv.setImageBitmap(croppedBitmap);
}
});
Uri destination = Uri.fromFile(new File(getCacheDir(), "cropped"));
CropUtil.saveOutput(StudentDetails.this, destination, croppedBitmap, 1);
runOnUiThread(new Runnable() {
#Override
public void run() {
dialog.dismiss();
}
});
}
}.start();
}
});
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK && requestCode == REQUEST_PICK) {
filePath = data.getData();
cropView.setVisibility(View.VISIBLE);
layoutImage.setVisibility(View.VISIBLE);
layout3.setVisibility(View.GONE);
layout4.setVisibility(View.VISIBLE);
textNameVal.setVisibility(View.GONE);
text1.setVisibility(View.GONE);
buttonUpload.setVisibility(View.GONE);
int x=(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, getResources().getDisplayMetrics());
int y=(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, getResources().getDisplayMetrics());
cropView.of(filePath).withAspect(x,y).initialize(StudentDetails.this);
}
}
Step1: Start to open gallery
Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
Step2: Get Image from gallery:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
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 picturePath = cursor.getString(columnIndex);
cursor.close();
ImageView imageView = (ImageView) findViewById(R.id.imgView);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
With your case, you can crop at the onActivityResult method.
It will help you.
With camera:
Step1: Open camera
/*
* Capturing Camera Image will lauch camera app requrest image capture
*/
private void captureImage() {
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_CAPTURE_IMAGE_REQUEST_CODE);
}
Step2: Get image from camera.
/**
* Receiving activity result method will be called after closing the camera
* */
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// if the result is capturing Image
if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// successfully captured the image
// display it in image view
previewCapturedImage();
} 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();
}
}
}
Step3: Add permission
<!-- Accessing camera hardware -->
<uses-feature android:name="android.hardware.camera" />

image capture app crashes on pressing back button

public class MainActivity extends Activity {
private static final int CAMERA_PIC_REQUEST = 2500;
Button Report_help;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Report_help=(Button)findViewById(R.id.report_help);
Report_help.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_PIC_REQUEST) {
Bitmap image = (Bitmap) data.getExtras().get("data");
ImageView imageview = (ImageView) findViewById(R.id.display_image);
imageview.setImageBitmap(image);
}
}
}
This app captures the image and displays in the imageview.But the problem is after I capture the image and press the back button app crashes.I don't know why is this so? Please anyone help.
I think when you press back button
Bitmap image = (Bitmap) data.getExtras().get("data");
in onActivityResult cause the Null pointer exception error, please catch this one.
Use the below code to check that case.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Uri selectedImageUri = null;
String filePath = null;
switch (requestCode) {
case PICK_Camera_IMAGE:
if (resultCode == RESULT_OK) {
//use imageUri here to access the image
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT).show();
}
break;
}
hope this helps you.

How to upload image from gallery in android

I want to upload image from my phone gallery into my application .In my application there is button named upload. when i click button,it should move to gallery and in gallery if i select image that selected image should display as thumbnail in application.I want to upload 10 images, from gallery in my application.
On click of the gallery button, start startActivityForResult as follows:
startActivityForResult(new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI), GET_FROM_GALLERY);
Consequently, detect GET_FROM_GALLERY (which is a static int, any request number of your choice e.g., public static final int GET_FROM_GALLERY = 3;) inside onActivityResult.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//Detects request codes
if(requestCode==GET_FROM_GALLERY && resultCode == Activity.RESULT_OK) {
Uri selectedImage = data.getData();
Bitmap bitmap = null;
try {
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
To view gallery:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),REQUEST_CODE);
and to use it in your app:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
switch (requestCode) {
case REQUEST_CODE:
if (resultCode == Activity.RESULT_OK) {
//data gives you the image uri. Try to convert that to bitmap
break;
} else if (resultCode == Activity.RESULT_CANCELED) {
Log.e(TAG, "Selecting picture cancelled");
}
break;
}
} catch (Exception e) {
Log.e(TAG, "Exception in onActivityResult : " + e.getMessage());
}
}
This is the way to go:
startActivityForResult(
new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI
),
GET_FROM_GALLERY
);

Getting identifier of originating button used to call camera intent on Android

If I have multiple buttons on a view to call camera intent (android.provider.MediaStore.ACTION_IMAGE_CAPTURE) and a ImageView for the preview of each image and I need to know which button called it in onActivityResult so I know which corresponding preview to use how do I pass an identifying variable? Below is current code that only works with one image.
Picture button:
final ImageButton cameraTakePhotoButton = (ImageButton) photoPromptOption.findViewById(R.id.cameraTakePhotoButton);
cameraTakePhotoButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
});
onActivityResult:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if(requestCode == CAMERA_PIC_REQUEST) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
final ImageView questionPhotoResult = (ImageView) findViewById(R.id.questionPhotoResult);
questionPhotoResult.setImageBitmap(thumbnail);
}
}
}
Ended up using a ListView and custom adapter then having it iterate over an ArrayList with objects of class Photo... I update the Bitmap (thumbnail) property for the Photo objects when I take the picture then refresh the ListView. This method works very well.
photoListView.setAdapter(new ArrayAdapter<Photo>(this, R.layout.photo_list_item, photosList) {
#Override
public View getView(final int position, View convertView, final ViewGroup parent) {
View row = null;
final Photo thisPhoto = getItem(position);
if (null == convertView) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.photo_list_item, null);
} else {
row = convertView;
}
photoPreview.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
File photoDirectory = new File(Environment.getExternalStorageDirectory()+"/Pictures/appName");
if(!photoDirectory.isDirectory()) {
photoDirectory.mkdir();
}
File photo = new File(Environment.getExternalStorageDirectory()+"/Pictures/appName/", thisPhoto.getId()+"_photo.jpg");
CameraHandlerSingleton.setPictureUri(Uri.fromFile(photo));
CameraHandlerSingleton.setPhotoId(thisPhoto.getId());
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
startActivityForResult(intent, CAMERA_PIC_REQUEST);
}
});
return row;
}
});
Then my onActivityResult:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK) {
if(requestCode == CAMERA_PIC_REQUEST) {
Uri selectedImage = CameraHandlerSingleton.getPictureUri();
String photoId = CameraHandlerSingleton.getPhotoId();
getContentResolver().notifyChange(selectedImage, null);
ContentResolver cr = getContentResolver();
Bitmap thumbnail;
try {
thumbnail = Bitmap.createScaledBitmap(android.provider.MediaStore.Images.Media.getBitmap(cr, selectedImage), 300, 200, true);
p.setThumbnail(thumbnail);
p.setTaken(true);
} catch(FileNotFoundException e) {
Toast.makeText(this, "Picture not found.", Toast.LENGTH_SHORT).show();
e.printStackTrace();
} catch(IOException e) {
Toast.makeText(this, "Failed to load.", Toast.LENGTH_SHORT).show();
Log.e("Camera", e.toString());
} catch(Exception e) {
Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
Log.e("Camera Exception", e.toString());
e.printStackTrace();
}
startActivity(getIntent());
finish();
}
}
}
Yikes. Why not use requestCode? Just tag each view with a unique code, making sure it doesn't clash with any other intents you're throwing around. Alternatively, you can use Object.hashCode() , but again, watch out for clashes.
cameraTakePhotoButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST + v.getTag());
}
});
Then check it in your handler:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if(requestCode == CAMERA_PIC_REQUEST + button1.getTag()) {
// do stuff
}
else if (requestCode == CAMERA_PIC_REQUEST + button2.getTag()) {
// do more stuff
}
}
}
If you have many buttons (or items in a ListView) that you're going to handle similarly, you can use a Map to recover the calling view from the tag.

Categories

Resources