android - How to set MP3 as Ringtone - android-fragments

File newSoundFile = new File("pathFile");
if (DebugUtil.DEBUG) {
DebugUtil.logError(new Exception(), newSoundFile.getAbsolutePath());
}
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, newSoundFile.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "Title");
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
values.put(MediaStore.MediaColumns.SIZE, newSoundFile.length());
values.put(MediaStore.Audio.Media.ARTIST, R.string.app_name);
values.put(MediaStore.Audio.Media.DURATION, 230);
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
values.put(MediaStore.Audio.Media.IS_ALARM, false);
values.put(MediaStore.Audio.Media.IS_MUSIC, false);
Uri uri = MediaStore.Audio.Media.getContentUriForPath(newSoundFile.getAbsolutePath());
context.getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + newSoundFile.getAbsolutePath() + "\"", null);
Uri newUri = context.getContentResolver().insert(uri, values);
try {
RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, newUri);
KitKatToast.makeText(context, R.string.msg_setAsRingTuneSuccess, KitKatToast.LENGTH_LONG).show();
}
catch (Exception e) {
if (DebugUtil.DEBUG) {
DebugUtil.logError(new Exception(), e.toString());
}
}

I saw many posts but anyone showed what i should actually have to do. So i decided to create this complete answer. The only think you actually need is a button.
Lets start.
Here is my MainActivity.java which i used
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
b2 = (Button) findViewById(R.id.button2);
b2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent1 = new Intent();
intent1.setAction(Intent.ACTION_GET_CONTENT);
intent1.setType("audio/*");
startActivityForResult(Intent.createChooser(intent1, "Choose Sound File"), 6);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK && requestCode == 6) {
Uri i = data.getData(); //getDATA
String s = = i.getPath(); //getPath
File k = new File(s); //set File from path
if (s != null) { //(file.exists
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "ring");
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
values.put(MediaStore.MediaColumns.SIZE, k.length());
values.put(MediaStore.Audio.Media.ARTIST, R.string.app_name);
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
values.put(MediaStore.Audio.Media.IS_ALARM, true);
values.put(MediaStore.Audio.Media.IS_MUSIC, false);
Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());
getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + k.getAbsolutePath() + "\"", null);
Uri newUri = getContentResolver().insert(uri, values);
try {
RingtoneManager.setActualDefaultRingtoneUri(MainActivity.this, RingtoneManager.TYPE_RINGTONE, newUri);
} catch (Throwable t) {
}
}
}
}
}
Lastly its really important to add those permisions in your AndroidManifest.xml for example if you dont add the permision to write external storage your app will crash like mine.. xD
What you need:
<uses-permission android:name="android.permission.WRITE_SETTINGS" ></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION" ></uses-permission>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" ></uses-permission>
You can try my app on Google Play : BackAtel Audio Manager
Hope that helps.... my problem is now solved!! i hope that i solved your problem too :))

Related

Is there a way to use the camera functions in Xamarin Forms without downloading any extra NuGet Packages?

I am currently building a project which allows the user to take a photo of something and use that photo. I was wondering if there were any other methods out there that does not require me to download any Plugins or NuGet Packages?
You need to create a ICameraPickerService in Xamarin Forms :
public interface IPhotoPickerService
{
Task<byte[]> GetImageStreamAsync();
}
In iOS , create the CameraPickerService :
[assembly: Dependency(typeof(CameraPickerService))]
namespace DependencyServiceDemos.iOS
{
public class CameraPickerService: ICameraPickerService
{
TaskCompletionSource<byte[]> taskCompletionSource;
UIImagePickerController imagePicker;
public Task<byte[]> GetImageStreamAsync()
{
// Create and define UIImagePickerController
imagePicker = new UIImagePickerController
{
SourceType = UIImagePickerControllerSourceType.Camera,
MediaTypes = UIImagePickerController.AvailableMediaTypes(UIImagePickerControllerSourceType.Camera)
};
// Set event handlers
imagePicker.FinishedPickingMedia += OnImagePickerFinishedPickingMedia;
imagePicker.Canceled += OnImagePickerCancelled;
// Present UIImagePickerController;
UIWindow window = UIApplication.SharedApplication.KeyWindow;
var viewController = window.RootViewController;
viewController.PresentModalViewController(imagePicker, true);
// Return Task object
taskCompletionSource = new TaskCompletionSource<byte[]>();
return taskCompletionSource.Task;
}
void OnImagePickerFinishedPickingMedia(object sender, UIImagePickerMediaPickedEventArgs args)
{
UIImage image = args.EditedImage ?? args.OriginalImage;
if (image != null)
{
// Convert UIImage to .NET Stream object
NSData data;
if (args.ReferenceUrl.PathExtension.Equals("PNG") || args.ReferenceUrl.PathExtension.Equals("png"))
{
data = image.AsPNG();
}
else
{
data = image.AsJPEG(1);
}
Stream stream = data.AsStream();
UnregisterEventHandlers();
// Set the Stream as the completion of the Task
taskCompletionSource.SetResult(data.ToArray());
}
else
{
UnregisterEventHandlers();
taskCompletionSource.SetResult(null);
}
imagePicker.DismissModalViewController(true);
}
void OnImagePickerCancelled(object sender, EventArgs args)
{
UnregisterEventHandlers();
taskCompletionSource.SetResult(null);
imagePicker.DismissModalViewController(true);
}
void UnregisterEventHandlers()
{
imagePicker.FinishedPickingMedia -= OnImagePickerFinishedPickingMedia;
imagePicker.Canceled -= OnImagePickerCancelled;
}
}
}
Not forgetting to add permission in Info.plist :
<key>NSCameraUsageDescription</key>
<string>Use Camera</string>
In addition , iOS need to run in a physical device.
In Android , create the CameraPickerService :
[assembly: Dependency(typeof(CameraPickerService))]
namespace DependencyServiceDemos.Droid
{
public class CameraPickerService : ICameraPickerService
{
public Task<byte[]> GetImageStreamAsync()
{
// Define the Intent for getting images
Intent getImageByCamera = new Intent("android.media.action.IMAGE_CAPTURE");
// Start the camera (resumes in MainActivity.cs)
MainActivity.Instance.StartActivityForResult(
getImageByCamera,
MainActivity.PickImageId);
// Save the TaskCompletionSource object as a MainActivity property
MainActivity.Instance.PickImageTaskCompletionSource = new TaskCompletionSource<byte[]>();
// Return Task object
return MainActivity.Instance.PickImageTaskCompletionSource.Task;
}
}
}
Adding permission in AndroidMainfest.xml :
<uses-permission android:name= "android.permission.CAMERA" />
<uses-permission android:name= "android.permission.WRITE_EXTERNAL_STORAGE" />
Get Image data in MainActivity :
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
internal static MainActivity Instance { get; private set; }
public int CAMERA_JAVA_REQUEST_CODE = 1;
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
Instance = this;
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
LoadApplication(new App());
DependencyService.Register<ITextToSpeechService, TextToSpeechService>();
}
// Field, property, and method for Picture Picker
public static readonly int PickImageId = 1000;
public TaskCompletionSource<byte[]> PickImageTaskCompletionSource { set; get; }
protected override void OnActivityResult(int requestCode, Result resultCode, Intent intent)
{
base.OnActivityResult(requestCode, resultCode, intent);
if (requestCode == PickImageId)
{
if ((resultCode == Result.Ok) && (intent != null))
{
Bundle bundle = intent.Extras;
Bitmap bitmap = (Bitmap)bundle.Get("data");
//// Set the Stream as the completion of the Task
MemoryStream memoryStream = new MemoryStream();
bitmap.Compress(Bitmap.CompressFormat.Jpeg, 50, memoryStream);
PickImageTaskCompletionSource.SetResult(memoryStream.ToArray());
}
else
{
PickImageTaskCompletionSource.SetResult(null);
}
}
}
}
Finally , show image in ContentPage of Forms :
async void OnPickPhotoButtonClicked(object sender, EventArgs e)
{
(sender as Button).IsEnabled = false;
byte[] data = await DependencyService.Get<IPhotoPickerService>().GetImageStreamAsync();
MemoryStream stream = new MemoryStream(data);
if (stream != null)
{
image.Source = ImageSource.FromStream(() => stream) ;
}
(sender as Button).IsEnabled = true;
}
The effect :
Note : If want to pick a Photo from the Picture Library, you can have a look at this official document .

How to make phone calls using Xamairn forms?

I'm new in xamarin and I want make a phone call directly (without opening the dialler). I tried with this example but it doesn't work.
Click Please help
public class PhoneCall_Droid : IPhoneCall
{
public void MakeQuickCall(string PhoneNumber)
{
try
{
var uri = Android.Net.Uri.Parse(string.Format("tel:{0}", PhoneNumber));
var intent = new Intent(Intent.ActionCall, uri);
Xamarin.Forms.Forms.Context.StartActivity(intent);
}
catch (Exception ex)
{
new AlertDialog.Builder(Android.App.Application.Context).SetPositiveButton("OK", (sender, args) =>
{
//User pressed OK
})
.SetMessage(ex.ToString())
.SetTitle("Android Exception")
.Show();
}
}
}
there are two error in your code above:
1.Xamarin.Forms.Forms.Context could not get the correct context.
you could defined a static variable in MainActiviy like :
public static MainActivity Instance;
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
Instance = this;
LoadApplication(new App());
}
you also could use the Current Activity Plugin,you could refer to Current Activity
2.After Android6.0 you should requests the runtime permissions and the official doucument
here is a simple example:
[assembly: Xamarin.Forms.Dependency(typeof(PhoneCall_Droid))]
namespace App18.Droid
{
class PhoneCall_Droid: IPhoneCall
{
public void MakeQuickCall(string PhoneNumber)
{
try
{
if(ActivityCompat.CheckSelfPermission(MainActivity.Instance, Android.Manifest.Permission.CallPhone) != Android.Content.PM.Permission.Granted ){
ActivityCompat.RequestPermissions(MainActivity.Instance, new string[] {Android.Manifest.Permission.CallPhone }, 1);
return;
}
else
{
var uri = Android.Net.Uri.Parse(string.Format("tel:{0}", PhoneNumber));
var intent = new Intent(Intent.ActionCall, uri);
MainActivity.Instance.StartActivity(intent);
}
}
catch (Exception ex)
{
new AlertDialog.Builder(MainActivity.Instance).SetPositiveButton("OK", (sender, args) =>
{
//User pressed OK
})
.SetMessage(ex.ToString())
.SetTitle("Android Exception")
.Show();
}
}
}
}
and you also could to use the nugetpackage Plugin.Permissions to request runtime permissions(Permission.Location)
refer to Plugin.Permissions
finally you could call like
DependencyService.Get<IPhoneCall>().MakeQuickCall(phonenumber);

IOSDrive becomes null and Appium TestNG tests restarts the app in between

I am facing issues with appium test groups. I have written multiple test cases for my app and running different sets at a time.
Basically, my test flow is: Login with Google => Input Password => Logout
Then for another set same flow with Login with Facebook.
Following is the code is written in AppiumTest.java file
#Test
public class AppiumTest {
IOSDriver<MobileElement> driver = null;
public DesiredCapabilities capabilitiesForDevice(String deviceCode) {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("platformName", "iOS");
capabilities.setCapability("udid", "SOME_VALID_UDID"); // 7+
capabilities.setCapability("platformVersion", "10.3.3");
capabilities.setCapability("app", "PACKAGE_PATH");
capabilities.setCapability("noReset", false);
capabilities.setCapability("showXcodeLog", true);
capabilities.setCapability("clearSystemFiles", false);
...
return capabilities;
}
#BeforeSuite(groups = {"google"}) // Removing suite also does not make any effect
public void setup() throws MalformedURLException {
System.out.print("Setting up driver.\n");
DesiredCapabilities capabilities = capabilitiesForDevice("iPhone5s");
String url = "http://0.0.0.0:4723/wd/hub";
driver = new IOSDriver<MobileElement>(new URL(url), capabilities);
}
#AfterSuite(groups = {"facebook"})
// Removing suite also does not make any effect
// Between 2 tests execution this method is not being called
public void tearDown() {
System.out.println("AfterSuite ... QUITTING DRIVER...");
driver.quit();
}
public MobileElement getElementByName(String name) {
try {
MobileElement theElement = (MobileElement) (new WebDriverWait(driver, 30))
.until(ExpectedConditions.visibilityOfElementLocated(By.name(name)));
return theElement;
} catch (Exception e) {
// e.printStackTrace();
}
return null;
}
public MobileElement getElementByXPath(String xpath) {
try {
MobileElement theElement = (MobileElement) (new WebDriverWait(driver, 30))
.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(xpath)));
return theElement;
} catch (Exception e) {
// e.printStackTrace();
}
return null;
}
#Test(groups = {"google"}, priority = 1)
public void loginWithGoogle() {
if (driver == null) {
printLog("Test driver is null.");
try {
setup();
} catch (MalformedURLException e) {
// e.printStackTrace();
}
}
printLog("driver = " + driver);
printLog("\nLoading is null and not logged in.." + System.currentTimeMillis());
String googleIcon = "icon google";
MobileElement gLoginElement = getElementByName(googleIcon);
gLoginElement.click();
printLog("\nGoogle clicked.." + System.currentTimeMillis());
...
// Input Credentials for Google auth or select user from the auth list
// Assert.assertEquals("Hi test found.", "Test", "Test1");
}
public void loginWithIncorrectPassword() {
String passwordEntry = "//XCUIElementTypeApplication[#name=\"eCare Vault\"]/XCUIElementTypeWindow[1]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeScrollView/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther[1]/XCUIElementTypeSecureTextField";
MobileElement pwdEntryElement = getElementByXPath(passwordEntry);
String passwordString = "12345";
pwdEntryElement.sendKeys(passwordString);
printLog("\n{Priority=>2} Password entered..." + passwordString + " # " + System.currentTimeMillis());
...
printLog("\nSend clicked..." + System.currentTimeMillis());
// Assert for incorrect pwd.
}
#Test(groups = {"google"}, priority = 12)
public void loginWithIncorrectPasswordGoogle() {
loginWithIncorrectPassword();
}
public void loginWithCorrectPassword() {
String passwordEntry = "//XCUIElementTypeApplication[#name=\"eCare Vault\"]/XCUIElementTypeWindow[1]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeScrollView/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther[1]/XCUIElementTypeSecureTextField";
MobileElement pwdEntryElement = getElementByXPath(passwordEntry);
String passwordString = "VALID PWD";
pwdEntryElement.sendKeys(passwordString);
printLog("\n{Priority=>6} Password entered..." + passwordString + " # " + System.currentTimeMillis());
// XCUIElementTypeButton[#name="Send"]
String sendKey = "Send";
MobileElement sendKeyElement = getElementByName(sendKey);
sendKeyElement.click();
printLog("\nSend clicked..." + System.currentTimeMillis());
}
#Test(groups = {"google"}, priority = 16)
public void loginWithCorrectPasswordGoogle() {
loginWithCorrectPassword();
}
public void logoutButtonClicked() {
// This method will logout user from the app and loads the main screen from where user will be able to tap the Google/Facebook icon
System.out.println("\nLogged out from ECV..." + System.currentTimeMillis());
}
#Test(groups = {"google"}, priority = 19)
public void logoutButtonClickedGoogle() {
logoutButtonClicked();
}
#Test(groups = {"facebook"}, priority = 20)
public void loginWithFB() {
System.out.println("\nLogin with Facebook..." + System.currentTimeMillis());
if (driver == null) {
printLog("Test driver is null.");
try {
setup();
} catch (MalformedURLException e) {
// e.printStackTrace();
}
}
printLog("driver = " + driver);
System.out.println("\nLoading is null and not logged in.." + System.currentTimeMillis());
String fbIcon = "icon facebook";
MobileElement fbLoginElement = getElementByName(fbIcon);
fbLoginElement.click();
System.out.println("\nFacebook clicked.." + System.currentTimeMillis());
...
// "Log In with the Facebook App" button exists for FB app login
// "Log In with the Facebook App" -> Tap
...
continueLoginElement.click();
System.out.println("\nKalis loggedIn.." + System.currentTimeMillis());
}
System.out.println(
"\n{Priority=>1} Password screen found.." + pwdScreen.getText() + " " + System.currentTimeMillis());
// Assert.assertEquals("Hi test found.", "Test", "Test1");
}
#Test(groups = {"facebook"}, priority = 22)
public void loginWithIncorrectPasswordFB() {
loginWithIncorrectPassword();
}
#Test(groups = {"facebook"}, priority = 26)
public void loginWithCorrectPasswordFB() {
loginWithCorrectPassword();
}
#Test(groups = {"facebook"}, priority = 28)
public void homePageLoadedFB() {
homePageLoaded();
}
#Test(groups = {"facebook"}, priority = 29)
public void logoutButtonClickedFB() {
logoutButtonClicked();
}
private void printLog(String message) {
System.out.println(message);
}
}
Testng.xml example
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name="TestGoogle">
<groups>
<run>
<include name="google" />
</run>
</groups>
<classes>
<class name="login.googleLogin.AppiumTest" />
</classes>
</test> <!-- Test -->
<test name="TestFB">
<groups>
<run>
<include name="facebook" />
</run>
</groups>
<classes>
<class name="login.googleLogin.AppiumTest" />
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
When I right click on testng.xml and run the test as TestNG Suite, the app gets restarted when the first test "TestGoogle" is completed and there after a 5-10 seconds restarts, the app continues running and executes pending test cases for "TestFacebook".
When I combine Facebook and Google, test cases for Google/Facebook are not executed.
<run>
<include name="google" />
<include name="facebook" />
</run>
Please give me hint of how to solve the issue.

Unable to save files in API 23 anymore

We have an application which saves images in a directory in an SD card. The directory is created if it does not already exist.
Here is the code that has done the job before the arrival of API 23:
OutputStream output;
File filepath = Environment.getExternalStorageDirectory();
File dir = new File(filepath.getAbsolutePath()
+ "/fingerprint/");
dir.mkdirs();
Bitmap bitmap = m_bitmap;
File file = new File(dir, userId.getText().toString() + ".jpg");
try {
output = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, output);
output.flush();
output.close();
} catch (Exception e) {
e.printStackTrace();
}
For some reasons, the directory is no longer created and files are no longer saved in Android 6.0. We introduced these methods to update our code with API changes in Marshmallow.
private void requestPermission(final Context context){
if(ActivityCompat.shouldShowRequestPermissionRationale((Activity)context, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
new AlertDialog.Builder(context)
.setMessage(context.getResources().getString(R.string.permission_storage))
.setPositiveButton(R.string.allow, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions((Activity) context,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
REQUEST_WRITE_EXTERNAL_STORAGE);
}
}).show();
} else {
ActivityCompat.requestPermissions((Activity)context,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
REQUEST_WRITE_EXTERNAL_STORAGE);
}
}
#Override
public void onRequestPermissionsResult(int requestCode,String permissions[], int[] grantResults) {
switch (requestCode) {
case REQUEST_WRITE_EXTERNAL_STORAGE: {
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(CaptureFingerprintActivity.this,
getResources().getString(R.string.permission_storage_success),
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(CaptureFingerprintActivity.this,
getResources().getString(R.string.permission_storage_failure),
Toast.LENGTH_SHORT).show();
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
return;
}
}
}
And the code that creates the directory and saves files is also updated.
OutputStream output;
File storageDir = null;
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
if (PackageManager.PERMISSION_GRANTED == ActivityCompat.checkSelfPermission(CaptureFingerprintActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
storageDir = new File(Environment.Environment.getExternalStorageDirectory()
, "fingerprint");
if (!storageDir.exists()) {
storageDir.mkdir();
} else {
requestPermission(CaptureFingerprintActivity.this);
}
}
Bitmap bitmap = m_bitmap;
File file = new File(storageDir, userId.getText().toString() + ".jpg");
try {
output = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, output);
output.flush();
output.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
This still doesn't seem to be creating directories.

No 'output' extra specified Exception

I use the Google Photo app to pick gallery photo and then when I crop the picture and save, it catches an exception, Here is my code :
goto_picture.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
IMAGE_UNSPECIFIED);
startActivityForResult(intent, PHOTO_ZOOM);
dialog.cancel();
}
});
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == PHOTO_GRAPH) {
startPhotoZoom(Uri.fromFile(file));
String imagePath = SystemUtils.getSDPath() + "/temp.jpg";
File picture = new File(imagePath);
if (picture.exists()) {
pictureBitmap = BitmapFactory.decodeFile(imagePath);
ImageUtils.SaveCacheBitmap(pictureBitmap);
rvEditAvatar.setImageBitmap(pictureBitmap);
}
}
if (requestCode == PHOTO_ZOOM) {
startPhotoZoom(data.getData());
}
if (requestCode == PHOTO_RESULT) {
Bundle extras = data.getExtras();
if (extras != null) {
pictureBitmap = extras.getParcelable("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
pictureBitmap.compress(Bitmap.CompressFormat.JPEG, 100,
stream);
ImageUtils.SaveCacheBitmap(pictureBitmap);
rvEditAvatar.setImageBitmap(pictureBitmap);
}
}
}
}
public void startPhotoZoom(Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 180);
intent.putExtra("outputY", 180);
intent.putExtra("return-data", true);
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
startActivityForResult(intent, PHOTO_RESULT);
}
Logcat:
Process: com.google.android.apps.photos, PID: 7031
java.lang.RuntimeException: Unable to resume activity
{com.google.android.apps.photos/com.google.android.apps.photos.photoeditor.intents.EditActivity}:
java.lang.UnsupportedOperationException: No 'output' extra specified
and can not save to specified inputUri:
content://com.google.android.apps.photos.contentprovider/0/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F72072/ACTUAL
As the exception said, you have to specify output extra like the following code.
intent.putExtra(MediaStore.EXTRA_OUTPUT, someOutPutPath);
And return data is not secure in case of big image cropped which may cause crash. I think that's why it forces you to use an output extra but not the data directly. So you may set the return-data to false as well:
intent.putExtra("return-data", false);
I meet this problem today, and solved by double check the data pass-back.I test following code on both Android L and Android 4. On Android L the fileUri is not empty while pre Android L we got fileUri null(in this case, I got the bitmap by simply getData).
private Bitmap decodeBitmapFromCrop(Intent data) {
Bundle extras = data.getExtras();
Bitmap photo = null;
if (extras != null) {
photo = extras.getParcelable("data");
} else {
Uri fileUri = data.getData();
if (fileUri != null) {
try {
photo = MediaStore.Images.Media.getBitmap(getContentResolver(), fileUri);
} catch (IOException e) {
XXLog.d(TAG, "Media.getBitmap", e);
}
}
}
return photo;
}

Resources