Expected Parcelable for result for camera Intent - java

I am making a camera intent and storing the snapshot using the activity result, this is my code:
File imageFolder=new File(context.getExternalCacheDir(),"Cam/" + form);
imageFolder.mkdirs();
String random= UUID.randomUUID().toString();
String filename = random + ".jpg";
TakenImage = imageFolder + "/" + filename;
Intent camera=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
camera.putExtra(MediaStore.EXTRA_OUTPUT,TakenImage);
activityResultCamera.launch(camera);
But I get this error on the last line:
Key output expected Parcelable but value was a java.lang.String. The default value was returned.
Attempt to cast generated internal exception:
java.lang.ClassCastException: java.lang.String cannot be cast to android.os.Parcelable
Camera is an Intent, I also declared ActivityResultLauncher as Intent
ActivityResultLauncher<Intent> activityResultCamera = registerForActivityResult(...
So, what I am doing wrong?

EXTRA_OUTPUT needs to be:
A Uri...
With a scheme of content
You have:
A String...
That is a filesystem path that other apps cannot access on Android 10+
Use FileProvider to get a Uri that points to your desired location, and use that Uri in EXTRA_OUTPUT.

Related

Espresso image picker - CursorIndexOutOfBoundsException error

I'm developing a test with Espresso to test the profile image change function. I added the following lines in the #Before method in the test.
I create an intent with the image Uri, with my file provider, to return ever that my app goes to the gallery to pick an image.
Intent resultData = new Intent();
String filename = "img1.jpg";
String path = "mnt/sdcard/" + filename;
File f = new File(path);
Context context =InstrumentationRegistry.getInstrumentation().getContext();
Uri contentUri = getUriForFile(context, "com.otsuka.ikigai.fileprovider", f);
resultData.setData(contentUri);
Instrumentation.ActivityResult result = new Instrumentation.ActivityResult(Activity.RESULT_OK,resultData);
intending(not(isInternal())).respondWith(result);
The code of the activity that changes user image, calls the following method when it receives the intent,(I must not change it).
mProfileImage = CommonBitmapUtils.rotate(this, data.getData());
profileEdited = true;
imgUserPhoto.setImageBitmap(mProfileImage);
And I'm getting the following error:
android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
Caused by this line in the function rotate of the CommonBitmapUtils class:
path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
The cursor has 0 rows don't know why.
Solved it by setting the following path. Without file providers and permissions.
I got the path by debugging the app without testing, and copying the value.
resultData.setData(Uri.parse("content://media/external/images/media/142583"));

convert parse.com file uri to string Android

i have a file that i got back from parse.com and i am trying to get the string stored in that file to a textView. At the moment all i am getting is a url in my text view and not the string i need.
getting the parse file:
ParseFile file = message.getParseFile("file");
Uri fileUri = Uri.parse(file.getUrl());
//passing it to another class
intent.setData(fileUri);
getting the file to display in textview
mDisplay = (TextView)findViewById(R.id.messageDisplay);
Uri textUri = getIntent().getData();
// i have tried this also [ File string = new File(textUri.toString()); ]
String filePath = textUri.getEncodedPath();
mDisplay.setText(filePath);
i have tried:-
1] to get the string bytes before passing string via intent
2] getting url and then converting to string
and 1 or 2 more methods. all are displaying the url and not the string, i feel the issue may lie in this line
Uri fileUri = Uri.parse(file.getUrl());
but i am not sure.

android Uri to Java URI

I need a quick help on how to convert android Uri to Java URI. My requirement is to capture images and store them in External storage. To pass these images across activities, I decided to use an Arraylist that holds Uris of images and pass on this arraylist as an intent-extra to next activity. But, Arraylist accepts only JavaURI.
String image = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date());
File photo=new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),image);
selectedImageA=Uri.fromFile(photo);
imageIndex++;
selectedImageJ= <Looking for code here>;
imagesUriArray.add(imageIndex,selectedImageJ);
Intent i = new Intent(getApplicationContext(),ScanActivity.class);
startActivity(i);
Try to declare:
ArrayList<Uri> imagesUriArray = new ArrayList<Uri>;
If this doesn't work, declare your ArrayList as String list and convert the uri to String. And later, initialize the uri from the Uri string

Android set ringtone failure

I try the following code and it doesn't set the ringtone. The logcat entry for "ff" says null so I guess the URI isnt being concatenated properly?, I cant seem to figure out where in my code I am going wrong.
String filepath =Environment.getExternalStorageDirectory().toString()+"//media//audio//ringtones//bluemoon.mp3";
File ringtoneFile = new File(filepath);
ContentValues content = new ContentValues();
content.put(MediaStore.MediaColumns.DATA,ringtoneFile.getAbsolutePath());
content.put(MediaStore.MediaColumns.TITLE, "test");
content.put(MediaStore.MediaColumns.SIZE, 215454);
content.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mpeg");
content.put(MediaStore.Audio.Media.ARTIST, "artist");
content.put(MediaStore.Audio.Media.DURATION, 230);
content.put(MediaStore.Audio.Media.IS_RINGTONE, true);
content.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
content.put(MediaStore.Audio.Media.IS_ALARM, false);
content.put(MediaStore.Audio.Media.IS_MUSIC, false);
Log.i("BOOM", "the absolute path of the file is :"+ringtoneFile.getAbsolutePath());
Uri uri = MediaStore.Audio.Media.getContentUriForPath(
ringtoneFile.getAbsolutePath());
Uri newUri = getApplicationContext().getContentResolver().insert(uri, content);
Uri ringtoneUri = newUri;
Log.i("ff","the ringtone uri is :"+ringtoneUri);
RingtoneManager.setActualDefaultRingtoneUri(getApplicationContext(),
RingtoneManager.TYPE_RINGTONE,newUri);
Its possible data is not accurate for the file and its causing a problem. You may need to change:
audio/mpeg to audio/mp3
and duration to the actual duration of the file.
Another thing you may want to fix is your ringtone file declaration statement, try changing it to match this:
File k = new File(path, "mysong.mp3");
Also, I have a feeling your path is broken too..
Environment.getExternalStorageDirectory().toString()+"//media//audio//ringtones//bluemoon.mp3"
why with the two "//"?
It should be (for example):
../sdcard/media/ringtone

Android how to respond to ACTION_VIEW/ ACTION_SEND?

I just cannot seem to figure this one out: how do I respond to the ACTION_VIEW and ACTION_SEND intents? I have them in my Manifest file (and they appear in the drop down list of apps). What I need to do is respond to these intents and retrieve a bitmap of the corresponding image.
Right now here is what works:
Uri uri = (Uri) extras.getParcelable(Intent.EXTRA_STREAM);
Bitmap mBitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));
Then I take that uri and fetch a bitmap. However, if I respond when the email app downloads and image getExtras() is null and I get an error.
Basically I need to know what to put in here to fill a variable, mBitmap:
if (Intent.ACTION_SEND.equals(action) || Intent.ACTION_VIEW.equals(action))
{
Uri uri = (Uri) extras.getParcelable(Intent.EXTRA_STREAM);
Bitmap mBitmap = (getExtras() == null) ? what goes here :
BitmapFactory.decodeStream(cr.openInputStream(uri))
}
getIntent().getData() will contain the Uri for which the Intent is targeted.

Categories