I am trying to store an image file in Parse.com for use as a profile photo.
I have a function used to capture the image:
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, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
According to Parse documentation, I need to have the data in byte[] form and then create a ParseFile with it. So, I have this:
byte[] data = fileUri.getBytes();
// Save image to Parse
photoFile = new ParseFile("profile_photo.jpg", data);
I am getting an error: cannot resolve method getBytes. Not entirely sure if I am missing an import, or if I am approaching this the entirely wrong way. How can I convert a captured image file to byte form so I can create a ParseFile?
Related
I'm currently trying to save images taken from a phone to its gallery, but the code below only works if I choose the stock camera app when the chooser dialog pops up. Whenever I choose another camera app(e.g., the Google camera), the taken picture doesn't get saved any where.
To make things even stranger, sometimes the picture does show up in its designated directory in the gallery, but after 15 mins or so, the same goes for when I use the stock camera app: the picture will get saved in the default camera shots directory, but takes quite a bit to show up in its designated directory, if it shows up there at all.
// Capturing Camera Image will launch camera app request image capture
void captureImage() {
//file uri to store image.
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
// Request camera app to capture image
Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
// start the image capture Intent
getActivity().startActivityForResult(captureIntent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
}
well ,
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
does not work anymore .
you should do something like this :
call Camera Activity :
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
and onActivityResult :
if (data.getData() == null) {
Bitmap bm = (Bitmap)
data.getExtras().get("data");
String timeStamp = new SimpleDateFormat(
"yyyyMMdd_HHmmss").format(new Date());
File pictureFile = new File(Environment
.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES)
.getAbsolutePath()
+ File.separator + "IMG_" + timeStamp);
try {
FileOutputStream fos = new FileOutputStream(
pictureFile);
bm.compress(Bitmap.CompressFormat.JPEG, 90, fos);
fos.close();
String filePath = pictureFile.getAbsolutePath();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} } else {
Uri imgUri =data.getData());
}
It turns out my code was working after all. The pictures were being saved in the new directory, but the problem was that the gallery wasn't being updated, which explains why the photos would randomly appear in the directory later on. Being new to this, it never occurred to me that I would have to update the gallery. I only came to this realization after using ES File Explorer to look through my files. To fix my problem, I just made a new method in my CameraFragment that would call on the media scanner. I called this method from onActivityResult().
Here's the new method, though there's nothing really "new" about it since I ran into the same code on other SO questions:
protected void mediaScan() {
getActivity().sendBroadcast(
new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
Uri.parse(fileUri.toString())));
}
I also don't need to call the package manager and iterate through the apps that could handle the camera intent if I'm not giving the option to use choose a picture from a gallery, so I'm going to remove all that from my question.
In my application I use the camera to take photos.
I use this code to start the camera Activity:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
timeStamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
File file = new File(directory, timeStamp+".png"); //name
Uri outputFileUri1 = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri1);
startActivityForResult(intent, CAMERA_RESULT);
This code is working, but how can I edit the preview image (crop, rotate,...) before my main Activity gets the data in onActivityResult()?
Or how can I start the photo editor for my image from my application?
You should create a Bitmap object out of your image, then you could manipulate it.
String fooFile = "PATH TO FILE";
Bitmap bmp = BitmapFactory.decodeFile(fooFile);
here is a crop example. for more examples just Google for 'Bitmap manipulation android'
This code save the image in " MyApp ".but how to load the image in other activity
final EditText txtRegid = (EditText)this.findViewById(R.id.regid);
String RegID = txtRegid.getText().toString();
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
File mImageFile = new File(Environment.getExternalStorageDirectory()+File.separator+"MyApp",
"PIC"+RegID+".jpg");
String mSelectedImagePath = mImageFile.getAbsolutePath();
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mImageFile));
startActivityForResult(intent, TAKE_PICTURE);
Well, if you have defined the BitMap variable as a data member of your MyApp class, you can easily fetch it in other activities by using a getter() function to fetch that BitMap image which you are storing in MyApp.
Or, you could pass your image along with Intent, by encoding it into String format.
If you know the absolute path of the file. You can use BitmapFactory.decodeFile(filePath, opts) method to get the Bitmap. Then use ImageView's method setImageBitmap() to show it.
When I'm taking a photo from a camera if I'm calling
File file = new File(getFilesDir().getAbsolutePath() + "/myImage.jpg");
Uri outputFileUri = Uri.fromFile(file);
cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
OK button on camera app is not functioning, simply does nothing (actually won't save it to internal memory I provided I guess and therefore the app itself does nothing).
If I however call
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/myImage.jpg");
Uri outputFileUri = Uri.fromFile(file);
cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
everything is fine and photo is stored on SDCard.
My question is, is there a way to store capture photo in full-resolution without SDCard?
The native camera app cannot save the image in your app's private internal directories as those are only available to your particular app.
Instead you can create a custom camera activity to save images to your internal directories or you need to use the stock camera app with external storage.
Note: if you plan on creating a custom camera activity make sure you target at least 2.3 and up. Anything below that mark is very difficult to work with.
The Camera activity will not be able to save the file into your activity's private files directory, that's why it fails quietly. You can move the image from the external storage into your files dir in onActivityResult.
You need to add permission
cameraIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
This is possible using the file provider. Please refer sample
public getOutputUri(#NonNull Context pContext) {
String photo = photo.jpeg;//your file name
File photoFile = new File(pContext.getFilesDir(), photo);
Uri lProviderPath = FileProvider.getUriForFile(pContext,
pContext.getApplicationContext()
.getPackageName() + ".provider", photoFile);
return lProviderPath;
}
private void capturePhoto() {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, getOutputUri(this));
cameraIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivityForResult(cameraIntent, 1);
}
Refer following android document for more details
https://developer.android.com/reference/android/support/v4/content/FileProvider
In my application I capture photo by android camera and then I want to send it to the server. For this I use Client Socket programming. I convert the capture photo into bytearray(byte[]) and then send it to the server. Every thing work perfactally.
Problem is there I am not able to send original photo to the server. Thumbnail photo is sended by the android mobile phone. But when I capture photo by the camera then original photo is there in Gallery.
How to get this original photo to use in my application?
My Code:
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra("outputFormat",Bitmap.CompressFormat.JPEG);
cameraIntent.putExtra("return-data", true);
startActivityForResult(Intent.createChooser(cameraIntent, "Select Picture"),
CAMERA_REQUEST);
And in onActivityResult method:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
// byte[] a = (byte[]) data.getExtras().getByteArray("data"); // I also use this but not work
ByteArrayOutputStream bos = new ByteArrayOutputStream();
photo.compress(CompressFormat.JPEG, 100, bos);
byte[] bitmapdata = bos.toByteArray();
}
}
in bitmapdata photo is there but compressed photo not the original.
Some of people say that, change quality field of compress(CompressFormat.JPEG, 100, bos); in between 0 to 100 but nothing will happen.
-- >> is there any other way also to get original photo which is captured by the camera
-- >> When The photo is in folder of my computer then I read this photo in the file by giving the path. ex- File file = new File(c:\newphoto\image.jpg); . can I use this code in android to read original photo because I know the name and location of photo. If yes then what is the path to read photo in gallery. Is this work if I give path as: \DCIM\Camera\photoName.jpg.
-- >> Or some change need in my current code and it will work fine?
You are using the Intent to capture the image using ACTION_IMAGE_CAPTURE. If you normally starts your camera using Intent then it will return image as a bitmap in onActivityResult() but it will be for thumbnail purpose.
If you want to get full resolution image from the camera then you should provide a file with the intent which you are firing to start the camera activity.
You can do like below
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File out = Environment.getExternalStorageDirectory();
out = new File(out, imagename);
i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(out));
startActivityForResult(i, CAMERA_RESULT);
Here there is a MediaStore.EXTRA_OUTPUT parameter which takes a Uri of the file in which you want your camera to write a images.
For more information you can refer below example
Capture full resolution image from camera