Here is what I am passing. pictureFile is a File
Intent intent = new Intent (context, ShowPicActivity.class);
intent.putExtra("picture", pictureFile);
In the next activity which one of the getters do I use to get it?
Intent intent = getIntent(); .....?
File implements serializable ( first thing to check to send an object through an intent. )
( source )
so you can do it, just cast the resulting object to File like this :
File pictureFile = (File)getIntent.getExtras().get("picture");
It should be fine.
(it use the getter for 'object' which needs a serializable object and return it. The cast should be enough.)
Try the following:
YourPictureClass picture = (YourPictureClass)getIntent().getExtras().get("picture");
By calling getExtras() in your Intent you get an instance of Bundle.
With the normal get() in class Bundle you can read every Object you pass to the calling Intent. The only thing you have to do is to parse it back to the class your object is an instance of.
You can send it like this:
intent.putExtra("MY_FILE", myFile);
and retrieve it like this:
File myFile = (File)intent.getSerializableExtra("MY_FILE");
However, it may be better (anyone?) to send the file as a string reference, in which case you could send as:
intent.putExtra("MY_FILE_STRING", myFile.toString());
and retrieve/reconstruct like this:
File myFile = new File(intent.getStringExtra("MY_FILE_STRING"));
Related
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.
HI can anyone please help me i am trying to share text with multiple image but i am getting this error Key android.intent.extra.TEXT expected ArrayList but value was a java.lang.String. The default value was returned.
Here is my code-
String text = "Share text.";
Uri pictureUri = getLocalBitmapUri(shareImg_imvw);
uriList.clear();
for(int i=0;i<5;i++)
{
uriList.add(pictureUri);
}
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
shareIntent.setType("*/*");
// shareIntent.putExtra(Intent.EXTRA_TEXT, text);
// new code
ArrayList<String> extra_text = new ArrayList<String>();
extra_text.add(text);
shareIntent.putStringArrayListExtra(Intent.EXTRA_TEXT, extra_text);
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uriList);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, getString(R.string.send_intent_title)));
First, ACTION_SEND and ACTION_SEND_MULTIPLE support either EXTRA_TEXT or EXTRA_STREAM. Apps do not have to support both. Do not expect both to be used by all apps.
Second, ACTION_SEND_MULTIPLE requires that EXTRA_TEXT and EXTRA_STREAM be ArrayList extras. Replace putExtra() with putStringArrayListExtra(), passing in an ArrayList<String> of the multiple strings that you want to share.
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
I am new to java.
I have a directory with a txt file (R.raw). I want to get access with a command
InputStream in_s = res.openRawResource(R.raw.itemname);
where itemname is a dynamic string with a filename from a previous activity.
How do I can get open file in R.raw by a string "n0.txt"?
In javascript I can implement it like R.raw["n0.txt"] or R.raw.itemname.
Thanks in advance.
You need to use method getIdentifier().
Context context;
Resources res;
int itemId = res.getIdentifier("itemname", "raw", context.getPackageName());
InputStream is = res.openRawResource(itemId);
Original post here.
I have a little bit strange question: this time everything works, but I can't understand why.
AFAIK it's possible to mount more than one sd card. Everything will be mounted to /mnt directory. (is it true?)
On my device there is only one sd card which mounted to /mnt/sdcard. And in my application I open files from it. I'm using next code:
private void open() {
// get file extension
String extension = "";
int dotIndex = downloadedFile.lastIndexOf('.');
if (dotIndex != -1) {
extension = downloadedFile.substring(dotIndex + 1, downloadedFile.length());
}
// create an intent
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
Uri data = Uri.fromFile(new File(downloadedFile));
String type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
if (type == null || type.length() == 0) {
// if there is no acceptable mime type
type = "application/octet-stream";
}
intent.setDataAndType(data, type);
// get the list of the activities which can open the file
List resolvers = context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
if (resolvers.isEmpty()) {
(new AlertDialog.Builder(context)
.setMessage(R.string.AttachmentUnknownFileType)
.setNeutralButton(R.string.NeutralButtonText, null)
.create()).show();
} else {
context.startActivity(intent);
}
}
Actually downloadedFile variable has value like file:///sdcard/mydir/myfile.txt. But the code works. Why? How Android understand what /sdcard/... is the same as /mnt/sdcard/...?
And main question: what happened if sd card will be mounted to other dir (for exmaple, /mnt/another-sd/ or even /media/sd)? What if more than one sd cards will be mounted: how android understand what card to use?
Thank you for any help! Have a good day!
It's simple android configueres the mounting over a settings file on phone boot so if there are mor sdcards Android will simply prefer one of them to set as
/sdcard/
so when the mount settings change your code is simply useless you can only hope the settings being untouched .
Every company that procuces Android smartphones use the "sdcard" path even custom roms like use it