I am working on android Launcher, Where it contains three activities, The first activity is HomeActivity which contain four buttons which will be perform intent operations and the rest of the activities are blank but, At HomeActivity when I click on contact button it will display contact list, but when I choose one of the contact it shows nothing and the rest of the buttons also behave like contact button it will not jump to the OnActivityResult() method so how can I fix this problem?
Below is my code:-
MainHomeActivity.java
public class MainHomeActivity extends AppCompatActivity {
DevicePolicyManager devicePolicyManager;
ComponentName adminComponent;
Button apps;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Button lock = (Button) findViewById(R.id.button_lock);
lock.setOnClickListener(btnListener);
Settings.System.putInt(getContentResolver(),Settings.System.SCREEN_OFF_TIMEOUT,15000);
apps = (Button) findViewById(R.id.main_menu);
}
Button.OnClickListener btnListener = new Button.OnClickListener() {
#Override
public void onClick(View v) {
adminComponent = new ComponentName(MainHomeActivity.this, AdminReceiver.class);
devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
if (!devicePolicyManager.isAdminActive(adminComponent)) {
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, adminComponent);
startActivityForResult(intent, 50);
} else {
devicePolicyManager.lockNow();
}
apps.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainHomeActivity.this, AppsListActivity.class);
startActivity(i);
}
});
}
};
}
HomeActivity.java
public class HomeActivity extends Activity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_home_activity);
ViewPager viewPage = (ViewPager) findViewById(R.id.viewpager);
viewPage.setAdapter(new CustomPageAdapter(this));
}
public void contact(View view){
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
if(intent.resolveActivity(getPackageManager()) != null){
startActivityForResult(intent,10);
}
}
public void audioIntent(View view){
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getParent()+"/ext_card");
intent.setDataAndType(uri,"audio/*");
startActivityForResult(intent,20);
}
public void videoIntent(View v){
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getParent()+"/ext_card");
intent.setDataAndType(uri,"video/*");
startActivityForResult(intent,30);
}
public void pdfIntent(View view){
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getParent()+"/ext.card");
intent.setDataAndType(uri,"pdf/*");
startActivityForResult(intent,40);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == RESULT_OK && resultCode == 10){
Uri contactUri = data.getData();
Intent contactIntent = new Intent(Intent.ACTION_CALL);
contactIntent.setData(contactUri);
startActivity(contactIntent);
}
if(resultCode == 20 && requestCode == RESULT_OK){
Uri audioUri = data.getData();
Intent audioIntent = new Intent(Intent.ACTION_VIEW);
audioIntent.setDataAndType(audioUri,"audio/*");
startActivity(audioIntent);
}
if(requestCode == RESULT_OK && resultCode == 30){
Uri videoUri = data.getData();
Intent videoIntent = new Intent(Intent.ACTION_VIEW);
videoIntent.setDataAndType(videoUri,"video/*");
startActivity(videoIntent);
}
if(requestCode == RESULT_OK && resultCode == 40){
Uri pdfUri = data.getData();
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(pdfUri,"pdf/*");
startActivity(pdfIntent);
}
}
}
CustomPagerAdapter.java
public class CustomPageAdapter extends PagerAdapter {
private Context mContext;
public CustomPageAdapter(Context context){
mContext = context;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
ModelObject modelObject = ModelObject.values()[position];
LayoutInflater inflater = LayoutInflater.from(mContext);
ViewGroup layout = (ViewGroup) inflater.inflate(modelObject.getLayoutResId(),container,false);
container.addView(layout);
return layout;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
#Override
public int getCount() {
return ModelObject.values().length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
#Override
public CharSequence getPageTitle(int position) {
ModelObject customPagerEnum = ModelObject.values()[position];
return mContext.getString(customPagerEnum.getTitleResId());
}
}
This is the screenshot of the application:-
You are doing wrong in your if conditions. You are trying to compare requestCode with RESULT_OK and resultCode with request codes (10,20,30 and 40) which is totally wrong.
Use:
if(requestCode == 10 && resultCode == RESULT_OK)
Instead of:
if(requestCode == RESULT_OK && resultCode == 10)
Update onActivityResult() as below:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == 10 && resultCode == RESULT_OK){
Uri contactUri = data.getData();
Intent contactIntent = new Intent(Intent.ACTION_CALL);
contactIntent.setData(contactUri);
startActivity(contactIntent);
}
if(requestCode == 20 && resultCode == RESULT_OK){
Uri audioUri = data.getData();
Intent audioIntent = new Intent(Intent.ACTION_VIEW);
audioIntent.setDataAndType(audioUri,"audio/*");
startActivity(audioIntent);
}
if(requestCode == 30 && resultCode == RESULT_OK){
Uri videoUri = data.getData();
Intent videoIntent = new Intent(Intent.ACTION_VIEW);
videoIntent.setDataAndType(videoUri,"video/*");
startActivity(videoIntent);
}
if(requestCode == 40 && resultCode == RESULT_OK){
Uri pdfUri = data.getData();
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(pdfUri,"pdf/*");
startActivity(pdfIntent);
}
}
Hope this will help~
Related
I have 2 imageview with a certain image which send to the camera of the device and I want to validate, I want that when a photo is taken it changes image depending on the button from which the photo was taken.
I tried to do it this way but it didn't work.
ImageView imageV, imageV2;
static final int IMAGE_REQUEST = 1;
private static final int PERMISSION_REQUEST = 2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageV = findViewById(R.id.image_view);
imageV2 = findViewById(R.id.image_view_2);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent camara = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (camara.resolveActivity(getPackageManager()) != null) {
startActivityForResult(camara, IMAGE_REQUEST);
}
}
});
btnIne.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent camara = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (camara.resolveActivity(getPackageManager()) != null) {
startActivityForResult(camara, IMAGE_REQUEST);
}
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == IMAGE_REQUEST) {
if (resultCode == Activity.RESULT_OK) {
//Images to change
imageV.setImageResource(R.drawable.image_view_2do);
imageV2.setImageResource(R.drawable.image_view2_2do);
}
else {
//Default images
imageV.setImageResource(R.drawable.image_view_1er);
imageV2.setImageResource(getResources().getString(R.string.image_view2_1er));
}
}
}
You have to add different request code in startActivityForResult. Like you have used IMAGE_REQUEST for both button.
As Example :
ImageView imageV, imageV2;
static final int IMAGE_REQUEST = 1;
static final int IMAGE_REQUEST_2 = 2;
private static final int PERMISSION_REQUEST = 2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageV = findViewById(R.id.image_view);
imageV2 = findViewById(R.id.image_view_2);
btnStatementAccount.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent camara = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (camara.resolveActivity(getPackageManager()) != null) {
startActivityForResult(camara, IMAGE_REQUEST);
}
}
});
btnIne.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent camara = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (camara.resolveActivity(getPackageManager()) != null) {
startActivityForResult(camara, IMAGE_REQUEST_2);
}
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == IMAGE_REQUEST) {
if (resultCode == Activity.RESULT_OK) {
//Images to change
imageV.setImageResource(R.drawable.image_view_2do);
imageV2.setImageResource(R.drawable.image_view2_2do);
}
else if (requestCode == IMAGE_REQUEST_2) {
if (resultCode == Activity.RESULT_OK) {
//Images to change
imageV.setImageResource(R.drawable.image_view_2do);
imageV2.setImageResource(R.drawable.image_view2_2do);
}
else {
//Default images
imageV.setImageResource(R.drawable.image_view_1er);
imageV2.setImageResource(getResources().getString(R.string.image_view2_1er));
}
}
}
I'm trying to display a captured image on my MainActivity. The problem is it says that onActivityResult is already defined. My first onActivityResult is for the VideoView. Now I have to put a new onActivityResult for my image. How do I make this possible? Do I only have to have one onActivityResult? Do I have to put the second onActivityResult to the first onActivityResult?
Here's my code:
public class MainActivity extends Activity {
private static final int CAMERA_REQUEST = 1888;
private ImageView imageView;
Button buttonPlay;
Button buttonFullScreen;
static final int REQUEST_VIDEO_CAPTURE = 1;
VideoView resultvideo;
MediaController mediacontroller;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
resultvideo = (VideoView)findViewById(R.id.videoView);
mediacontroller = new MediaController(MainActivity.this);
mediacontroller.setAnchorView(resultvideo);
resultvideo.setMediaController(mediacontroller);
Button click = (Button)findViewById(R.id.buttonRecord);
resultvideo = (VideoView)findViewById(R.id.videoView);
this.imageView = (ImageView)this.findViewById(R.id.imageView);
Button photoButton = (Button) this.findViewById(R.id.buttonCapture);
photoButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
}
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) {
final Uri videoUri = data.getData();
if (requestCode == REQUEST_VIDEO_CAPTURE && resultCode == RESULT_OK) {
resultvideo.setVideoURI(videoUri);
mediacontroller.setAnchorView(resultvideo);
resultvideo.pause();
}
buttonPlay = (Button) findViewById(R.id.buttonPlay);
{
buttonPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediacontroller.show();
mediacontroller.setAnchorView(resultvideo);
resultvideo.start();
}
});
}
buttonFullScreen = (Button) findViewById(R.id.buttonFullScreen);
{
buttonFullScreen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
intent.putExtra("VIDEO_URI", videoUri.toString());
startActivity(intent);
}
});
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
}
create two view ImageView for Image and VideoView for Video and use this:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode){
case CAMERA_REQUEST:
if (resultCode == Activity.RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
//Hide video view
videoview.setVisibility(View.GONE);
}
break;
case REQUEST_VIDEO_CAPTURE:
//bring your video stuff here
videoview.setVisibility(View.VISIBLE);
imageView.setVisibility(View.GONE);
break;
}
}
You need only a single onActivityResult(int requestCode, int resultCode, Intent data) for your Activity class
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode){
case CAMERA_REQUEST:
if (resultCode == Activity.RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
break;
case REQUEST_VIDEO_CAPTURE:
//bring your video stuff here
break;
}
}
Your onActivityResult should look like the above code. Every function can be declared with a the same types and order of parameters only once in a class.
You can not have two function with the same signature.
You have to have a single onActivityResult, and in the if statement, you have to handle the resoult.
Example
if(resultCode == Activity.RESULT_OK){
if (requestCode == CAMERA_REQUEST ) {
...
}
if (requestCode == REQUEST_VIDEO_CAPTURE ) {
...
}
}
How i can choose image from gallery and them put it on parse.com ?
There i open GALLERY:
#Override
public void onClick(View arg0) {
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, 1);
i.setType("image/*");
i.setAction(Intent.ACTION_VIEW);
///// - here must be code tat can choose file that i must upload to parse
////
There is code that can upload image to parse.com :
I think it will be something like this
ParseObject image = new ParseObject("guestList");
image.put("photo", image);
image.saveInBackground();
UPD
public class SendGuestPhoto extends Activity {
ImageView imageView;
private static final int PICK_IMAGE = 100;
String path;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.send_guest_photo);
imageView = (ImageView) findViewById(R.id.image_view);
Button button_choose_guest_photo = (Button)findViewById(R.id.button_choose_guest_photo);
button_choose_guest_photo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openGallery();
}
});
}
private void openGallery() {
Intent gallery =
new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(gallery, PICK_IMAGE);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == PICK_IMAGE) {
Uri imageUri = data.getData();
imageView.setImageURI(imageUri);
}
}
public void SendPhoto(){
Button button_send_guest_photo = (Button)findViewById(R.id.button_send_guest_photo);
button_send_guest_photo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ParseObject photos = new ParseObject("guestsImages");
photos.put("photos", ????????????????);
photos.saveInBackground();
}
});
}
}
I'm attempting to view an image after selecting it using Gallery launched via intent however I'm getting an error when attempting to do so. I think this may have to do with:
intent.setDataAndType(selectedVideo, "video/*, image/*");
or some sort of conflict in onActivityResult between opening images and video - but I'm not sure.
Any input is greatly appreciated:
P.S.
Ideally this would open the image using the gallery once it is selected - not open it using my app.
Source:
ImageButton pb = (ImageButton) findViewById(R.id.photos);
pb.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Toast.makeText(UI.this, "Before and After Photos",
// Toast.LENGTH_LONG).show();
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) {
// Toast.makeText(UI.this, "Video Testimonial", Toast.LENGTH_LONG)
// .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'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?
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"));
}
}
;
}
}
What you have
startActivityForResult(intent, SELECT_PHOTO);
Then you have
if (requestCode == 1) {
Then
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(selectedVideo, "video/*, image/*");
startActivity(Intent.createChooser(intent,
"Complete action using"));
You are trying to use intent chooser and android says there is no app that can perform the action.
You need to change to
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SELECT_PHOTO) {
if (resultCode == Activity.RESULT_OK) {
Uri selectedImage = data.getData();
iv.setImageURI(selectedImage);
}
}
Example:
I have used a ImageView to display the image selected from gallery
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imageButton1"
android:layout_centerHorizontal="true"
android:layout_marginTop="168dp"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
MainActivity
public class MainActivity extends Activity {
private static final int SELECT_PHOTO = 100;
ImageView iv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv= (ImageView) findViewById(R.id.imageView1);
ImageButton ab = (ImageButton) findViewById(R.id.imageButton1);
ab.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Image Testimonial", Toast.LENGTH_LONG)
.show();
Intent intent = new Intent(
Intent.ACTION_PICK
);
intent.setType("image/*");
startActivityForResult(intent, SELECT_PHOTO);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SELECT_PHOTO) {
if (resultCode == Activity.RESULT_OK) {
Uri selectedImage = data.getData();
iv.setImageURI(selectedImage);
}
}
}
}
To the edited question
public class MainActivity extends Activity {
// Splash screen timer
private static int SPLASH_TIME_OUT = 5000;
private static final int SELECT_PHOTO = 101;
private static final int SELECT_VIDEO = 100;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fg);
ImageButton ab = (ImageButton) findViewById(R.id.imageButton1);
ab.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Audio Testimonial", Toast.LENGTH_LONG)
.show();
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, 1);
}
});
ImageButton vb = (ImageButton) findViewById(R.id.imageButton2);
vb.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Video Testimonial", Toast.LENGTH_LONG)
.show();
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("video/*");
startActivityForResult(photoPickerIntent, SELECT_VIDEO);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK)
{
if (requestCode == SELECT_VIDEO) {
{
Uri selectedVideo = data.getData();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(selectedVideo, "video/*");
startActivity(intent);
}
}
else if (requestCode == SELECT_PHOTO) {
Uri selectedImage = data.getData();
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(selectedImage,"image/*");
startActivity(intent);
}
}
// ImageButton wb = (ImageButton) findViewById(R.id.imageButton3);
// wb.setOnClickListener(new View.OnClickListener() {
// public void onClick(View v) {
// Toast.makeText(MainActivity.this, "Written Testimonial",
// Toast.LENGTH_LONG).show();
// Intent intent = new Intent(MainActivity.this, Written.class);
// startActivity(intent);
// }
// });
ImageButton pb = (ImageButton) findViewById(R.id.imageButton4);
pb.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Before and After Photos",
Toast.LENGTH_LONG).show();
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
}
});
}
}
In Huawei P9, android marshmallow, one device has not any application that can manage that intent's request (it has no gallery nor photo app). So manually installed those app and worked perfectlly
I have the same code in 2 Classes of my android-project. I´m just begin to learn java / android, so pls could you give me some tips? Do I have set a new class? Thank you for your help!
Main.java
public class Main extends Activity {
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.activity_main);
Button btnGallery = (Button) findViewById(R.id.btnGallery);
btnGallery.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);
Intent uploadActivity = new Intent(Main.this, Upload.class);
uploadActivity.putExtra("picturePath", picturePath);
startActivity(uploadActivity);
cursor.close();
}
}
}
And Upload.java
public class Upload extends Activity {
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.activity_upload);
Bundle extras = getIntent().getExtras();
String picturePathView = extras.getString("picturePath");
ImageView imageView = (ImageView) findViewById(R.id.imgView);
findViewById(R.id.imgView).setScrollBarStyle(
View.SCROLLBARS_INSIDE_INSET);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePathView));
Button btnGallery = (Button) findViewById(R.id.btnGallery);
btnGallery.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);
ImageView imageView = (ImageView) findViewById(R.id.imgView);
findViewById(R.id.imgView).setScrollBarStyle(
View.SCROLLBARS_INSIDE_INSET);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
cursor.close();
}
}
}
This is actually what hasanghaforian said, but a little bit more conrect.
Your abstract Activity which enables holds the properties which are the same for Upload and Main could be the following
public abstract class AbstractMediaPickerActivity extends Activity {
protected static int RESULT_LOAD_IMAGE = 1;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
protected void startMediaPicker() {
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
protected abstract void onImagePicked(String picturePath);
#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();
onImagePicked(picturePath);
}
}
}
Your both other classes are inherited by the AbstractMediaPickerActivity
public class MainActivity extends AbstractMediaPickerActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnGallery = (Button) findViewById(R.id.btnGallery);
btnGallery.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
startMediaPicker();
}
});
}
protected void onImagePicked(String picturePath) {
Intent uploadActivity = new Intent(Main.this, Upload.class);
uploadActivity.putExtra("picturePath", picturePath);
startActivity(uploadActivity);
}
}
public class Upload extends AbstractMediaPickerActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upload);
Bundle extras = getIntent().getExtras();
String picturePathView = extras.getString("picturePath");
ImageView imageView = (ImageView) findViewById(R.id.imgView);
findViewById(R.id.imgView).setScrollBarStyle(
View.SCROLLBARS_INSIDE_INSET);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePathView));
Button btnGallery = (Button) findViewById(R.id.btnGallery);
btnGallery.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
startMediaPicker();
}
});
}
#Override
protected void onImagePicked(String picturePath) {
ImageView imageView = (ImageView) findViewById(R.id.imgView);
findViewById(R.id.imgView).setScrollBarStyle(
View.SCROLLBARS_INSIDE_INSET);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
Create a Class(for example SuperActivity) that extends Activity and add duplicated code to it.Then create Activities A,B that extend SuperActivity.
Sure you can put all the code in one class. I'm not gonna write all the code but you will get the idea how to gain refactoring in your code.
For onClickListner, you can do the following:
#Override
public void onClick(View clickedView) {
int clickedViewId = clickedView.getId();
switch(clickedViewId) {
case R.id.firstItem:
// code when first view is clicked
case R.id.secondItem:
// code when second view is clicked
default:
// optional
}
}
And then setting the same onClickListner for both views.
The same kind of switch statement you can use in onActivityResult method. I hope now you can merge the two classes in one.