Unable To Open Images and Video Using onActivityResult - Edited / Updated - java

I'm trying to accomplish a very simple task:
I need to be able to click the imageButton for video, select from gallery and play, and click the imageButton for images, select an image and view it - ALL USING THE GALLERY
How might this be accomplished?
I believe I'll need to modify my onActivityResult since it does not currently perform as expected.
Current Source:
ImageButton pb = (ImageButton) findViewById(R.id.photos);
pb.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, SELECT_PHOTO);
}
});
ImageButton vb = (ImageButton) findViewById(R.id.video);
vb.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
.show();
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("video/*");
startActivityForResult(intent, SELECT_VIDEO);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if (resultCode == Activity.RESULT_OK) {
Uri selectedVideo = data.getData();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(selectedVideo, "video/*, image/*");
startActivity(Intent.createChooser(intent,
"Complete action using"));
}
}
;
}
}
Edit:
I have updated my source to reflect the most recent answer - however I am still unable to open images - I continue to get the "unable to play video" when attmempting to view an image.

checkout this for video:
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Video"), SELECT_VIDEO);
and this for images:
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Images"), SELECT_PHOTO);
and in onActivityResult try this :
if (requestCode==SELECT_VIDEO) {
//take whatever video action
}else if(requestCode==SELECT_PHOTO){
//take whatever image action
}

Related

Application terminate when cropping Image

I am having an Application which captures an image and send the ImageUri to Crop Intent for cropping. Some of the photos are getting cropped properly. When there is a change in resolution Application(i.e. Photo taken using front camera with different resolution or Uploading a downloaded images) is getting terminated automatically without any error.
Code to open Camera
fabcamera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Open Camera and upload the photo
Intent cameraIntent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent,CAMERA_CAPTURE);
}
});
private void performCrop(Uri picUri){
Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.setDataAndType(picUri, "image/*");
cropIntent.putExtra("crop", "true");
cropIntent.putExtra("aspectX", 0);
cropIntent.putExtra("aspectY", 0);
cropIntent.putExtra("outputX", 0);
cropIntent.putExtra("outputY", 0);
cropIntent.putExtra("return-data", true);
startActivityForResult(cropIntent, PIC_CROP);
}
fabgallery.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Open Gallery and upload the photo
Intent intent=new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(intent.createChooser(intent,"Select File"),SELECT_FILE);
}
});
Can anyone give me a suggestion?

Move data by intent without startActivity

I have 2 Activities. On 2nd activity i have data which i want to move by button "Back" to 1st Activity.
Usually i moving by something like this:
button12.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
pass=editText2.getText().toString();
Intent intent = new Intent(FirstActivity.this, MapsActivity.class);
intent.putExtra("pass_value", pass);
startActivity(intent);
}
});
But now i dont want to start Activity, instead of this i want close Activity, so i need to use finish()
Currently i created this, but no working:
b2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, FirstActivity.class);
intent.putExtra("number", numberOfTrue);
finish();
}
});
I need something more in this code, but i dont know what.
Start a new activity with
Intent intent = new Intent(FirstActivity.this, MapsActivity.class);
intent.putExtra("pass_value", pass);
startActivityForResult(intent,1)
You can use setResult method to acheive your desired result
Intent output = new Intent();
output.putExtra("number", numberOfTrue);
setResult(Activity.RESULT_OK, output);
finish();
get Your result in FirstActivity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && resultCode == Activity.RESULT_OK && data != null) {
int num = data.getIntExtra("pass_value");
}
}
b2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
finish();
Intent intent = new Intent(this, FirstActivity.class);
intent.putExtra("number", numberOfTrue); }});
You need to open by function startActivityForResult
startActivityForResult(Intent intent, int requestCode)
And get the data returned in the function
onActivityResult
As an example you can see a face to the camera for a picture

Four Activities don't display Toast

I have four activities:
Activity A
private void addCard() {
Intent intent = new Intent(MainActivity.this, GetNumberActivity.class);
startActivityForResult(intent, REQUEST_CODE_CREATE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_CREATE) {
if (resultCode == RESULT_OK) {
if (data.hasExtra("data")) {
// Card has been create
Toast.makeText(getApplication(), "Karata została wygenerowana.", Toast.LENGTH_SHORT).show();
}
}
}
}
Activity B
Intent intent = new Intent(GetNumberActivity.this, ScanQrCodeActivity.class);
intent.putExtra(EXTRA_MESSAGE, uunitValue);
startActivityForResult(intent, REQUEST_CODE);
Then in the second activity I have to pass data to the third activity.
Activity C
Card card = new Card(path3, base32, nameCard, intervalTotp, passwordHotp, getDate(), expirationDate, hotpValue);
Intent intent = new Intent(ScanQrCodeActivity.this, Stage3Activity.class);
intent.putExtra("card", card);
startActivity(intent);
finish();
Activity D
Intent data = new Intent(Stage3Activity.this,MainActivity.class);
data.putExtra("data", card);
startActivityForResult(data, RESULT_OK);
When I press the button on Activity A, the Toast is not shown.
You need to update your code as follow
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_CODE_CREATE) {
if (data.hasExtra("data")) {
// Card has been create
Toast.makeText(getApplication(), "Karata została wygenerowana.", Toast.LENGTH_SHORT).show();
}
}
}
First check for RESULT_OK and then proceed further
Happy Coding!
Replace getApplication() with this (the context of the current activity)
Toast.makeText(this.class, "Karata została wygenerowana.", Toast.LENGTH_SHORT).show();
Use getApplicationContext() instead of getApplication() in your makeText() method

Call method when intent closes

I am making a music player application, and I am trying to implement playlists. I have a file chooser in another intent, and I would like the ListView in the mainActivity to update when the file chooser intent closes. how can I call my UpdateListView method when it closes?
start intent:
Intent intent = new Intent(this, FileChooser.class);
startActivity(intent);
Closing intent
public void closeButton(View view){
finish();
}
Any help would be appreciated! thanks!
I assume you are using your own FileChoser class, not a standard Android one:
private static final int FileChooserRequestCode = 666;
Intent intent = new Intent(this, FileChooser.class);
startActivityForResult(intent, FileChooserRequestCode);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == FillChooserRequestCode) {
if (resultCode == Activity.RESULT_OK) {
// ... file is chosen
String fileName = data.getStringExtra("FileName");
} else {
... dialog is closed
}
}
}
in FileChoser you do
Intent intent = new Intent();
intent.putStringExtra("FileName", fileName);
SetResult(Activity.RESULT_OK, intent);
finish();
and
SetResult(Activity.RESULT_CANCELED);
finish();
You can use startActivityForResult() please refer the link Getting Results From Activity
static final int FILE_CHOOSER_INTENT = 1; // The request code
...
private void chooseFile() {
Intent intent = new Intent(this, FileChooser.class);
startActivityForResult(intent, FILE_CHOOSER_INTENT);
}
Call setResult pass your result data as Intent. for details refer link SetResult function
Override this in your calling activity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == FILE_CHOOSER_INTENT) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
// The user picked a contact.
// The Intent's data Uri identifies which contact was selected.
// Do something with the contact here (bigger example below)
}
}
}

Get address from file explorer Android

I'm making a program that asks the user to find a file which could be stored on the sdcard or the internal card of the phone. I know that in order to open the default file explorer in the phone I must use this code:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
startActivity(intent);
However, my problem is related with the communication with the File Explorer, how can I get the address of the file the user selected.
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
startActivityForResult(intent, YOUR_REQUEST_CODE);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case YOUR_REQUEST_CODE:
//get the uri from data's extras
break;
}
//do whatever you want with uri
} else {
Toast.makeText(this, "Wrong result", Toast.LENGTH_SHORT).show();
}
}

Categories

Resources