My UI has a button and a videoview.When we click button,go to Gallery Video and when we select a video in Video gallery,it return my UI and videoview will display video that is selected..I used code as follow but it don't display video :(
public class VideoGalleryActivity extends Activity {
/** Called when the activity is first created. */
Button button;
VideoView videoView;
private static final int PICK_FROM_GALLERY=1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button=(Button)findViewById(R.id.button);
videoView=(VideoView)findViewById(R.id.videoview);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Complete action using"),PICK_FROM_GALLERY);
}
}) ;
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode != RESULT_OK) return;
if (requestCode == PICK_FROM_GALLERY) {
Uri mVideoURI = data.getData();
videoView.setVideoURI(mVideoURI);
}
}
You have to let videoview start by adding videoview.start();. Code:
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode != RESULT_OK) return;
if (requestCode == PICK_FROM_GALLERY) {
Uri mVideoURI = data.getData();
videoView.setVideoURI(mVideoURI);
videoview.start(); //edited
}
}
Related
Hey guys im new to programming in Android and i came to an issue, i have three ImageView and every time i click on these ImageViews i want it to start on CropActivity. Afterwards, if crop activity is done, it will retrieve its image Uri to the imageview i clicked. However the issue is whenever i click on ImageView 2, get the image,and then crop it, the image is set right away to the three of the ImageViews. And not on the Image View 2. I tried searching online and cant seem to find the answer to it.
Here is my onCreate
public class EditProfileActivity extends AppCompatActivity {
private CardView profileImageEdit, profileImageEdit2, profileImageEdit3;
ImageView ivBack, ImageUploaded1, ImageUploaded2, ImageUploaded3;
Uri imageUri1, imageUri2, imageUri3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_profile);
profileImageEdit = findViewById(R.id.profileImageEdit);
profileImageEdit2 = findViewById(R.id.profileImageEdit2);
profileImageEdit3 = findViewById(R.id.profileImageEdit3);
ImageUploaded1 = findViewById(R.id.ImageUploaded1);
ImageUploaded2 = findViewById(R.id.ImageUploaded2);
ImageUploaded3 = findViewById(R.id.ImageUploaded3);
profileImageEdit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CropImage.activity()
.setAspectRatio(1, 1)
.start(EditProfileActivity.this);
}
});
profileImageEdit2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CropImage.activity()
.setAspectRatio(1, 1)
.start(EditProfileActivity.this);
}
});
profileImageEdit3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CropImage.activity()
.setAspectRatio(1, 1)
.start(EditProfileActivity.this);
}
});
ivBack.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(EditProfileActivity.this, ProfileActivity.class));
}
});
below the above Line is my cropActvity onActivity result
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
imageUri1 = result.getUri();
imageUri2 = result.getUri();
imageUri3 = result.getUri();//this is the problem
ImageUploaded1.setImageURI(imageUri1);
ImageUploaded2.setImageURI(imageUri2);
ImageUploaded3.setImageURI(imageUri3);
} else {
startActivity(new Intent(EditProfileActivity.this, ProfileActivity.class));
finish();
}
}
Here is the image: EditProfile
Many thanks guys!
create new imageView instance:
ImageView pickedImageView;
after every profileImageEdit set it to clicked ImageView:
ImageView pickedImageView;
profileImageEdit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CropImage.activity()
.setAspectRatio(1, 1)
.start(EditProfileActivity.this);
pickedImageView = profileImageEdit;
}
});
profileImageEdit2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CropImage.activity()
.setAspectRatio(1, 1)
.start(EditProfileActivity.this);
pickedImageView = profileImageEdit2;
}
});
profileImageEdit3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CropImage.activity()
.setAspectRatio(1, 1)
.start(EditProfileActivity.this);
pickedImageView = profileImageEdit3;
}
});
modify your onActivityResult with following:
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (pickedImageView != null)
pickedImageView.setImageURI(result.getUri());
} else {
startActivity(new Intent(EditProfileActivity.this, ProfileActivity.class));
finish();
}
}
I am using this code to pick an image from gallery and display it in an imageView, the problem that this is working just for photos in internal storage, and when I pick an image from my sd card it's not displayed in my imageView
I hope you have a solution.
public class MainActivity extends AppCompatActivity {
ImageView img ;
Button btn ;
Uri imageUri ;
private static final int PICK_IMAGE = 1 ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = findViewById(R.id.imageView) ;
btn = findViewById(R.id.button) ;
btn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
openGallery() ;
}
});
}
public void openGallery(){
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI) ;
startActivityForResult(intent,PICK_IMAGE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode==PICK_IMAGE && data != null){
imageUri = data.getData() ;
img.setImageURI(imageUri);
}else{
Toast.makeText(this," you haven't selected",Toast.LENGTH_LONG).show();
}
}
}
I am using those permissions in my manifest file :
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE">
</uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
I asked and I will answer, the problem was that most of my sd card photos are 2.0 MB or more and they can't be treated directly by the imageView so I added some lines in onActivityResult()
public class MainActivity extends AppCompatActivity {
ImageView img ;
Button btn ;
Uri imageUri ;
private static final int PICK_IMAGE = 1 ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = findViewById(R.id.imageView) ;
btn = findViewById(R.id.button) ;
btn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
openGallery() ;
}
});
}
public void openGallery(){
Intent intent = new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI) ;
startActivityForResult(intent,PICK_IMAGE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent
data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode==PICK_IMAGE && data != null){
imageUri = data.getData() ;
Bitmap bitmap ;
try {
bitmap=MediaStore.Images.Media
.getBitmap(this.getContentResolver(),imgUri);
Bitmap mBitmap = Bitmap.createScaledBitmap(bitmap, 160, 160, true);
img.setImageBitmap(mBitmap);
} catch (IOException e) {
e.printStackTrace();
}
}else{
Toast.makeText(this," you haven't selected",Toast.LENGTH_LONG).show();
}
}
I hope this will be helpful for those who can face the same problem
When intent is fired for recording video on button click, it is recording that but in onActivityResult() method it is not playing the video.
Thanks in advance.
public class Main2Activity extends AppCompatActivity {
private Button button;
private VideoView videoView;
static final int REQUEST_VIDEO_RECORD = 100;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
button = (Button) findViewById(R.id.record_video_button);
videoView = (VideoView) findViewById(R.id.display_video_view);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dispatchVideoIntent();
}
});
}
private void dispatchVideoIntent() {
Intent videoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
if(videoIntent.resolveActivity(getPackageManager()) != null){
startActivityForResult(videoIntent, REQUEST_VIDEO_RECORD);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_VIDEO_RECORD && resultCode == RESULT_OK){
Uri videoUri = data.getData();
videoView.setVideoURI(videoUri);
videoView.start();
}
}
}
I want make video player app that launch from gallery and play the video to 'new activity'. the problem is, i already browse the video at the gallery. the video are playing at the 'main activity' not play at the 'new activity'. please help.
MainActivity :
public class MainActivity extends Activity {
Button button1;
VideoView videoView;
private static final int SELECT_VIDEO = 1;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);
videoView=(VideoView)findViewById(R.id.videoView);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent();
intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Video"), SELECT_VIDEO);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != RESULT_OK) return;
if (requestCode == SELECT_VIDEO) {
Uri mVideoURI = data.getData();
videoView.setVideoURI(mVideoURI);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
videoView.start();
}
}
}
what coding should i put in my 'new activity.java'?
and i also want to put 4 video in same activity. what should i do?
put the intent in NewActivity not in MainActivity
I am making an android application, which takes an image from camera and then displays it. However, I am unable to display the clicked image probably because the onActivityResult() is not triggered.
Here is my piece of code. Can anyone suggest me what am i missing ?
public class Main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final int CAMERA_PIC_REQUEST = 1337;
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
#override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d("Message1", "I reached 2");
//super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_PIC_REQUEST) {
// do something
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageBitmap(thumbnail);
}
}
});
}
}
onActivityResult() must be declared in your Activity class (not inside the onClickListener). If you correct the "#override" ('o' must be capitalized), typo before your current onActivityResult() declaration, you'll see what I mean...
See the Activity.onActivityResult() documentation.
Here's how your class should look like:
public class Main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final int CAMERA_PIC_REQUEST = 1337;
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d("Message1", "I reached 2");
//super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_PIC_REQUEST) {
// do something
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageBitmap(thumbnail);
}
}
}