Title for File Chooser - java

I have a task where the user has to pick two files. I do not like the option to display a dialog before hands, saying Please pick the first file now, and then the second!", so I would like to display a title over the file chooser.
This is my (example) code:
public class MainActivity extends AppCompatActivity {
private static final int FILE_CHOOSE_RESULT_FIRST = 1, FILE_CHOOSE_RESULT_SECOND = 2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chooseFirstFile();
}
private void chooseFirstFile() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
Intent finalIntent = Intent.createChooser(intent, "Select first file");
startActivityForResult(finalIntent, FILE_CHOOSE_RESULT_FIRST);
}
private void chooseSecondFile() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
Intent finalIntent = Intent.createChooser(intent, "Select second file");
startActivityForResult(finalIntent, FILE_CHOOSE_RESULT_SECOND);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == FILE_CHOOSE_RESULT_FIRST && resultCode == RESULT_OK) {
Log.v("TEST", "First file is " + data.toString());
chooseSecondFile();
} else if (requestCode == FILE_CHOOSE_RESULT_SECOND && resultCode == RESULT_OK) {
Log.v("TEST", "Second file is " + data.toString());
}
}
}
As you can see, I add a title to the file chooser ("Select first file" and "Select the second file"), but it doesn't get shown:
How can I actually show the file chooser's title?
I've seen the question this one can be seen as a duplicate of, but I did not understand it to answer it, because it asks about the gravity and style of the title.

There is no way to do that. File Picker is based on the ResolverActivity. As evident from this post: How to change the appearance of the file picker title on Android?, there is this info:
createChooser creates an intent with an ACTION_CHOOSER action.
When you launch your intent, a ChooserActivity is created (not sure how exactly) which extends ResolverActivity. The layout applied to ResolverActivity is resolver_list or resolver_list_with_default. In there you can find the title in question.
There is no way to do that as you can't change the title, which is you could say "read-only". But as the answer from the other question also tells, you can create your custom file chooser based on the original source code. Or using a Document Provider create a custom navigator.

Related

How to upload and save an image from gallery. startActivityForResult deprecated

I'm currently learning how to use android studio in java and am trying to make a social media app. I am currently creating an edit profile page where the user would update their details and upload a profile picture.
I have been following tutorials online and all of the ones I have come across use the startActivityForResult method. It has been crossed out and wont call the method as it is deprecated. But I don't know what to use instead.
`ProfileImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//open gallery
Intent OpenGalleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(OpenGalleryIntent, 1000);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #androidx.annotation.Nullable Intent data){
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1000){
if(resultCode == Activity.RESULT_OK){
Uri imageUri = data.getData();
ProfileImage.setImageURI(imageUri);
UploadImageToFirebase(imageUri);
}
}
}
private void UploadImageToFirebase(Uri image){
StorageReference fileRef = storageReference.child("Profile.jpg");
fileRef.putFile(image).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(Edit_Profile.this, "Image Uploaded", Toast.LENGTH_SHORT).show();
}
});![enter image description here](https://i.stack.imgur.com/padRc.jpg)`
I know there is an alternative but I don't understand how it works.
startActivityForResult is indeed deprecated in later versions of AndroidX Activity and Fragment APIs (while I believe you can still use it despite of warning). New way to get result from activity is registerForActivityResult.
In your code you would need to create a launcher, which will handle result (selected image)
private final ActivityResultLauncher<Intent> launcher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
result -> {
if (result.getResultCode() == Activity.RESULT_OK
&& result.getData() != null) {
Uri photoUri = result.getData().getData();
//use photoUri here
}
}
);
and then launch this launcher in onClickListener
profileImage.setOnClickListener(view -> {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
launcher.launch(intent);
});
Yes startActivityForeResult is deprecated.
Now you can use ActivityResultLauncher for the callbacks
https://developer.android.com/training/basics/intents/result#java

Get real path of a file from URI? [duplicate]

I am trying to fetch a file this way:
final Intent chooseFileIntent = new Intent(Intent.ACTION_GET_CONTENT);
String[] mimetypes = {"application/pdf"};
chooseFileIntent.setType("*/*");
chooseFileIntent.addCategory(Intent.CATEGORY_OPENABLE);
if (chooseFileIntent.resolveActivity(activity
.getApplicationContext().getPackageManager()) != null) {
chooseFileIntent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
activity.startActivityForResult(chooseFileIntent, Uploader.PDF);
}
Then in onActivityResult :
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
According to many threads I'm supposed to fetch the file name from the intent with data.getData().getPath(), the file name I'm expecting is my_file.pdf, but instead I'm getting this :
/document/acc=1;doc=28
So what to do? Thanks for your help.
I am trying to fetch a file
Not with that code. That code is asking the user to pick a piece of content. This may or may not be a file.
According to many threads I'm supposed to fetch the file name from the intent with data.getData().getPath()
That was never correct, though it tended to work on older versions of Android.
So what to do?
Well, that depends.
If you wish to only accept files, integrate a file chooser library instead of using ACTION_GET_CONTENT. (UPDATE 2019-04-06: since Android Q is banning most filesystem access, this solution is no longer practical)
If you are willing to allow the user to pick a piece of content using ACTION_GET_CONTENT, please understand that it does not have to be a file and it does not have to have something that resembles a filename. The closest that you will get:
If getScheme() of the Uri returns file, your original algorithm will work
If getScheme() of the Uri returns content, use DocumentFile.fromSingleUri() to create a DocumentFile, then call getName() on that DocumentFile — this should return a "display name" which should be recognizable to the user
To get the real name and to avoid getting a name that looks like "image: 4431" or even just a number, you can write code as recommended by CommonsWare.
The following is an example of a code that selects a single pdf file, prints its name and path to the log, and then sends the file by email using its uri.
private static final int FILEPICKER_RESULT_CODE = 1;
private static final int SEND_EMAIL_RESULT_CODE = 2;
private Uri fileUri;
private void chooseFile() {
Intent fileChooser = new Intent(Intent.ACTION_GET_CONTENT);
fileChooser.setType("application/pdf");
startActivityForResult(Intent.createChooser(fileChooser, "Choose one pdf file"), FILEPICKER_RESULT_CODE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == FILEPICKER_RESULT_CODE) {
if (resultCode == RESULT_OK) {
fileUri = data != null ? data.getData() : null;
if (fileUri != null) {
DocumentFile d = DocumentFile.fromSingleUri(this, fileUri);
if (d != null) {
Log.d("TAG", "file name: " + d.getName());
Log.d("TAG", "file path: " + d.getUri().getPath());
sendEmail(fileUri);
}
}
}
}
}
private void sendEmail(Uri path) {
String email = "example#gmail.com";
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setType("application/octet-stream");
intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "PDF file");
String[] to = { email };
intent.putExtra(Intent.EXTRA_EMAIL, to);
intent.putExtra(Intent.EXTRA_TEXT, "This is the pdf file...");
intent.putExtra(Intent.EXTRA_STREAM, path);
startActivityForResult(Intent.createChooser(intent, "Send mail..."), SEND_EMAIL_RESULT_CODE);
}
hope it helps.

How to limit multiple image selection from the gallery?

I have implemented adding multiple image selection from the gallery in my project. However, I want to limit the user so he/she can select only 3 images from the gallery.
I have implemented selecting multiple images from the gallery like this:
`Intent intent = new Intent();
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), 1);`
How can I achieve this?
Thanks.
You can get the count of ClipData when selecting multiple images from and gallery and if that count is greater than 3 you can notify the user about it.
You can do something like this after selecting images from gallery:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == RESULT_OK || resultCode != RESULT_CANCELED){
ClipData clipData = data.getClipData();
if(clipData.getItemCount() > 3){
//notify user here...
}
}
}
This is not possible.
https://developer.android.com/reference/android/content/Intent.html#EXTRA_ALLOW_MULTIPLE
You'll have to manually check the returned data to see if it's more than 10 items, and if so, show a Toast
put this in your build.gradle(app)
compile 'com.github.esafirm.android-image-picker:imagepicker:1.5.0'
compile 'com.github.esafirm.android-image-picker:rximagepicker:1.5.0'
and this in your activity
private void pickImage() {
ImagePicker.create(UploadPhotosActivity.this)
.showCamera(false)
.limit(3)
.imageTitle(getString(R.string.select_image))
.folderTitle(getString(R.string.folder))
.theme(R.style.ImagePickerTheme)
.start(RC_CODE_PICKER);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_CODE_PICKER) {
Log.d("===uploadPhoto", "gallery : " + data);
imagesList = (ArrayList<Image>) ImagePicker.getImages(data);
Intent intent = new Intent(UploadPhotosActivity.this, ImageCropperActivity.class);
intent.putExtra(ImageCropperActivity.EXTRA_VIEW_PORT_RATIO, imagesList);
startActivity(intent);
}
} else {
Intent returnIntent = new Intent();
setResult(Activity.RESULT_CANCELED, returnIntent);
finish();
}
}

Android how to display image in Arraylist which intent from another activity?

I have a MultiplePhotoSelectActivity.java which let user select multiple photo and store the path in an ArrayList.
public void btnChoosePhotosClick(View v){
ArrayList<String> selectedItems = imageAdapter.getCheckedItems();
if (selectedItems!= null && selectedItems.size() > 0) {
//Toast.makeText(MultiPhotoSelectActivity.this, "Total photos selected: " + selectedItems.size(), Toast.LENGTH_SHORT).show();
Log.d(MultiPhotoSelectActivity.class.getSimpleName(), "Selected Items: " + selectedItems.toString());
Intent intent = new Intent(MultiPhotoSelectActivity.this,PreuploadActivity.class);
intent.putStringArrayListExtra("selectedItems", selectedItems);
setResult(RESULT_OK, intent);
startActivity(intent);
}
}
This is ArrayList<String> selectedItems come from imageAdapter
ArrayList<String> getCheckedItems() {
ArrayList<String> mTempArry = new ArrayList<>();
for(int i=0;i<mImagesList.size();i++) {
if(mSparseBooleanArray.get(i)) {
mTempArry.add(mImagesList.get(i));
}
}
return mTempArry;
}
After user choose the photo,the following result will appear in logcat
D/MultiPhotoSelectActivity: Selected Items: [/storage/emulated/0/Pictures/Screenshot_1486795867.png, /storage/emulated/0/Pictures/15592639_1339693736081458_1539667284_n.jpg, /storage/emulated/0/15592639_1339693736081458_1539667284_n.jpg]
The problem now is,I want to display the image in my another activity using the file path in the array list,after the user choose the image
Here is PreuploadActivity.java that should be receive the intent data.
This is the button to let user choose photo in MultiplePhotoSelectActivity.java
//this button will open gallery,and select photo
addPhoto.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(PreuploadActivity.this,MultiPhotoSelectActivity.class);
startActivityForResult(intent,PICK_IMAGE_REQUEST);
}
});
This is the onActivityResult() which should receive the Intent data from MultiplePhotoSelectActivity.java
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data.getData() !=null){
ArrayList<String> selectedItems = data.getStringArrayListExtra("selectedItems");
for(String selectedItem : selectedItems){
Uri filePath = Uri.parse(selectedItem);
try{
// bimatp factory
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
bitmap = BitmapFactory.decodeFile(filePath.getPath(),
options);
//Setting image to ImageView
ImageView imageView = new ImageView(getApplicationContext());
LinearLayout.LayoutParams layoutParams =
new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
imageView.setLayoutParams(layoutParams);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setPadding(0, 0, 0, 10);
imageView.setAdjustViewBounds(true);
imageView.setImageBitmap(bitmap);
linearMain.addView(imageView);
}catch (Exception e) {
e.printStackTrace();
}
}
So now in onActivityResult() of PreuploadActivity.java I cant display back the image in the ArrayList which sent from MultiplePhotoSelectActivity.java.I suspect is something wrong when putExtra in the intent,what I tried so far but still no different.
The answer of this Stackoverflow question
putParcelable or putSerializable like the answer
How to transfer a Uri image from one activity to another?
So what I need to know,
1) How should I putExtra and getExtra in the intent in both Activity in order send and receive the ArrayList of the image?
2) Is my handle to display the image correct?If no,please tell me what I doing wrong.
EDIT:Try for Aslam Hossin solution
After I tried this
ArrayList<String> myList = (ArrayList<String>) getIntent().getSerializableExtra("selectedItems ");
I got the following error
After looking at some documentation I figure out I make a few mistake
MultiPhotoSelectActivity.java
Intent intent = new Intent(MultiPhotoSelectActivity.this,PreuploadActivity.class);
intent.putStringArrayListExtra("selectedItems", selectedItems);
setResult(RESULT_OK, intent);
startActivity(intent);
I figure out,there are 3 mistake at above code,
1)In MultiPhotoSelectActivity.java should not a new intent,but it should be send the data back to PreuploadActivity.java
2) I should setResult like this
setResult(Activity.RESULT_OK, data);
3) According to the documentation as below ,so I add finish() after setResult()
Data is only returned once you call finish(). You need to call setResult() before calling finish(), otherwise, no result will be returned.
I solve it by setting result code in PreuploadActivity.java like below
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == PICK_IMAGE_REQUEST && resultCode == Activity.RESULT_OK ){
//setting Activity.RESULT_OK
ArrayList<String> selectedItems = data.getStringArrayListExtra("selectedItems");
This is MultiPhotoSelectActivity.java I do the following changes
ArrayList<String> selectedItems = imageAdapter.getCheckedItems();
if (selectedItems!= null && selectedItems.size() > 0) {
//Toast.makeText(MultiPhotoSelectActivity.this, "Total photos selected: " + selectedItems.size(), Toast.LENGTH_SHORT).show();
Log.d(MultiPhotoSelectActivity.class.getSimpleName(), "Selected Items: " + selectedItems.toString());
final Intent data = new Intent();
data.putStringArrayListExtra("selectedItems", selectedItems);
setResult(Activity.RESULT_OK, data);
finish();
}
}

Refresh text EditText after finish()

I created a 'New File' activity using
startActivityForResult(new Intent(MainActivity.this, NewFile.class),1);
The NewFile activity lets users set certain options for their text file then after clicking a button a string is saved to a static variable in my StringBuilder class and finish(); is called. How can I load this new string into the MainActivity's EditText? onCreate() is only called when the activity is first created right?
Do it on onResume or onActivityResult. It would be ideal though onActivityResult since you've used startActivityForResult, before finishing the other activity you set the setResult(int resultCode, Intent data) if you have intent to sent back or if none setResult(int resultCode). I think it is better to put the string which will be used to update your EditText as extra in the intent, then set the text using that string in onActivityResult method.
#Override
protected void onResume() {
super.onResume();
et.setText(DocumentBuilder.docText);
}
in your class NewFile.java :
String strName = "toto";
Intent intent = new Intent();
intent.putExtra("name", "toto");
setResult(1, intent);
finish();
in your MainActivity.java :
#Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
// Handle successful result
String name = intent.getStringExtra("name");
editText.setText(name);
}
}
}
refer this tutorial for more explanations

Categories