I'm starting DialogFragment from AddFragment by calling showPickImageDialog() method on button press. And in AlertDialogFragment onActivityResult I'm sending results back to AddFragment but AddFragment is not receiving any results. Any ideas why?
AddFragment:
public class AddFragment extends Fragment {
private static final int REQUEST_IMAGE = 3;
private static final String DIALOG_IMAGE = "image";
private static final String TAG = "TAG";
void showPickImageDialog() {
FragmentManager fm = getActivity()
.getSupportFragmentManager();
AlertDialogFragment pdialog = new AlertDialogFragment();
pdialog.setTargetFragment(this, REQUEST_IMAGE);
pdialog.show(fm, DIALOG_IMAGE);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_IMAGE && resultCode == Activity.RESULT_OK){
Log.e(TAG,"back in AddFragment");
}
}
}
In AlertDialogFragment Im calling sendResult method from onActivityResult method. SendResult method is called I checked it with Logs
AlertDialogFragment:
public class AlertDialogFragment extends DialogFragment {
private final static String TAG = "Cload";
public static final String EXTRA_PHOTOFILENAME =
"com.example.tadas.dreamcload1.image";
private static final int REQUEST_IMAGE = 3;
private String filename;
private Uri uri;
private void sendResult(int resultCode) {
if(getTargetFragment() == null){return;}
Intent i = new Intent();
i.putExtra(EXTRA_PHOTOFILENAME,filename);
getTargetFragment()
.onActivityResult(getTargetRequestCode(), resultCode, i);
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
//LayoutInflater inflater = getLayoutInflater();
// View dialogLaout = inflater.inflate(R.layout.custom_dialog_pick_image,null);
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.custom_dialog_pick_image, null);
final AlertDialog dialog = new AlertDialog.Builder(getActivity())
.setView(view)
.create();
ImageButton gallerybtn = (ImageButton)view.findViewById(R.id.gallery);
ImageButton camerabtn = (ImageButton)view.findViewById(R.id.camera);
gallerybtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.e(TAG,"gallery btn pressed");
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, REQUEST_IMAGE);
}
});
camerabtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
return dialog;
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_IMAGE && resultCode == Activity.RESULT_OK){
uri = data.getData();
final String[] filePathColumn = {MediaStore.MediaColumns.DATA, MediaStore.MediaColumns.DISPLAY_NAME};
Cursor c = getActivity().getContentResolver()
.query(uri,filePathColumn,null,null,null);
if (c.moveToFirst()){
int ids = c.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
uri = Uri.parse(c.getString(ids));
filename = uri.toString();
c.close();
}
sendResult(Activity.RESULT_OK);
dismiss();
}
}
You can override your onActivityResult method in your Activity and call your Fragment's onActivityResult manually like below:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.your_container);
fragment.onActivityResult(requestCode, resultCode, data);
}
You can define this part in your Activity's onActivityResult method:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_IMAGE && resultCode == Activity.RESULT_OK){
uri = data.getData();
final String[] filePathColumn = {MediaStore.MediaColumns.DATA, MediaStore.MediaColumns.DISPLAY_NAME};
Cursor c = getActivity().getContentResolver()
.query(uri,filePathColumn,null,null,null);
if (c.moveToFirst()){
int ids = c.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
uri = Uri.parse(c.getString(ids));
filename = uri.toString();
c.close();
}
// You do not need this any more
//sendResult(Activity.RESULT_OK);
//dismiss();
// Just find your fragment and call it's onActivityResult
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.your_container);
fragment.onActivityResult(requestCode, resultCode, data);
}
}
I think this change'll help you.
Related
When I run it, I get an error saying that childName cannot be null. The variable "data" is getting null when taking the photo and I don't know how to fix it.
This is my code.
public class TakePhoto extends AppCompatActivity {
private Button btnPhoto;
private ImageView mImageView;
private static final int CAMERA_REQUEST_CODE = 1;
private StorageReference storage;
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String fecha = df.format(c.getTime());
private ProgressDialog progress;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_take_photo);
storage = FirebaseStorage.getInstance().getReference();
btnPhoto = (Button) findViewById(R.id.buttonCamera2);
mImageView = (ImageView) findViewById(R.id.fotoFinal);
progress = new ProgressDialog(this);
btnPhoto.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_REQUEST_CODE);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK){
Uri uri = data.getData();
StorageReference filepath = storage.child(getIntent().getStringExtra("dato")).child(fecha + " " + uri.getLastPathSegment());
filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(TakePhoto.this, "Subiendo...", Toast.LENGTH_LONG).show();
}
});
}
}
}
I have two activities and the second activity should send back a string of text to the appropriate textview on activity 1 depending on if I select button 1 or button 2. I believe my intent in launchFoodActivity() in activity 1 is incorrectly asking for 2 requests at the same time. How can I correct my code to have each string of text be sent to the appropriate textview in activity 1?
Activity 1
public class MainActivity extends AppCompatActivity {
private TextView mReplyTextView1;
private TextView mReplyTextView2;
public static final int TEXT_REQUEST = 1;
public static final int TEXT_REQUEST2 = 2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mReplyTextView1 = findViewById(R.id.food_text_1);
mReplyTextView2 = findViewById(R.id.food_text_2);
}
public void launchFoodActivity(View view) {
Intent intent = new Intent(this, FoodListActivity.class);
startActivityForResult(intent, TEXT_REQUEST);
Intent intent2 = new Intent(this, FoodListActivity.class);
startActivityForResult(intent2, TEXT_REQUEST2);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 1:
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
String textFoodReply1 = data.getStringExtra(FoodListActivity.EXTRA_REPLY);
mReplyTextView1.setText(textFoodReply1);
break;
}
}
case 2:
if (requestCode == 2) {
if (resultCode == RESULT_OK) {
String textFoodReply2 = data.getStringExtra(FoodListActivity.EXTRA_REPLY);
mReplyTextView2.setText(textFoodReply2);
break;
}
}
}
}
}
Activity 2
public class FoodListActivity extends AppCompatActivity {
public static final String EXTRA_REPLY = "com.dev20.shoppinglist.extra.REPLY";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_food_list);
}
public void returnFoodItem1(View view) {
String replyItem1 = getResources().getString(R.string.cheese_text);
Intent replyIntent = new Intent();
replyIntent.putExtra(EXTRA_REPLY, replyItem1);
setResult(RESULT_OK, replyIntent);
finish();
}
public void returnFoodItem2(View view) {
String replyItem2 = getResources().getString(R.string.apples_text);
Intent replyIntent2 = new Intent();
replyIntent2.putExtra(EXTRA_REPLY, replyItem2);
setResult(RESULT_OK, replyIntent2);
finish();
}
}
I am creating gallery using HorizontalScrollView, and I want to click the image in HorizontalScrollView.
Here is my code:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK){
if(data.getClipData() != null){
int totalItemsSelected = data.getClipData().getItemCount();
linearLayout = (LinearLayout) findViewById(R.id.linear);
for(int i = 0; i < totalItemsSelected; i++){
Uri fileUri = data.getClipData().getItemAt(i).getUri();
ImageView image = new ImageView(this);
image.setAdjustViewBounds(true);
image.setImageURI(fileUri);
linearLayout.addView(image);
}
}
}
}
Hi use the below code to implement click event to ImageView:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK){
if(data.getClipData() != null){
int totalItemsSelected = data.getClipData().getItemCount();
linearLayout = (LinearLayout) findViewById(R.id.linear);
for(int i = 0; i < totalItemsSelected; i++){
Uri fileUri = data.getClipData().getItemAt(i).getUri();
ImageView image = new ImageView(this);
image.setAdjustViewBounds(true);
image.setImageURI(fileUri);
//click event
image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.e(TAG,"Selected File URI : "+fileUri);
}
});
linearLayout.addView(image);
}
}
}
}
Set onClickListener in loop. So every ImageView will have it's own click listener. Before adding image into layout.
image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// do ur work here
}
})
Thanks
See below:
for(int i = 0; i < totalItemsSelected; i++){
Uri fileUri = data.getClipData().getItemAt(i).getUri();
ImageView image = new ImageView(this);
image.setAdjustViewBounds(true);
image.setImageURI(fileUri);
image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//what you want to do
}
});
linearLayout.addView(image);
}
I just need help on my codes, I'm using Android Studio. Everything is working fine but the recorded video won't show in viewVideo on my layout.
Here's the code:
public class MainActivity extends AppCompatActivity {
static final int REQUEST_VIDEO_CAPTURE = 1;
VideoView resultvideo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button click = (Button)findViewById(R.id.videorec);
resultvideo = (VideoView)findViewById(R.id.videoView);
}
public void dispatchTakeVideoIntent(View v) {
Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
if (takeVideoIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takeVideoIntent, REQUEST_VIDEO_CAPTURE);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_VIDEO_CAPTURE && resultCode == RESULT_OK) {
Uri videoUri = data.getData();
resultvideo.setVideoURI(videoUri);
}
}
}
Uri videoUri = data.getData();
resultvideo.setVideoURI(videoUri);
// start call missing
resultvideo.start();
I'm trying to load an image from gallery on android but when I pick the image, it doesn't display in my ImageView,and I get the error "open failed ENOENT (No Such file or directory) can anyone help me please?
Here's my code :
private static int RESULT_LOAD_IMAGE = 1;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
buttonLoadImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
}
#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) {
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();
ImageView imageView = (ImageView) findViewById(R.id.imgView);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
Try Like this..
/** Called when the activity is first created. */
ImageView imageView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView = (ImageView) findViewById(R.id.imgView);
#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) {
Uri selectedImage = data.getData();
String picturePath;
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(selectedImage , projection, null, null, null);
if (cursor != null) {
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
picturePath= cursor.getString(column_index);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
Same code is working on my side. Check with your media images.