I'm trying to make an app with Android Studio that can select a file in SD card and get its path, like an OpenFileDialog, I've tried this:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
startActivityForResult(intent, PICKFILE_REQUEST_CODE);
However, it does not work, how can I do it ?
Try this:
Intent mediaIntent = new Intent(Intent.ACTION_GET_CONTENT);
mediaIntent.setType("*/*"); //set mime type as per requirement
startActivityForResult(mediaIntent,0);
You can change type according to your need.
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0
&& resultCode == Activity.RESULT_OK) {
Uri uri = data.getData();
Log.d("", "Video URI= " + videoUri);
}
}
Related
How to get the intent callback sharing result, i have seen some code and implemented from my side. Below is the sample code
try {
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "Body");
startActivityForResult(Intent.createChooser(intent, "Choose"), 1);
} catch (Exception e) {
e.printStackTrace();
}
Code for callback result
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
}
}
}
In this requestCode is always getting 0 not 1 after i successfully share to any social media platform, is there any code which is newer version?
The method you're using is deprecated, The new way is
ActivityResultLauncher<Intent> launcher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(), result -> {
if (result.getResultCode() == Activity.RESULT_OK) {
Intent data = result.getData();
// do something
}
});
Launching the activity
launcher.launch(yourIntent);
sharing result
Intent intent = new Intent();
intent.putExtra(YOUR_KEY, YOUR_DATA);
setResult(RESULT_OK, intent);
Dependency
dependencies {
implementation 'androidx.appcompat:appcompat:1.4.2'
implementation "androidx.activity:activity:1.2.0"
...
}
To learn more about this visit
I want to croping image from other activity to another activity with Canhub Android Image Cropper library. This is my code :
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
mCropImageUri = result.getOriginalUri();
createImage();
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
AppLogger.e(result.getError().getMessage());
}
}
if (requestCode == PermissionCheckUtils.LOCATION_PERMISSION_REQUEST_CODE && resultCode == RESULT_OK) {
getLocation();
}
}
and this when i access the camera :
private void openCropImage() {
Intent intent = CropImage.activity().setGuidelines(CropImageView.Guidelines.ON).getIntent(this);
startActivityForResult(intent, CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE);
}
with these code, i open camera but source include galery. My question is how to open the croper with source just from camera. I already read the documentation but I'm confused : https://github.com/CanHub/Android-Image-Cropper
It is very simple !!
In Your openCropImage() Function set following code :
Intent intent = CropImage
.activity()
.setImageSource(includeGallery = false, includeCamera = true)
.setGuidelines(CropImageView.Guidelines.ON)
.getIntent(this);
startActivityForResult(intent, CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE);
That's it !! Happy Coding :-)
I have an app that allows the user to select a file from the file chooser. The problem lies when I try to turn that Uri to a File, it creates something that I can't use (/document/raw:/storage/emulated/0/Download/CBTJourney-Backup/EntriesBackup1570487830108) I would like to get rid of everything before raw: but the right way. Where ever I try to copy from that file using InputStream, it doesn't copy anything. It's like the file doesn't exist. Any ideas?
public void chooseDatabaseFile() {
Intent intent = new Intent();
intent.addCategory(Intent.CATEGORY_OPENABLE);
// Set your required file type
intent.setType("*/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Choose Database to Import"),GET_FILE_PATH);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GET_FILE_PATH && data != null) {
if(resultCode == RESULT_OK){
Uri currFileURI = data.getData();
if(currFileURI == null) {
return;
}
else{
String databasePath = currFileURI.getPath();
// TODO: Determine if actual database
importDatabase(new File(databasePath));
// File produces "/document/raw:/storage/emulated/0/Download/CBTJourney-Backup/EntriesBackup1570487830108"
}
}
}
}
Have you tried changing your mimetype?
Otherwise take a look at:
Convert file: Uri to File in Android
I want to call the android's native camera and don't save it in the storage, because I save it myself after processing image. It works almost for all devices, except Motorola XT1032 (Android 5.1, API 22). So is there a way to tell the camera not to save the picture?
Here is how I call the camera intent.
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + File.separator + "image_"+System.currentTimeMillis()+".jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
((Activity) mContext).startActivityForResult(intent, 1001);
Create the request code variable:
private static final int REQUEST_CAMERA = 1;
Open the camera:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);
and in case you want to show it to the user:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQUEST_CAMERA:
if (resultCode == RESULT_OK) {
// successfully captured the image
Bitmap mBitmap = (Bitmap) data.getExtras().get("data");
if (mBitmap != null) {
imageView.setImageBitmap(mBitmap);
}
}
}
Ok so this here is the intent I am sending
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
startActivityForResult(intent, REQUEST_CODE);
And then in the onActivityResult I am doing this:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i("Intent name:",data.toString());
if (requestCode == REQUEST_CODE){
if (resultCode == Activity.RESULT_OK){
Toast.makeText(this, "Image saved to \n" + fileUri.toString() , Toast.LENGTH_LONG).show();
Toast.makeText(this, "Result Code: " + resultCode , Toast.LENGTH_LONG).show();
//Bitmap mBitMap = BitmapFactory.decodeFile(data.getData().toString());
//imageView.setImageBitmap(mBitMap);
}
else if (resultCode == RESULT_CANCELED){
Toast.makeText(this, "Capture Cancelled", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(this, "Capture failed", Toast.LENGTH_LONG).show();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
The LogCat is showing a NullPointerException at the line that says Image Saved....
And also this:
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=null}
This happens whether i try to use the data object or the fileUri field of my class.
Why is data being returned null?
Why is it that even though I am using a field of the class i still get the same error?
Whenever you save an image by passing EXTRAOUTPUT with camera intent ie
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
in a file, the data parameter inside the onActivityResult always return null. So, instead of using data to retrieve the image , use the filepath to retrieve the Bitmap.
So onActivityResult would be something like this:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
String[] fileColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(imageUri,
fileColumn, null, null, null);
String contentPath = null;
if (cursor.moveToFirst()) {
contentPath = cursor.getString(cursor
.getColumnIndex(fileColumn[0]));
Bitmap bmp = BitmapFactory.decodeFile(contentPath);
ImageView img = (ImageView) findViewById(R.id.imageView1);
img.setImageBitmap(bmp);
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Capture Cancelled", Toast.LENGTH_LONG)
.show();
} else {
Toast.makeText(this, "Capture failed", Toast.LENGTH_LONG)
.show();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
Make sure that you have taken imageUri or fileUri as a global variable so that you can access it inside onActivityResult as well. Best of luck
The correct/preferred way to handle data in these cases would be as:
In called Activity set data to the Intent , then setResult code as RESULT_OK and then finish that activity.
In this recieving activity , check the result code.. and retrieve data from Intent variable as :intent.getExtra("... "); //The variables which you have set in the child activity that has been closed now..