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.
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 want to get the image from one activity to another activity. I tried converting the image to byte array in sender activity (PhotoActivity) and decoding the array to form original bitmap image on the receiver side (MainActivity) on the click of button 'btdn' but this isn't working. The image is visible in the sender activity's imageview but it stops when the button is clicked. This is my code:-
PhotoActivity.java
public class PhotoActivity extends Activity {
ImageView vitb;
Button btcm, btdn;
static final int REQUEST_IMAGE_CAPTURE = 1;
static final int REQUEST_IMAGE_SELECT = 2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_photo);
vitb = findViewById(R.id.imageView);
btcm = findViewById(R.id.btn_cam);
btdn = findViewById(R.id.btn_back);
btcm.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
selectImage();
}
});
btdn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Drawable drawable = vitb.getDrawable();
Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
ByteArrayOutputStream bs = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 0, bs);
byte[] byteArray = bs.toByteArray();
Intent nIntent = new Intent();
nIntent.putExtra("PICTURE", byteArray);
setResult(RESULT_OK, nIntent);
finish();
}
});
}
private void selectImage() {
final CharSequence[] options = { "Take Photo", "Choose from Gallery","Cancel" };
AlertDialog.Builder builder = new AlertDialog.Builder(PhotoActivity.this);
builder.setTitle("Add Photo!");
builder.setItems(options, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int item) {
if (options[item].equals("Take Photo"))
{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,REQUEST_IMAGE_CAPTURE);
}
else if (options[item].equals("Choose from Gallery"))
{
Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent,REQUEST_IMAGE_SELECT);
}
else if (options[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
Bitmap bmp;
Bundle bundle = data.getExtras();
bmp = (Bitmap)bundle.get("data");
vitb.setImageBitmap(bmp);
}
else if (requestCode == 2) {
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 = findViewById(R.id.imageView);
vitb.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
}
}
MainActivity.java
public class MainActivity extends Activity{
private static final int REQUEST_CODE1 = 101;
private static final int REQUEST_CODE2 = 102;
Button btnLoc, btnPic;
Button btnSubmit;
TextView tvLoc;
ImageView image;
String phoneNumber = "9000000000";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSubmit = findViewById(R.id.btnSubmit);
btnLoc = findViewById(R.id.btnLoc);
btnPic = findViewById(R.id.btnPic);
tvLoc = findViewById(R.id.textView3);
image = findViewById(R.id.img);
btnSubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
try {
SmsManager.getDefault().sendTextMessage(phoneNumber, null, "Hello!", null, null);
}
catch (Exception e) {
AlertDialog.Builder alertDialogBuilder = new
AlertDialog.Builder(MainActivity.this);
AlertDialog dialog = alertDialogBuilder.create();
dialog.setMessage(e.getMessage());
dialog.show();
}
}
});
btnLoc.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,LocActivity.class);
startActivityForResult(intent,REQUEST_CODE1);
}
}
);
btnPic.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,PhotoActivity.class);
startActivityForResult(intent,REQUEST_CODE2);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK){
if(requestCode == REQUEST_CODE1 && data !=null) {
String strMessage = data.getStringExtra("loc");
tvLoc.setText(strMessage);
}
if(requestCode == REQUEST_CODE2 && data !=null) {
Bundle extras = getIntent().getExtras();
byte[] b = extras.getByteArray("PICTURE");
Bitmap bmp = BitmapFactory.decodeByteArray(b, 0, b.length);
image.setImageBitmap(bmp);
}
}
}
}
I have this button which should play the video on another activity or to be specific in Main2Activity. The problem is that the video wouldn't play when I click the button. Everything is working fine, the only problem I have is when I click that said button.
MainActivity:
#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);
}
});
}
}
Main2Activity:
public class Main2Activity extends Activity {
Button buttonPlay;
Button buttonFullScreen;
static final int REQUEST_VIDEO_CAPTURE = 1;
VideoView resultvideo;
MediaController mediacontroller;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String uri = getIntent().getStringExtra("VIDEO_URI");
Uri videoUri = Uri.parse(uri);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main2);
resultvideo = (VideoView)findViewById(R.id.videoView2);
resultvideo.start();
}
}
you will need to add
resultvideo.setVideoURI(videoUri);
after you initialed the resultvideo.
I have been trying to select an image from the gallery and then set an imageview on Android by using the following code, but when I select an image, it goes back to the "select an image screen" (i.e. the first screen that comes up after the intent starts) and after about 3 seconds, the screen turns black. What's odd is that no errors are called, and about 1 in every 10 attempts, it actually works. I am running 5.0.1 Lollipop on a Nexus 6, and I have figured out that onActivityResult is not being called whenever the screen goes black. Thanks for any help,
Jacob
Here is my code:
public static int GAN = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_funky);
Button a = (Button) findViewById(R.id.button);
ImageView b = (ImageView) findViewById(R.id.imageView5);
a.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, GAN);
System.out.println("hello world");
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
System.out.println("hello world");
if (requestCode == GAN && 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.imageView5);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
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();
}
});
}
}