I am capturing image using camera in android using the following code snippet.
Intent imageIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(Constants.ATTACH_IMAGE);
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
imageIntent.putExtra("outputFormat",Bitmap.CompressFormat.JPEG.toString());startActivityForResult(imageIntent, Constants.ATTACH_IMAGE);
in onActivityResult, I want to convert the "content://" uri back to File, compress it and show a thumb nail.
How can I do it?
Edit :-
What I have tried:-
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d("activity result","coming" + String.valueOf(requestCode)+ contentFileUri);
// View popupView = getActivity().getLayoutInflater().inflate(R.layout.popup_add_snag, null);
// LinearLayout attachLayout = (LinearLayout)popupView.findViewById(R.id.attachLayout);
// ImageView photoImageicon = (ImageView)popupView.findViewById(R.id.imageicon);
try{
// if (resultCode == Constants.RESULT_OK){
if(contentFileUri !=null) {
Log.d("fileuri", contentFileUri.toString());
String attachmentType = "IMAGE";
// mAdapter.attachmentType=attachmentType;
photoImageIcon.setVisibility(View.VISIBLE);
// attachLayout.setVisibility(View.INVISIBLE);
Toast.makeText(getActivity().getApplicationContext(), R.string.successfull_image, Toast.LENGTH_SHORT).show();
Log.d("fileuri", contentFileUri.toString());
InputStream inputStream = getContext().getContentResolver().openInputStream(contentFileUri);
contentFileUri looks like this:- "content://com.fa.ha.provider/external_files/F/7c7.jpeg"
But above results in "FileNotFound Exception, not a directory" at the last line.
Ideally, just hang onto the File that getOutputMediaFileUri() is creating. Hopefully, getOutputMediaFileUri() is setting up a FileProvider Uri for you, based off of a File, so if you hold onto the File, you do not need to worry about "converting" anything. See this sample app.
If getOutputMediaFileUri() is poorly written, perhaps you do not have a File. In that case, in onActivityResult(), you can pass the Uri to an image-loading library (Picasso, Glide, etc.) and have it populate an ImageView or simply load the Bitmap for you.
Related
I've written code in Android to capture an image and then use that image as follows:
private static Intent i;
final static int cons = 0;
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
public void takePicture()
{
i= new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //Open camera
startActivityForResult(i, cons);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Log.d("Testing if condition", "Test"); //This code does not execute
Bundle ext = data.getExtras();
Log.d("Upload image", ext.toString());
sendForUpload(data); // function to upload file to server
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
} else {
// Image capture failed, advise user
}
}
}
This code lets me take a photo and it saves to the SD card. However, I'm not sure if the file is sent to the sendForUpload function where I've handled getting the path and uploading the file. In fact nothing inside the if (resultCode == RESULT_OK) block works. How do I use the image captured from this activity?
Well, you have a few problems.
First, you are calling startActivityForResult(i, cons);. cons is 0. You are trying to compare 0 with CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE. Since CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE is 100, that will not work.
Second, you are calling data.getExtras().toString() (split over two Java statements). I would expect this to return a value like android.os.Bundle#34334143. If that is what you want to upload, fine. I am guessing that you want to upload the image. Since you did not include EXTRA_OUTPUT in your ACTION_IMAGE_CAPTURE Intent, you get a Bitmap of the thumbnail image taken by the camera via data.getParcelableExtra("data").
Third, you are making inappropriate assumptions:
This code lets me take a photo and it saves to the SD card.
There is no requirement for the camera app to save the image anywhere, let alone to removable storage. In fact, I would argue that this is a bug in your camera app. That is not surprising, as ACTION_IMAGE_CAPTURE implementations have bugs.
where I've handled getting the path and uploading the file
There is no path. You did not include EXTRA_OUTPUT in your ACTION_IMAGE_CAPTURE Intent, and so all you get back is the thumbnail Bitmap.
I'm using this to put the image in a ImageView:
Uri uri = data.getData();
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
You can use bitmap or uri objects for upload or show your picture..
Hope it helps you.
How to Prevent Adobe Creative SDK plugin from saving the edited file automatically in android?
When i try to edit the image, the file is automatically saved to device.
It's not possible to avoid saving the image, but you can set where it is saved using the AdobeImageIntent.Builder()'s .withOutput() method. Example:
Intent imageEditorIntent = new AdobeImageIntent.Builder(this)
.setData(uri) // input image source
.withOutput(Uri.parse("file://" + getFilesDir() + "/my-pic-name.jpg")) // output file destination
.build();
Whether you set the location using .withOutput() or not, you will get back the saved image's Uri in your onActivityResult() method:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
/*
`1` being an arbitrary int we used as a requestCode
when starting the `imageEditorIntent`
*/
case 1:
// Do things, for example:
Uri editedImageUri = data.getData();
mEditedImageView.setImageURI(editedImageUri);
break;
}
}
}
If you don't want to store the data long term, you might use that Uri to go back and delete the saved image when you are done with it.
I am using SquareCamera library (https://github.com/boxme/SquareCamera) for taking square picture.The problem I am facing is that SquareCamera is creating its own folder where taken pics are getting stored. I want these pics to store in my own folder. I don't know how to achieve that. I am very new to android. Below is the code where instead of default camera I am calling its own class.
public void onLaunchCamera(View view) {
// create Intent to take a picture and return control to the calling application
Intent intent = new Intent(this,CameraActivity.class);
// Start the image capture intent to take photo
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
And this is the onActivityResult method
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Uri takenPhotoUri = data.getData();
Bitmap takenImage = BitmapFactory.decodeFile(takenPhotoUri.getPath());
imageView.setImageBitmap(takenImage);
I thought about saving this bitmap into my own folder but I couldn't think how to delete the created directory of SquareCamera.
So I found the solution. I added the library as a module in my app. Referring (https://www.youtube.com/watch?v=1MyBO9z7ojk). And there I changed the source code a little bit and now it's working perfect.
I'm a bit long in the tooth at Android and am not 100% with the new Uri methods of file access enforced since KitKat. For conventional file access you can get a private writeable file using.
private static final File OUTPUT_DIR = Environment.getExternalStorageDirectory();
FileOutputStream fos;
void yourMethodBeginsHere() {
String outputPath = new File(OUTPUT_DIR, "test.png").toString();
try {
fos = new FileOutputStream(outputPath, false);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//Work with file
}
If you need a truly external file path please refer to the excellent answer already existing at https://stackoverflow.com/a/26765884/5353361 which deals fully with the new Uri based system of permissions and the integrated file explorer.
I wanto to load an image into an ImageView on android. Now, i know that if the image is too large, i have to scale it so that it doesn't take too much memory.
I want to do that doint what it says here: http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
Now, the way i get the image is via it's URI like this:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"text"),1);
protected void onActivityResult(int reqCode, int resCode, Intent data){
super.onActivityResult(reqCode, resCode, data);
if (reqCode == 1 && resCode == RESULT_OK && data != null) {
Uri selectedImage = data.getData();
}
}
And if i want to use some decode*() method, i cannot do that with the URI. So my frist choice was to use:
decodeFile(imageUri.getPath(),options);
But when i do that, the path i get from the gallery are something like: /documents/image:9023
How should i do this?
I just need to open the gallery and get and image from displaying and also be able to save it as a bitmap so i can edit it.
Even I was stuck with similar problem few days before,
1.) First convert your Uri to string
String SelectedImageString= selectedImage.toString();
2.)once you will get String path of image you can convert it to bitmap and also resize it using bitmapFactory options.
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
Bitmap bitmap;
bitmap=BitmapFactory.decodeFile(SelectedImageString, options);
imageView.setImageBitmap(bitmap);
It worked for me I hope it will help you.
And if i want to use some decode*() method, i cannot do that with the URI
Sure you can. Call openInputStream() on ContentResolver, and pass that InputStream to the decodeStream() method on BitmapFactory.
A Uri is not necessarily a file, so do not attempt to treat it as one.
I made a program where user show which picture he want to load in this app. When user want to load picture, he can choose what kind of file explorer he want to use:
Everything works, but if I choose "Gallery" and mark my image, it doesn't work. It happens only when i choose "Gallery".
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK){
String path = data.getData().getPath();
try {
loadedimage = BitmapFactory.decodeFile(sciezka_z_obrazem);
}
catch (Exception e) {
}
} [...]
After picking image from Gallery, loadedimage is null.
When I use Dropbox or "My Files" everything is OK.
I guess you should use
Uri mUri = data.getData();
In case of Gallery the file returned is of the form content:// rather than file://
So you should use the uri rather than doing getPath on uri
Edit:
Use uri to decode like below
InputStream is = getContentResolver().openInputStream(uri);
loadedimage = BitmapFactory.decodeStream(is)
You should try this.
File filePath= new File(path);
and then use
Uri.fromFile(filePath)
this would set the content wchich could be openend.