My app takes a picture using the phones camera and stores the picture in the phones gallery. I would like to then get that picture path and store it in my datastore. Can someone please help me? Heres my code:
public void onClick(View v){
switch(v.getId())
{
case R.id.btnLoadPic:
//Options for the dialogue menu
final CharSequence[] items = {"Camera", "Gallery"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Choose an Option");
builder.setItems(items, new DialogInterface.OnClickListener() {
/**
* Make onclick functionality for the options in the dialogue menu
*/
public void onClick(DialogInterface dialog, int item) {
// Camera option
if (item == 0){
PackageManager pm = getPackageManager();
if (pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)){
//Toast.makeText(this, "camera", Toast.LENGTH_SHORT).show();
dispatchTakePictureIntent(11);
} else {
Toast.makeText(null, "No camera avalible", Toast.LENGTH_SHORT).show();
}
}
// Gallery option this works fine
private void dispatchTakePictureIntent(int actionCode) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePictureIntent, actionCode);
//handleSmallCameraPhoto(takePictureIntent);
}
private void handleSmallCameraPhoto(Intent intent) {
Bundle extras = intent.getExtras();
Bitmap mImageBitmap = (Bitmap) extras.get("data");
ImageView mImageView = (ImageView) this.findViewById(R.id.imagePlayer);
mImageView.setImageBitmap(mImageBitmap);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
handleSmallCameraPhoto(data);
}
The last bits of codes accesses the camera and displays the picture in the imageview. How do i get that picture path in a string format?
This will help. Tested and worked!
public class MainActivity extends Activity {
private static final int REQUEST_IMAGE = 100;
private static final String TAG = "MainActivity";
TextView tvPath;
ImageView picture;
File destination;
String imagePath;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvPath = (TextView) findViewById(R.id.idTvPath);
picture = (ImageView) findViewById(R.id.idIvImage);
String name = dateToString(new Date(),"yyyy-MM-dd-hh-mm-ss");
destination = new File(Environment.getExternalStorageDirectory(), name + ".jpg");
Button click = (Button) findViewById(R.id.idBtnTakePicture);
click.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(destination));
startActivityForResult(intent, REQUEST_IMAGE);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if( requestCode == REQUEST_IMAGE && resultCode == Activity.RESULT_OK ){
try {
FileInputStream in = new FileInputStream(destination);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 10;
imagePath = destination.getAbsolutePath();
tvPath.setText(imagePath);
Bitmap bmp = BitmapFactory.decodeStream(in, null, options);
picture.setImageBitmap(bmp);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
else{
tvPath.setText("Request cancelled");
}
}
public String dateToString(Date date, String format) {
SimpleDateFormat df = new SimpleDateFormat(format);
return df.format(date);
}
}
Dont forget to add these in your manifest file:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA"/>
Related
I am making an app that sends a picture taken from the Camera app however the image it returns seems to be only a thumbnail how can I get it to turn the whole image?
The following code gets an image, but it's too small.
public class OnTheJobActivity extends Activity{
private static final int CAMERA_PIC_REQUEST = 1337;
private Button takePictureButton;
private Button sendPictureButton;
private Bitmap thumbnail;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.onthejob);
takePictureButton = (Button) findViewById(R.id.takePictureButton);
takePictureButton.setOnClickListener(takePictureButtonListener);
sendPictureButton = (Button) findViewById(R.id.sendPictureButton);
sendPictureButton.setOnClickListener(sendPictureButtonListener);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_PIC_REQUEST) {
thumbnail = (Bitmap) data.getExtras().get("data");
ImageView image = (ImageView) findViewById(R.id.photoResultView);
image.setImageBitmap(thumbnail);
sendPictureButton.setVisibility(Button.VISIBLE);
}
}
private OnClickListener takePictureButtonListener = new OnClickListener() {
#Override
public void onClick(View arg0){
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
};
private OnClickListener sendPictureButtonListener = new OnClickListener() {
#Override
public void onClick(View arg0){
Intent i = new Intent(Intent.ACTION_SEND);
i.putExtra(Intent.EXTRA_EMAIL, "abc#gmail.com");
i.putExtra(Intent.EXTRA_SUBJECT,"On The Job");
i.putExtra(Intent.EXTRA_STREAM, thumbnail);
i.setType("image/bmp");
startActivity(Intent.createChooser(i,"Emailfile"));
}
};
}
You could also change the intent you're using.
//in your buttonListener
ContentValues values = new ContentValues();
imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
//create new Intent
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
try{
startActivityForResult(i, ACTIVITY_GET_IMAGE);
}
catch(Exception ex){
Log.v("BRE", ex.toString());
}
//in your activity
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == ACTIVITY_GET_IMAGE){
if(resultCode == RESULT_OK){
try{String uri = data.getData().toString()}
catch(NullPointerException e){//do something}
}
}
}
This will return a uri which you can then use to access the full resolution image
Try using the implementation shown here
Specifically:
Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
public void onPictureTaken(byte[] imageData, Camera c) {
}
};
I need to store image from camera, and gallery to firebase. In my project upload from gallery working, but when i try to upload from camera nothing happens. When click on button you chose between gallery or camera. When take picture or chose image from gallery, in imageview i get picture. Then i click on save and if picture is from gallery then is saved to storage, and create child in database, but if picture is from camera it won't work. Is there solution for this problem?
MainActivity.java
public class MainActivity extends AppCompatActivity {
private Uri mImageUri = null;
private Button btn,mSubmitBtn;
private ImageView imageview;
private int GALLERY = 1, CAMERA = 2;
private StorageReference mStorage;
private DatabaseReference mDatabase;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn);
mSubmitBtn = (Button)findViewById(R.id.submit);
imageview = (ImageView) findViewById(R.id.iv);
mStorage = FirebaseStorage.getInstance().getReference();
mDatabase = FirebaseDatabase.getInstance().getReference().child("Upload");
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showPictureDialog();
}
});
mSubmitBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startPosting();
}
});
}
private void showPictureDialog(){
AlertDialog.Builder pictureDialog = new AlertDialog.Builder(this);
pictureDialog.setTitle("Select Action");
String[] pictureDialogItems = {
"Select photo from gallery",
"Capture photo from camera" };
pictureDialog.setItems(pictureDialogItems,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
choosePhotoFromGallary();
break;
case 1:
takePhotoFromCamera();
break;
}
}
});
pictureDialog.show();
}
public void choosePhotoFromGallary() {
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, GALLERY);
}
private void takePhotoFromCamera() {
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA);
}
////////////////////////////
private void startPosting(){
if (mImageUri !=null){
StorageReference filepath = mStorage.child("Images").child(mImageUri.getLastPathSegment());
filepath.putFile(mImageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Uri downloadUrl = taskSnapshot.getDownloadUrl();
DatabaseReference newPost = mDatabase.push();//push kreira uniq random id
newPost.child("image").setValue(downloadUrl.toString());
}
});
}
}
////////////////////////
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == this.RESULT_CANCELED) {
return;
}
if (requestCode == GALLERY) {
if (data != null) {
mImageUri = data.getData();
imageview.setImageURI(mImageUri);
}
} else if (requestCode == CAMERA) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
imageview.setImageBitmap(thumbnail);
imageview.setImageURI(mImageUri);
}
}
}
I faced the same issue and this happens mostly on Kitkat and lower version.When the image is clicked than getting the bitmap file directly try to do following way,
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
File destination = new File(Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".jpg");
FileOutputStream fo;
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
} catch (IOException e) {
e.printStackTrace();
}
imageview.setImageBitmap(thumbnail);
Also in the manifest file make sure you have declared the following permission too along with the READ_EXTERNAL
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Reason for this issue is because Android 4.4 (KitKat) has changed how permissions are handled on the SD card. So its best practice to make your own directory on the SD card to handle all files your app needs to write. This works for all the higher versions too.
Hope this helps.
hello i am struck in this coding first of all i am having two image view's when we click one of them the chooser dialog box will open which is having two option 1.take photo 2.select from gallery when i select from galley everything is working fine where as the problem is,
when we select take photo option and after we had captured the image when setting its preview for the first image its working fine where as the problem here is in the second image. when we select take photo option and after capturing the photo it is replacing the image of the first image with the second image.
This is Java Code:
public class PrescriptionUpload extends ActionBarActivity {
ImageView uploadimage1, uploadimage2;
Bitmap imagemap, imagemapsec;
int imageupload = 1;
int imagesecpload = 2;
int requestcamera = 0;
Button uploadpric;
StringBuilder sb;
String uploadimg2;
Toolbar include2;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.prescriptionimages);
include2= (Toolbar) findViewById(R.id.include2);
setSupportActionBar(include2);
assert getSupportActionBar() != null;
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
uploadimage1 = (ImageView) findViewById(R.id.uploadimage1);
uploadimage1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
selectimage(imageupload);
}
});
uploadimage2 = (ImageView) findViewById(R.id.uploadimage2);
uploadimage2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
selectimage(imagesecpload);
}
});
}
private void selectimage(final int number) {
final CharSequence[] items = {"Take Photo", "Choose from Gallery"};
AlertDialog.Builder builder = new AlertDialog.Builder(PrescriptionUpload.this);
builder.setTitle("Add Prescription");
builder.setItems(items, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int item) {
if (items[item].equals("Take Photo")) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, requestcamera);
} else if (items[item].equals("Choose from Gallery")) {
Intent intent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(
Intent.createChooser(intent, "Select File"),
number);
}
}
});
builder.show();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
Uri selectedImageUri = data.getData();
if (requestCode == imageupload) {
try {
imagemap = MediaStore.Images.Media.getBitmap(getContentResolver(), selectedImageUri);
uploadimage1.setImageBitmap(imagemap);
uploadimage2.setVisibility(View.VISIBLE);
} catch (IOException e) {
e.printStackTrace();
}
} else if (requestCode == imagesecpload) {
try {
imagemapsec = MediaStore.Images.Media.getBitmap(getContentResolver(), selectedImageUri);
uploadimage2.setImageBitmap(imagemapsec);
} catch (IOException e) {
e.printStackTrace();
}
} else if (requestCode == requestcamera) {
if (uploadimage1.getId()==R.id.uploadimage1){
Bitmap testurl = (Bitmap) data.getExtras().get("data");
uploadimage1.setImageBitmap(testurl);
uploadimage2.setVisibility(View.VISIBLE);
}else if (uploadimage2.getId()==R.id.uploadimage2){
Bitmap testurltwo = (Bitmap) data.getExtras().get("data");
uploadimage2.setImageBitmap(testurltwo);
}
}
}
}
i had solved my problem using onActivityResult and with the increment of the variable
how to store the imagePath of the image that I had captured using the camera activity to database. I also need to retrieve that imagePath and display on another activity?
Can someone help me please?
Update:
public class Image_secondPage extends Activity
{
public static final int CAMERA_RESULT = 1;
Button btn1;
ImageView imageView1;
private final String tag = getClass().getName();
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.image_secondpage);
imageView1 = (ImageView)findViewById(R.id.imageView1);
Button btnNext = (Button)findViewById(R.id.buttonNext);
btnNext.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Intent intent = new Intent(Image_secondPage.this, ImagesPage.class);
startActivity(intent);
}
});
PackageManager pm = getPackageManager();
if(pm.hasSystemFeature(PackageManager.FEATURE_CAMERA))
{
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.EXTRA_OUTPUT, MyFileContentProvider.CONTENT_URI);
startActivityForResult(i, CAMERA_RESULT);
}
else
{
Toast.makeText(getBaseContext(), "Camera is not available", Toast.LENGTH_LONG).show();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
Log.i(tag, "Receive the camera result");
if(resultCode == RESULT_OK && requestCode == CAMERA_RESULT)
{
File out = new File(getFilesDir(), "newImage.jpg");
if(!out.exists())
{
Toast.makeText(getBaseContext(), "Error while capturing image", Toast.LENGTH_LONG).show();
return;
}
Bitmap mBitmap = BitmapFactory.decodeFile(out.getAbsolutePath());
imageView1.setImageBitmap(mBitmap);
}
}
#Override
protected void onDestroy()
{
super.onDestroy();
imageView1 = null;
}
I am supposed to get the imagePath that I have captured and show the image on the imageView that I had on the next class but I don't know how. Can someone help me?
try use this
bitmap = (Bitmap) data.getExtras().get("data");
this will give you bitmap image, then you can save it to database or anything you want.
let me know if this not solve your problem.
I am making an app that sends a picture taken from the Camera app however the image it returns seems to be only a thumbnail how can I get it to turn the whole image?
The following code gets an image, but it's too small.
public class OnTheJobActivity extends Activity{
private static final int CAMERA_PIC_REQUEST = 1337;
private Button takePictureButton;
private Button sendPictureButton;
private Bitmap thumbnail;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.onthejob);
takePictureButton = (Button) findViewById(R.id.takePictureButton);
takePictureButton.setOnClickListener(takePictureButtonListener);
sendPictureButton = (Button) findViewById(R.id.sendPictureButton);
sendPictureButton.setOnClickListener(sendPictureButtonListener);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_PIC_REQUEST) {
thumbnail = (Bitmap) data.getExtras().get("data");
ImageView image = (ImageView) findViewById(R.id.photoResultView);
image.setImageBitmap(thumbnail);
sendPictureButton.setVisibility(Button.VISIBLE);
}
}
private OnClickListener takePictureButtonListener = new OnClickListener() {
#Override
public void onClick(View arg0){
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
};
private OnClickListener sendPictureButtonListener = new OnClickListener() {
#Override
public void onClick(View arg0){
Intent i = new Intent(Intent.ACTION_SEND);
i.putExtra(Intent.EXTRA_EMAIL, "abc#gmail.com");
i.putExtra(Intent.EXTRA_SUBJECT,"On The Job");
i.putExtra(Intent.EXTRA_STREAM, thumbnail);
i.setType("image/bmp");
startActivity(Intent.createChooser(i,"Emailfile"));
}
};
}
You could also change the intent you're using.
//in your buttonListener
ContentValues values = new ContentValues();
imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
//create new Intent
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
try{
startActivityForResult(i, ACTIVITY_GET_IMAGE);
}
catch(Exception ex){
Log.v("BRE", ex.toString());
}
//in your activity
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == ACTIVITY_GET_IMAGE){
if(resultCode == RESULT_OK){
try{String uri = data.getData().toString()}
catch(NullPointerException e){//do something}
}
}
}
This will return a uri which you can then use to access the full resolution image
Try using the implementation shown here
Specifically:
Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
public void onPictureTaken(byte[] imageData, Camera c) {
}
};