How to make user pick picture? [duplicate] - android

This question already has answers here:
Get/pick an image from Android's built-in Gallery app programmatically
(19 answers)
Closed 7 years ago.
Okay, so i'm very new to this android development.
I have a button. i want this button to open the pick image from... kinda activity so the user can pick image from system or different gallery apps he has or camera or web. like other apps who do so. I want that the image selected will get the image id and store it to a database, and display the image as the button. I will do the storing, but how do i get image ID? and all the other stuff? thanks alot for anyone who helps me! Cheers!
Add request code:
public class add_requests extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_requests);
}
Globals mThumbIds = Globals.getInstance();
public void onSaveClick(View view){
EditText desc = (EditText) findViewById(R.id.request_name);
EditText message = (EditText) findViewById(R.id.request_message);
String message1 = message.getText().toString();
String desc1 = desc.getText().toString();
Request r1 = new Request(R.drawable.iphone, message1,desc1);
mThumbIds.getData().add(r1);
finish();
}
public void browsePictures(){
Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
}
}

Following is sample to pick and use picture from Gallery.
public void pickImage() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, PICK_PHOTO_FOR_AVATAR);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_PHOTO_FOR_AVATAR && resultCode == Activity.RESULT_OK) {
if (data == null) {
//Display an error
return;
}
InputStream inputStream = context.getContentResolver().openInputStream(data.getData());
//Now you can do whatever you want with your inpustream, save it as file, upload to a server, decode a bitmap...
}
}

Related

How can I send back the captured image taken from second activity back to the main activity?

This is my code from the main activity class named Account class wherein when I press the add entry button it will navigate to the next activity which is the AddEntry class
//ADD ENTRY BUTTON
accountBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(Account.this, AddEntry.class);
startActivityForResult(intent,REQUEST_CODE_ADD);
}
});
After it goes to the second activity which is the AddEntry class, a picture would be captured using the user's camera when the ImageView named entryPhoto is clicked
entryPhoto.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent takePhoto = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File tempImage = null;
try {
tempImage = createImage();
} catch (Exception e) {
e.printStackTrace();
}
if (tempImage != null){
Uri uriImage = FileProvider.getUriForFile(c,"com.example.login.fileprovider",
tempImage);
mCurrentPhotoUri = uriImage;
takePhoto.putExtra(MediaStore.EXTRA_OUTPUT,uriImage);
if (ContextCompat.checkSelfPermission(c, Manifest.permission.CAMERA)!= PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(AddEntry.this,
new String[]{Manifest.permission.CAMERA},
REQ_CODE_CAMERA);
}
else {
startActivityForResult(takePhoto,REQ_CODE_TAKE_PHOTO);
}
}
}
});
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQ_CODE_TAKE_PHOTO && resultCode == RESULT_OK) {
entryPhoto.setImageURI(mCurrentPhotoUri);
}
Here is the code where I put all the the information inputted by user then send it back to the main activity. In the addPhoto is where I am supposed to put the image to be passed. I only tried passing an image from my drawable because I really don't know how to pass the image captured.
Intent data = new Intent();
data.putExtra("addPhoto",R.drawable.anonymous);
data.putExtra("addName", entryName.getText().toString());
data.putExtra("addRemark", entryRemark.getText().toString());
data.putExtra("addBirthday", entryBirthday.getText().toString());
data.putExtra("addAddress", entryAddress.getText().toString());
data.putExtra("addGender", selectedGender);
data.putExtra("addContactNo", entryContactNo.getText().toString());
data.putExtra("addHobbies", entryHobbies.getText().toString());
data.putExtra("addOtherInfo", entryOtherInfo.getText().toString());
setResult(RESULT_OK,data);
finish();
And this is the code in the onActivityResult in the main activity
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data); //ONACTRESULT OF ADD ENTRY
if (requestCode == REQUEST_CODE_ADD && resultCode == RESULT_OK){
int addPhoto = data.getIntExtra("addPhoto",1);
String addName = data.getStringExtra("addName");
String addRemark = data.getStringExtra("addRemark");
String addBirthday = data.getStringExtra("addBirthday");
String addAddress = data.getStringExtra("addAddress");
String addContactNo = data.getStringExtra("addContactNo");
String addGender = data.getStringExtra("addGender");
String addHobbies = data.getStringExtra("addHobbies");
String addOtherInfo = data.getStringExtra("addOtherInfo");
entryList.add(0,new Entry(addPhoto,addName,addRemark,addBirthday,addGender,addAddress,addContactNo,addHobbies,addOtherInfo));
myAdapter.notifyDataSetChanged();
}
Please I hope you will help me. I am still a beginner but I really want to be good at programming. Thank you in advance.
I think you can pass the Uri directly in bundle of Intent as it extends Parcelable which means it can retrieved as is
Adding
data.putExtra("addPhoto", mCurrentPhotoUri);
Retrieving
Uri photoUri = getIntent().getParcelableExtra<Uri>("addPhoto");

How to add Internal and External storage to File Chooser in android

My sample app is open file chooser and select file / directory then getting the path into EditText, I tried the methods in this question and I reached to this result
public class MainActivity extends AppCompatActivity {
private Button browse;
private EditText editTextPath;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
browse = (Button) findViewById(R.id.browse);
editTextPath = (EditText) findViewById(R.id.path);
browse.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(Intent.createChooser(intent,"Select file or dir"), 1);
setResult(Activity.RESULT_OK);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == Activity.RESULT_OK) {
String Fpath = data.getDataString();
editTextPath.setText(Fpath);
}
}
}
I want to add internal and external storage like this to file chooser
The user can tap the "..." affordance in the action bar and choose "Show internal storage" to display those options.
There is no official support for anything on ACTION_GET_CONTENT or ACTION_OPEN_DOCUMENT to show internal storage options automatically, though hopefully this will be supported someday.
you must use Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
insted Intent intent = new Intent(Intent.ACTION_GET_CONTENT );

Photo Rotated After Capture in Portrait Mode

I am attempting to take a captured image and display to the Activity. When the image is set, it gets rotated to the right 90 degrees. I'm not sure what I am doing wrong. I capture the image by using MediaStore.ACTION_IMAGE_CAPTURE and saved it and set it with a URI. Here's the code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_selectusername);
confirm = (Button) findViewById(R.id.confirm_select);
confirm.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
takePhoto();
}
});
}
private void takePhoto() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File photo = new File(Environment.getExternalStorageDirectory(), "temp.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
//This is for photo editing purposes
inputURI = Uri.fromFile(photo);
outputURI = Uri.fromFile(new File(getCacheDir(), photoname));
//Cropping, Resizing, etc. takes place
startActivityForResult(intent, TAKE_PICTURE);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent result) {
super.onActivityResult(requestCode, resultCode, result);
if(requestCode == TAKE_PICTURE && resultCode == RESULT_OK){
ImageView imageView = (ImageView) findViewById(R.id.profilepicture_select);
imageView.setImageURI(outputURI);
}else{
Log.d("TEST", "Something went wrong!");
}
}
I can show more code and clarify if necessary. All Help is appreciated!
Your code looks fine. It's likely a problem with the device's camera. This is a known issue for the Galaxy S4. Some devices output strange exif data which results in the image being off when you display it. How do you fix it?
Here's a lead for you:
Android camera/picture orientation issues with Samsung Galaxy S3, S4, S5

How to show file chooser when button is clicked?

I am trying to upload image in my app and i want to show some choices when my upload button is clicked. Unfortunately it gives me this error. I dont know what is wrong with the code. I got this from hereTrying open a specific folder in android using intent
thanks in advance!
uploadpic.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
openFolder();
}
});
}
public void openFolder()
{
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()
+ "/Pictures/");
intent.setDataAndType(uri, "images");
startActivity(Intent.createChooser(intent, "Open folder"));
takephoto.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
dispatchTakePictureIntent();
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
profpic.setImageBitmap(imageBitmap);
}
}
static final int REQUEST_IMAGE_CAPTURE = 1;
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
You only want the user to select an image, am I right? Then maybe a general file picker isn't whats best suited for your needs.
A quick google search came up with this (also from SO). It seems like that solution is limited to pictures on an SD card.
If you don't want to click the link:
This is how you would start an intent in order to get an image.
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
You'll then need to override onActivityResult(), but this is all described in the link.

Access to QR code coordinate in ZXing on Android

I'm developing a ZXing project in Eclipse:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View view)
{
Log.d("test", "button works!");
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("com.google.zxing.client.android.SCAN.SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
b1 = (Button)findViewById(R.id.button111);
}
public void onActivityResult(int requestCode, int resultCode, Intent intent)
{
if (requestCode == 0)
{
if (resultCode == RESULT_OK)
{
String contents = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
Log.i("xZing", "contents: "+contents+" format: "+format);
// Handle successful scan
}
else if (resultCode == RESULT_CANCELED)
{
// Handle cancel
Log.i("xZing", "Cancelled");
}
}
I found the finder pattern coordinates are in the Detector class (getTopLeft and getTopRight and getBottomLeft) but when I want to access them I'm facee with errors in the code. I don't know how I can access these variables. For example, I define a public static FinderPattern topLeft1 in the Detector class and in the processFinderPatternInfo method I add topLeft1=topLeft.
After startActivity for the result I add:
float m1=Detector.topLeft1.x;
m2=Detector.topLeft1.y;
b.setText("(x,y)="+m1+","+m2);
but when I run the program and click the button to scan: I get:
unfortunately zxing has stopped
I need 10 reputation to post an image and I can't post logcat image :-(
Can any one help me soon?
Thanks a lot
I solved my problem.I must write OnActivityResult class out of OnCreate Method! That's all!
thank you all.

Categories

Resources