I have this method that opens the native camera.
public void takePhoto(){
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
Uri photoUri;
photoUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
startActivityForResult(cameraIntent, CAM_REQUREST);
}
It is working great, but I want to be able to return to my activity if the user forgets to, or if there is something my app needs attention for. Is there a way that I can do this from my activity?
startActivityForResult() will automatically return after the result is obtained, so after the photo is taken in this case. If you want to relaunch your app at any point, you should use startActivity() with your own package in the intent.
Do it like this:
PackageManager p = getPackageManager();
String myPackage = getApplicationContext().getPackageName();
Intent intent = p.getLaunchIntentForPackage(myPackage);
startActivity(intent);
Override onActivityResult in your activity
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
Log.e(TAG, "--onActivityResult MainActivity--");
if (requestCode == CAM_REQUREST && resultCode == RESULT_OK) {
Uri selectedImage = intent.getData();
Log.e(TAG, "Your image :" + selectedImage);
}
}
Related
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
startActivity(intent);
1.How can I get an URI of music file using code above?
2.If I have Uri already, how set it as ringtone or alarm?
Also tried use:
Convert a file path to Uri in Android
To get a return result from an activity, use startActivityForResult instead of startActivity.
private int REQUEST_ACTION_PICK = 1;
...
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
// startActivity(intent);
startActivityForResult(intent, REQUEST_ACTION_PICK); //fixme
// ...
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_ACTION_PICK && resultCode == RESULT_OK) {
Uri uri = data.getData();
Log.d(TAG, uri.toString());
}
}
As for 'how set it as ringtone or alarm?', perhaps this might work?
Set default alarm sound programatically Android
I know with Android 7 you can't pass File// but in this case I'm not doing that. For testing purposes, when a user selects a file, I just want to intent to another class. When I click a video, the app just stops responding so there isn't a crash log or anything. This is my onActivityResult method:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_TAKE_GALLERY_VIDEO) {
Uri selectedImageUri = data.getData();
// OI FILE Manager
String filemanagerstring = selectedImageUri.getPath();
// MEDIA GALLERY
String selectedImagePath = getPath(selectedImageUri);
if (selectedImagePath != null) {
Log.d(TAG, "THE PATHHHHHHHHH " + selectedImagePath);
Intent intent = new Intent(getActivity().getApplicationContext(),
GalleryUpload.class);
// intent.putExtra("path", selectedImagePath);
startActivity(intent);
}
}
}
}
Replace all of that with:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_TAKE_GALLERY_VIDEO) {
Uri selectedImageUri = data.getData();
Intent intent=new Intent(getActivity(), GalleryUpload.class)
.setData(selectedImageUri)
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
}
}
}
This correctly handles both file and content Uri schemes, including passing along read access to GalleryUpload.
GalleryUpload can then use getIntent().getData() to retrieve the Uri, then use ContentResolver and openInputStream() to get an InputStream on the content identified by the Uri.
I've an android application where in an activity the user chooses a picture from the phone's memory to upload it to the server using the Action Pick intent
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
However; now the requirements has changed and the client wants to upload any file with any extension, what intent should I use in order to open the phone's memory (like the file manager) and choose whatever I want?
Now I'm using the Get content intent thanks to you guys:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
startActivityForResult(intent, 1);
but the problem is in the start activity for result
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
if (requestCode == 1) {
Log.d("select pivture", "passed her");
Uri selectedMediaUri = data.getData();
String Fpath = selectedMediaUri.getPath();
Log.d("Fpath", Fpath);
}
I get as follows:
/document/primary:Download/tool208.pdf
Which is not what I want, I want path like this one:
/external/images/media/6634
Use below method:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
startActivityForResult(intent, PICKFILE_REQUEST_CODE);
UPDATE
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==PICKFILE_REQUEST_CODE){
}
}
I'm trying to retrieve a file from my ACTION_IMAGE_CAPTURE activity, but when I use
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
I get a null intent on my activity result. Here is the full code.
The intent
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(cameraIntent, 1888);
My onActivityResult
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == 1888 && resultCode == RESULT_OK)
{
Uri uri = (Uri)data.getData();
Intent i = new Intent(getApplicationContext(), Camara.class);
i.putExtra("uri", uri);
startActivity(i);
}
}
i'm using the following permissions
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
i need to access to the file of the image captured but i'm getting null on my parameter data
and then, How do I get the file? Like this?
File myfile = new File(uri.getPath());
I also tried this:
Uri uri = (Uri)data.getData();
Intent i = new Intent(getApplicationContext(), Camara.class);
i.putExtra("uri", uri);
startActivity(i);
In this case I can get the bitmap with this
Bitmap lafoto = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
but i can't get the File
I tried to follow this solution but didn't work for me Get Path of image from ACTION_IMAGE_CAPTURE Intent
The problem was that when you give a extra_output, the file is written in the URI provided so you just access via that URI to use the image captured. Because that, no data is provided on the onActivityResult, because the camera app will only return a mini-bitmap as a Intent extra in the onActivityResult when no extra_output is provided in the camera app intent.
I had the same problem and changed my code flow like below;
capturePhoto.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, TAKE_PHOTO);
}
});
then
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode){
case TAKE_PHOTO:
if (resultCode == Activity.RESULT_OK) {
final Bitmap bitmap = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(bitmap);
}
break;
}
}
I read an text input from the user, this input i use it as a name of a picture i'm taking by the camera.
i store this name and the path of the image name into Sqlite Database.
what I'm trying to do is, after clicking OK to accept the taken picture, i want the saved path to be displayed in a toast.
the problem is, when I click OK to accept the picture, nothing is being displayed and i cant switch from the camera activity to the activity that called the camera activity"the previous activity"
Note: I'm using the emulator not a real device.
OnClickListener btn_TakePictureListener = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String imgPath = retrievePath();
intent.putExtra(MediaStore.EXTRA_OUTPUT, getImageUri(imgPath));
startActivityForResult(intent, RequestCode);
}
};
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RequestCode && resultCode == RESULT_OK) {
String s = data.getData().toString();
Toast.makeText(getBaseContext(), ""+s, Toast.LENGTH_SHORT).show();
}
}
if you are passing Uri for you image then you can retrieve image as taken by camera:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(
Environment.getExternalStorageDirectory(), "temp.jpg")));
startActivityForResult(intent, 1);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == NONE)
return;
if (requestCode == 1) {
// Set the file save path with directory
File picture = new File(Environment.getExternalStorageDirectory()
+ "/temp.jpg");
Uri imageuri= Uri.fromFile(picture);
//set to imageview here
}
}
EDIT:
For Getting data uri in onActivityResult start Camra Activiyt as:
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,IMAGE_UNSPECIFIED);
startActivityForResult(intent, 2);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == NONE)
return;
if (requestCode == 1) {
Uri uriimg = data.getData();
Toast.makeText(getBaseContext(), ""+uriimg.toString(), Toast.LENGTH_SHORT).show();
}
}