Setting alarm via Uri reference - android

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

Related

Android 7 (api 24) OnActivityResult from gallery, I can not intent to another class

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.

Trying to getIntExtra() on an intent in onActivityResult(), always null when using a Chooser?

I am wondering if using a Chooser means you can't send extra custom data to the receiving intent?
I call a file selection intent like this
public static void receiveFiles(final AppCompatActivity activity, int which) {
Intent receiveIntent = new Intent(Intent.ACTION_GET_CONTENT);
receiveIntent.setType("text/comma-separated-values");
String[] mimetypes = {"text/csv", "text/comma-separated-values", "application/csv"};
receiveIntent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
receiveIntent.addCategory(Intent.CATEGORY_OPENABLE);
receiveIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, false);
receiveIntent.putExtra(IMPORT_OPTION_WHICH, which); //this line
activity.startActivityForResult(Intent.createChooser(receiveIntent, "Pick CSV"), REQUEST_IMPORT_CSV);
}
But then in onActivityResult after the user selects a file:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
switch (requestCode) {
case REQUEST_IMPORT_CSV:
if (data != null) {
Uri uri = data.getData(); //is correct
int which = data.getIntExtra(IMPORT_OPTION_WHICH, -1);
//but this always -1!
}
break;
}
}
}
How can I get it to correctly extract the value I passed into the intent?

Upload files from phone intent

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){
}
}

Cancel active intent from activity

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);
}
}

camera activity does not return to the previous/caller activity

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();
}
}

Categories

Resources