Why is my code not functioning? i want to transfer image from one activity to another. Please help! Note: I have create a camera function and thats where I get image.
This is the MainActivity.java
public class MainActivity extends AppCompatActivity {
public static final int CAMERA_REQUEST = 9999;
ImageView imageView1;
Button camera,send;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView1 = findViewById(R.id.imageView1);
camera = findViewById(R.id.camera);
send = findViewById(R.id.send);
camera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent d1 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(d1, CAMERA_REQUEST);
}
});
send.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
imageView1.buildDrawingCache();
Bitmap bitmap = imageView1.getDrawingCache();
Intent intent = new Intent(MainActivity.this, ResultActivity.class);
intent.putExtra("bp", bitmap);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
imageView1.setImageBitmap(bitmap);
}
}
This is the ResultActivity.java
public class ResultActivity extends AppCompatActivity {
ImageView imageView2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
imageView2 = findViewById(R.id.imageView2);
Intent intent = new Intent();
Bitmap bitmap = intent.getParcelableExtra("bp");
imageView2.setImageBitmap(bitmap);
}
}
If I've done something wrong. Please let me know. Thankssss
Youre already receiving the bitmap as a Uri in the intent received in 'OnActivityResult'.
All you have to do is to use this intent to move to the second activity.
Then from there you can convert it to bitmap:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Intent intent = new Intent(MainActivity.this, ResultActivity.class);
intent.putExtra("Some key",data.getData().toString());
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
imageView1.setImageBitmap(bitmap);
}
}
On ResultActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
Uri uri = Uri.parse(getIntent.getExtra.getString("Some key"));
imageView2 = findViewById(R.id.imageView2);
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
imageView2.setImageBitmap(bitmap);
}
}
Related
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'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 ) {
...
}
}
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.
In my code, camera should take photo and save it in ImageView. It works good until saving picture to the ImageView. It displays nothing!!
There are my errors:
And here is my code:
public class MainActivity extends AppCompatActivity {
static final int REQUESTED_IMAGE_CAPTURE=1;
ImageView output;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button shot=(Button)findViewById(R.id.button);
output=(ImageView)findViewById(R.id.imageView);
if(!hasCamera())
shot.setEnabled(false);
}
private boolean hasCamera(){
return getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY);
}
public void lanch_camera(View view){
Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,REQUESTED_IMAGE_CAPTURE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode==REQUESTED_IMAGE_CAPTURE && resultCode ==RESULT_OK){
Bundle extras=data.getExtras();
Bitmap pic=(Bitmap) extras.get("data");
output.setImageBitmap(pic);
}
}
}
Please help me. Thanks.
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();
}
});
}
}