I am creating gallery using HorizontalScrollView, and I want to click the image in HorizontalScrollView.
Here is my code:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK){
if(data.getClipData() != null){
int totalItemsSelected = data.getClipData().getItemCount();
linearLayout = (LinearLayout) findViewById(R.id.linear);
for(int i = 0; i < totalItemsSelected; i++){
Uri fileUri = data.getClipData().getItemAt(i).getUri();
ImageView image = new ImageView(this);
image.setAdjustViewBounds(true);
image.setImageURI(fileUri);
linearLayout.addView(image);
}
}
}
}
Hi use the below code to implement click event to ImageView:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK){
if(data.getClipData() != null){
int totalItemsSelected = data.getClipData().getItemCount();
linearLayout = (LinearLayout) findViewById(R.id.linear);
for(int i = 0; i < totalItemsSelected; i++){
Uri fileUri = data.getClipData().getItemAt(i).getUri();
ImageView image = new ImageView(this);
image.setAdjustViewBounds(true);
image.setImageURI(fileUri);
//click event
image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.e(TAG,"Selected File URI : "+fileUri);
}
});
linearLayout.addView(image);
}
}
}
}
Set onClickListener in loop. So every ImageView will have it's own click listener. Before adding image into layout.
image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// do ur work here
}
})
Thanks
See below:
for(int i = 0; i < totalItemsSelected; i++){
Uri fileUri = data.getClipData().getItemAt(i).getUri();
ImageView image = new ImageView(this);
image.setAdjustViewBounds(true);
image.setImageURI(fileUri);
image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//what you want to do
}
});
linearLayout.addView(image);
}
Related
I am developing an app which takes a picture with the camera and allows the user to share it. But the share intent isn't working. I have checked but none are working on my device. I am running the app on an Android 6.0 marshmallow tablet.
Here is my code
public class SharePic extends Activity {
static final int REQUEST_IMAGE_CAPTURE = 1;
ImageView picView;
Button shareBtn;
Bitmap imageBitmap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_share_pic);
picView = (ImageView) findViewById(R.id.picView);
shareBtn = (Button) findViewById(R.id.shareBtn);
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
imageBitmap = (Bitmap) extras.get("data");
picView.setImageBitmap(imageBitmap);
}
}
public void share(View view){
String bitmapPath = MediaStore.Images.Media.insertImage(getContentResolver(), imageBitmap,"title", null);
Uri bitmapUri = Uri.parse(bitmapPath);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/png");
intent.putExtra(Intent.EXTRA_STREAM, bitmapUri);
startActivity(Intent.createChooser(intent , "Share"));
}
}
Any help?
So, when is the share button being called.
I guess you might have to set event listener setOnClickListener on share button click in case you haven't set it in the XML
shareBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
share(v);
}
});
I just need help on my codes, I'm using Android Studio. Everything is working fine but the recorded video won't show in viewVideo on my layout.
Here's the code:
public class MainActivity extends AppCompatActivity {
static final int REQUEST_VIDEO_CAPTURE = 1;
VideoView resultvideo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button click = (Button)findViewById(R.id.videorec);
resultvideo = (VideoView)findViewById(R.id.videoView);
}
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) {
if (requestCode == REQUEST_VIDEO_CAPTURE && resultCode == RESULT_OK) {
Uri videoUri = data.getData();
resultvideo.setVideoURI(videoUri);
}
}
}
Uri videoUri = data.getData();
resultvideo.setVideoURI(videoUri);
// start call missing
resultvideo.start();
I would like to know how to put a play button on the viewVideo on my layout. Also do I have to put something on my layout for it to work?
Here's my code:
public class MainActivity extends AppCompatActivity {
static final int REQUEST_VIDEO_CAPTURE = 1;
VideoView resultvideo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button click = (Button)findViewById(R.id.videorec);
resultvideo = (VideoView)findViewById(R.id.videoView);
}
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) {
if (requestCode == REQUEST_VIDEO_CAPTURE && resultCode == RESULT_OK) {
Uri videoUri = data.getData();
resultvideo.setVideoURI(videoUri);
resultvideo.start();
}
}
}
Use FrameLayout.
Specify a drawable you can see in FrameLayout children. and use Foreground Drawable to draw the button with a drawable:
myFrameLayout.setForegroundDrawable(myPlayDrawable);
I'm starting DialogFragment from AddFragment by calling showPickImageDialog() method on button press. And in AlertDialogFragment onActivityResult I'm sending results back to AddFragment but AddFragment is not receiving any results. Any ideas why?
AddFragment:
public class AddFragment extends Fragment {
private static final int REQUEST_IMAGE = 3;
private static final String DIALOG_IMAGE = "image";
private static final String TAG = "TAG";
void showPickImageDialog() {
FragmentManager fm = getActivity()
.getSupportFragmentManager();
AlertDialogFragment pdialog = new AlertDialogFragment();
pdialog.setTargetFragment(this, REQUEST_IMAGE);
pdialog.show(fm, DIALOG_IMAGE);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_IMAGE && resultCode == Activity.RESULT_OK){
Log.e(TAG,"back in AddFragment");
}
}
}
In AlertDialogFragment Im calling sendResult method from onActivityResult method. SendResult method is called I checked it with Logs
AlertDialogFragment:
public class AlertDialogFragment extends DialogFragment {
private final static String TAG = "Cload";
public static final String EXTRA_PHOTOFILENAME =
"com.example.tadas.dreamcload1.image";
private static final int REQUEST_IMAGE = 3;
private String filename;
private Uri uri;
private void sendResult(int resultCode) {
if(getTargetFragment() == null){return;}
Intent i = new Intent();
i.putExtra(EXTRA_PHOTOFILENAME,filename);
getTargetFragment()
.onActivityResult(getTargetRequestCode(), resultCode, i);
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
//LayoutInflater inflater = getLayoutInflater();
// View dialogLaout = inflater.inflate(R.layout.custom_dialog_pick_image,null);
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.custom_dialog_pick_image, null);
final AlertDialog dialog = new AlertDialog.Builder(getActivity())
.setView(view)
.create();
ImageButton gallerybtn = (ImageButton)view.findViewById(R.id.gallery);
ImageButton camerabtn = (ImageButton)view.findViewById(R.id.camera);
gallerybtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.e(TAG,"gallery btn pressed");
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, REQUEST_IMAGE);
}
});
camerabtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
return dialog;
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_IMAGE && resultCode == Activity.RESULT_OK){
uri = data.getData();
final String[] filePathColumn = {MediaStore.MediaColumns.DATA, MediaStore.MediaColumns.DISPLAY_NAME};
Cursor c = getActivity().getContentResolver()
.query(uri,filePathColumn,null,null,null);
if (c.moveToFirst()){
int ids = c.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
uri = Uri.parse(c.getString(ids));
filename = uri.toString();
c.close();
}
sendResult(Activity.RESULT_OK);
dismiss();
}
}
You can override your onActivityResult method in your Activity and call your Fragment's onActivityResult manually like below:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.your_container);
fragment.onActivityResult(requestCode, resultCode, data);
}
You can define this part in your Activity's onActivityResult method:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_IMAGE && resultCode == Activity.RESULT_OK){
uri = data.getData();
final String[] filePathColumn = {MediaStore.MediaColumns.DATA, MediaStore.MediaColumns.DISPLAY_NAME};
Cursor c = getActivity().getContentResolver()
.query(uri,filePathColumn,null,null,null);
if (c.moveToFirst()){
int ids = c.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
uri = Uri.parse(c.getString(ids));
filename = uri.toString();
c.close();
}
// You do not need this any more
//sendResult(Activity.RESULT_OK);
//dismiss();
// Just find your fragment and call it's onActivityResult
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.your_container);
fragment.onActivityResult(requestCode, resultCode, data);
}
}
I think this change'll help you.
I'm working on an android app that produces effects in images. I want the users of my app to have two options.
1) They can choose pictures from Gallery
2) They can capture a new photo from camera
Here is how I'm accomplishing the aforementioned two tasks:
Button takePhotoFromCameraButton;
Button chooseFromGalleryButton;
String imagePath = Environment.getExternalStorageDirectory().getAbsolutePath() + System.currentTimeMillis() + "_image.jpg";
File imageFile = new File(imagePath);
imageUri = Uri.fromFile(imageFile);
In both buttons, I'm passing the same onClickListner.
#Override
public void onClick(View clickedView) {
int clickedViewId = clickedView.getId();
switch(clickedViewId) {
case R.id.takeFromCamera:
Intent imageCaptureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
imageCaptureIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(imageCaptureIntent,0);
break;
case R.id.chooseFromGallery:
Intent choosePictureIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(choosePictureIntent, 1);
break;
default:
// As we have only two buttons, and nothing else can be clicked except the buttons. So no need to put
// code in the "DEFAULT CASE"
}
}
And I'm capturing the result from both, the following way:
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
switch(requestCode) {
case 0:
Intent cameraIntent = new Intent(MainOptionsActivity.this,ApplyEffectsActivity.class);
cameraIntent.putExtra("imageFileUri", imageUri);
startActivity(cameraIntent);
break;
case 1:
Uri imageUriForGallery = intent.getData();
Intent galleryIntent = new Intent(MainOptionsActivity.this,ApplyEffectsActivity.class);
galleryIntent.putExtra("imageFileUri", imageUriForGallery);
startActivity(galleryIntent);
break;
}
}
}
The button for gallery images works fine. But when I invoke camera by pressing the second button and capture the image, nothing happens. It stays there till I cancel the camera and come back to my app. I don't receive any image back!
Where I'm wrong? Don't mind this silly question, I'm just a beginner in android! :(
When I implemented mine, I made it work this way through a fragment:
public class ConcertFragment extends Fragment implements SurfaceHolder.Callback {
ImageView toto;
ToggleButton btnFlashlight;
RayMenu menu;
View rootView;
private Camera cam;
boolean hasCamera;
Parameters params;
private int REQUEST_IMAGE_CAPTURE;
Bitmap photo;
Uri imageUri;
public ConcertFragment() {
}
public void startCameraIntent() {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, REQUEST_IMAGE_CAPTURE);
}
#Override
public void onStart() {
super.onStart();
SurfaceView preview = (SurfaceView)getView().findViewById(R.id.background);
SurfaceHolder mHolder = preview.getHolder();
mHolder.addCallback(this);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.concert, menu);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getCamera();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_concert, container, false);
toto = (ImageView) rootView.findViewById(R.id.toto);
return rootView;
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode != 0)
{
if (requestCode == REQUEST_IMAGE_CAPTURE) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
toto.setImageBitmap(photo);
View content = rootView.findViewById(R.id.toto);
content.setDrawingCacheEnabled(true);
Bitmap bitmap = content.getDrawingCache();
File root = Environment.getExternalStorageDirectory();
File cachePath = new File(root.getAbsolutePath() + "/DCIM/Camera/image.jpg");
try {
cachePath.createNewFile();
FileOutputStream ostream = new FileOutputStream(cachePath);
bitmap.compress(CompressFormat.JPEG, 100, ostream);
ostream.close();
} catch (Exception e) {
e.printStackTrace();
}
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(cachePath));
startActivity(Intent.createChooser(share,"Share via"));
}
else {
cam.release();
}
}
}
// Get the camera
private void getCamera() {
if (cam != null) {
try {
cam = Camera.open();
params = cam.getParameters();
cam.startPreview();
hasCamera = true;
} catch (RuntimeException e) {
Log.e("Camera Error. Failed to Open. Error: ", e.getMessage());
cam.release();
}
}
}
Hope this helps :) Just ask if you need more information.
How to do it with an activity, instead of fragment
public class MyCameraActivity extends Activity {
private static final int CAMERA_REQUEST = 1888;
private ImageView imageView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.imageView = (ImageView)this.findViewById(R.id.imageView1);
Button photoButton = (Button) this.findViewById(R.id.button1);
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);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
}