Retrieve data from onActivityResult (Fragment class) - java

onActivityResult (Activity class)
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (requestCode == 1 && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
Data.desiredaddress = cursor.getString(columnIndex);
cursor.close();
}
super.onActivityResult(requestCode, resultCode, data);
}
}
onActivityResult (Fragment class)
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 2 && resultCode == Activity.RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor1 = getActivity().getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor1.moveToFirst();
int columnIndex = cursor1.getColumnIndex(filePathColumn[0]);
Data.desiredaddress = cursor1.getString(columnIndex);
cursor1.close();
}
}
My problem :
The value of Data.desiredaddress change if it is modify in activity class but it did not change in fragment class.
can someone help me with this.
the app has no error and it didn't crash.
Thank you.

After i go through some other question , i found out that the reason for this is because onActivityResult (parent activity) will not be ignored even if i called onActivityResult from fragment class.
Thank you to #Aswin Rajendiran for the idea.
here is the link for further explaination : https://stackoverflow.com/a/16434914/4148255

Related

How to get Image Size from PhotoPicker Intent

Here is my On Click Listener:
#Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/jpeg");
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
startActivityForResult(Intent.createChooser(intent, "Complete action using"), PHOTO_PICKER_REQUEST_CODE);
}
here is my OnActivityResult:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PHOTO_PICKER_REQUEST_CODE && resultCode == RESULT_OK) {
Uri selectedImageUri = data.getData();
File imageFile = new File(getRealPathFromURI(selectedImageUri));
long length = imageFile.length();
length = length / 1024;
Log.d(TAG, "onActivityResult: ImageSize " + length +" Kb");
}
}
ImageSize always returns 0 (zero).
You probably missing the permission in the manifest. You need to add the following permission in AndroidManifest.xml:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Or, if you're targetting Android 6.0 (API level 23) and above, you need to Requesting Permissions at Run Time
And please check your getRealPathFromURI() method. I've testing with this following code and it's work:
public String getRealPathFromURI(Uri contentUri) {
String res = null;
String[] proj = { MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(contentUri, proj, "", null, "");
if (cursor.moveToFirst()) {
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
res = cursor.getString(column_index);
}
cursor.close();
return res;
}
Or use the code from Convert content:// URI to actual path in Android 4.4

For what this Android code is used?

I'm new on Android development, can you please explain me the following code and for what this Android code is it used?
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
if (requestCode == RESULT_LOAD_IMG && resultCode == getActivity().RESULT_OK
&& null != data) {
selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
// Get the cursor
Cursor cursor = getActivity().getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imgPath = cursor.getString(columnIndex);
cursor.close();
String fileNameSegments[] = imgPath.split("/");
fileName = fileNameSegments[fileNameSegments.length - 1];
_txt_img_url.setText(fileName);
Toast.makeText(getActivity(), fileName, Toast.LENGTH_SHORT).show();
ImageTask imgtask = new ImageTask();
imgtask.execute();
onActivityResult method is used to handle the data returned from an intent,
check here the documentation
https://developer.android.com/training/basics/intents/result.html
In short in this case if the result is ok, and we have data to handle, a toast appear with the path of the selected file.

Intent data is null in onActivityResult when I pick an Image from gallery

I need to get an Image from the gallery, and transform it into a bitmap.
This is the code for sending the Intent :
Intent i = new Intent(
Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
onActivityResult :
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == mResultLoadImage && resultCode == RESULT_OK && data != null) {
Uri pickedImage = data.getData();
String[] filePath = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(pickedImage, filePath,
null, null, null);
cursor.moveToFirst();
String imagePath = cursor.getString(cursor
.getColumnIndex(filePath[0]));
bitmap = BitmapFactory.decodeFile(imagePath);
someFunction(bitmap);
cursor.close();
}
}
I get always a NullPointerException, and with the debuger, I found out that the data Intent is null.
Any solutions?
Try this, it should work:
Intent intent = new Intent(Intent.ACTION_PICK);
startActivityForResult(intent, RESULT_LOAD_IMAGE);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE) {
Uri imageUri = data.getData();
Bitmap imgBitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri), null, null);
// then do whatever you want with the bitmap
}
}

Android: Open fileBrowser to get file path

I have been trying to figure out a way for my application to open a file browser where the user can select a video File and have my app store that path into a string to use later. Is there an Intent I can use or function? Any suggestions?
You can do it via intent chooser. Try this code i think this solution solve your purpose.
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, "DEMO"),1001);
then onActivityResult method.
public void onActivityResult(int requestCode, int resultCode,
Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
// super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1001) {
Uri currFileURI = data.getData();
String path=currFileURI.getPath();
}}
Here is a code I have used one of my previous projects.
Intent intent = new Intent();
intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Video "), 33);
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == 33) {
Uri selectedMediaUri = data.getData();
filemanagerstring = selectedMediaUri.getPath();
selectedMediaPath = getPath(selectedMediaUri);
if (!selectedMediaPath.equals("")) {
filePath = selectedMediaPath;
} else if (!filemanagerstring.equals("")) {
filePath = filemanagerstring;
}
int lastIndex = filePath.lastIndexOf("/");
fileName = filePath.substring(lastIndex+1);
//filepath is your file's path
}
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.MediaColumns.DATA };
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
if (cursor != null) {
// HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL
// THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} else
return "";
}
use below code.
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
startActivityForResult(intent,PICKFILE_RESULT_CODE);
and on activity result you get file path.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
switch(requestCode){
case PICKFILE_RESULT_CODE:
if(resultCode==RESULT_OK){
String FilePath = data.getData().getPath();
textFile.setText(FilePath);
}
break;
}
}

save path of image imported from gallery into sql database and display it in an imageview later

I'm trying to save the path of an image imported from the gallery using this method:
case R.id.media:
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
return true;
Here is the on activity result method:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
final Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = (cursor.getString(columnIndex));
cursor.close();
mImage = picturePath;
ImageView imageView = (ImageView) findViewById(R.id.note_image);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
imageView.setClickable(true);
imageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent viewImageIntent = new Intent(android.content.Intent.ACTION_VIEW, selectedImage);
startActivity(viewImageIntent);
}
});
}
}
And here is the populate field method:
mImage =(note.getString(
note.getColumnIndexOrThrow(NotesDbAdapter.KEY_IMAGE)));
But this is not working, the path doesnt get saved and when i close the activity and start the activity again, the image is gone. How can i fix this?

Categories