Facebook image sharing working in emulator but not in device - android

Now I am working on a Android application. In my app i have to submit a image from my gallery to facebook.I used the following code.
if (item == 1) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_FILE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode != RESULT_OK) return;
Bitmap bitmap = null;
if (requestCode == PICK_FROM_FILE) {
mImageCaptureUri = data.getData();
mPath = getRealPathFromURI(mImageCaptureUri); //from Gallery
if (mPath == null){
mPath = mImageCaptureUri.getPath(); //from File Manager
}
if (mPath != null) {
System.out.println("mpath is not null"+mPath);
bitmap = BitmapFactory.decodeFile(mPath);
}
}
}
private void postToFacebook(String desc){
AsyncFacebookRunner mAsyncFbRunner = new AsyncFacebookRunner(mFacebook);
Bundle params = new Bundle();
try {
params.putByteArray("photo",
Utility.scaleImage(getApplicationContext(),mImageCaptureUri));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
params.putString("caption", desc);
mAsyncFbRunner.request("me/photos", params, "POST",
new PhotoUploadListener());
}
Its working fine for me in emulater but in real device image is not posting.But no crash.Please help me friends

Chk your manifest first, may this will help you.
android.permission.MANAGE_ACCOUNTS

Related

Why Camera activity returning null?

I used below codes for taking image from camera and put it in Image View (imgViewAds).
private void BtnPhoto_Click(object sender, EventArgs e)
{
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.SetVmPolicy(builder.Build());
Intent cameraIntent = new Intent(Android.Provider.MediaStore.ActionImageCapture);
var activities = PackageManager.QueryIntentActivities(cameraIntent, 0);
if (activities.Count > 0)
{
addAds.ImageName = Guid.NewGuid().ToString() + ".jpg";
Java.IO.File imageFile = new Java.IO.File(AdsAdapter.ImagePath(addAds.ImageName));
Android.Net.Uri imageUri = Android.Net.Uri.FromFile(imageFile);
cameraIntent.PutExtra(MediaStore.ExtraSizeLimit, 1024*10);
cameraIntent.PutExtra(MediaStore.ExtraOutput, imageUri);
StartActivityForResult(cameraIntent, 0);
}
else
{
Toast.MakeText(this, "Not Camera", ToastLength.Long).Show();
}
}
And here is OnActivityResult the the camare send result here.
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
if (resultCode == Result.Ok && (data != null))
{
Bundle extras = data.Extras;
Bitmap imageBitmap = (Bitmap)extras.Get("data");
imgViewAds.SetImageBitmap(imageBitmap);
MemoryStream stream = new MemoryStream();
imageBitmap.Compress(Bitmap.CompressFormat.Png, 0, stream);
addAds.AdsImage = stream.ToArray();
}
base.OnActivityResult(requestCode, resultCode, data);
}
but the data that sending to OnActivityResult is null and Image did not come to Image View.
When you pass EXTRA_OUTPUT with a URI to write to, the camera intent will be null and the picture is in the URI that you passed in.
so you could simply remove these two lines:
Android.Net.Uri imageUri = Android.Net.Uri.FromFile(imageFile);
cameraIntent.PutExtra(MediaStore.ExtraOutput, imageUri);
note that in this way you get the thumbnail of the image. so if you want the whole image you could use something like this (I haven't tested the code, but you could get the idea):
if (requestCode == CAMERA_REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
addAds.ImageName = Guid.NewGuid().ToString() + ".jpg";
Java.IO.File imageFile = new Java.IO.File(AdsAdapter.ImagePath(addAds.ImageName));
Uri uri = Uri.fromFile(file);
Bitmap bitmap;
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
bitmap = cropAndScale(bitmap, 300); // if you mind scaling
profileImageView.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

How to Upload Image from Camera to Server instead of image chooser

How can i upload images from camera to my web server ,
I was able to upload using image chooser like in this tuto
https://www.simplifiedcoding.net/android-upload-image-to-server/
, but i would like to choose images from camera and galery instead
public void uploadMultipart() {
//getting the actual path of the image
String path = getPath(filePath);
//Uploading code
try {
String uploadId = UUID.randomUUID().toString();
//Creating a multi part request
new MultipartUploadRequest(this, uploadId, Constants.UPLOAD_URL)
.addFileToUpload(path, "image") //Adding file
.addParameter("name", name) //Adding text parameter to the request
.setNotificationConfig(new UploadNotificationConfig())
.setMaxRetries(2)
.startUpload(); //Starting the upload
} catch (Exception exc) {
Toast.makeText(this, exc.getMessage(), Toast.LENGTH_SHORT).show();
}
}
//method to show file chooser
private void showFileChooser() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}
//handling the image chooser activity result
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
filePath = data.getData();
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
imageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
I also found this method , but it didn't work for me
How can replace the file chooser method in the above code properly to get the image path and name , and then upload it as in the tuto
private void selectImage() {
final CharSequence[] options = { "Take Photo", "Choose from Gallery","Cancel" };
android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(UploadImageTest.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);
File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
startActivityForResult(intent, 1);
}
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, 2);
}
else if (options[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
For Uploading a captured image to the server the whole your code remains the same you just need to pass your captured image file path into your uploadMultipart function.
You will get captured image file path in your onActivityResult.

onbackpressed in show file chooser

private void showFileChooser() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}
hey. i used that code for pick image from storage. but if i press the back button in my gadget. he is fc . how to give condition on canceled pick from storage..and not force close?
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST) {
filePath = data.getData();
if(filePath != null) {
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), filePath);
imageView.setImageBitmap(bitmap);
Toast.makeText(this, data.getDataString(), Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
}else if(filePath == null){
startActivity(new Intent(this,HalamanUser.class));
}
} else if (requestCode == CAMERA_REQUEST) {
Log.i("hello", "REQUEST cALL");
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), filePath);
imageView.setImageBitmap(bitmap);
} catch (Exception e) {
Log.i("hello", "Exception" + e.getMessage());
}
}else {
startActivity(new Intent(this,HalamanUser.class));
}
}
When the user presses back, the result is RESULT_CANCELED, and the data that you receive is null. So the app crashes when you call data.getData(), as you are calling getData() on a null object. The are a few ways to get around this: you can check what the resultCode is, and make sure it's RESULT_OK. You can also simply check whether the data Intent is null before trying to get the data from it:
if (requestCode == PICK_IMAGE_REQUEST) {
if (data != null) {
filePath = data.getData();
} else {
// Note: if filePath is by default null, you don't need this else statement
filePath = null;
}
if (filePath != null) {
...

Image saving issue in marshmallow devices

Aim :my application take images from camera and gallery.And crop this image and save to external storage.
Issue : Application shows unfortunately photos has stooped message while clicking save button from crop page.The error happens only in marshmallow devices.working fine in all other devices.
my code to take image is given below:
final CharSequence[] options = {"Take Photo", "Choose from Gallery"};
AlertDialog.Builder builder = new AlertDialog.Builder(CategoryActivity.this);
builder.setTitle("Select Pic Using...");
builder.setItems(options, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int item) {
if (options[item].equals("Take Photo")) {
try {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
pic = new File(Environment.getExternalStorageDirectory(),
"tmp_" + String.valueOf(System.currentTimeMillis()) + ".jpg");
picUri = Uri.fromFile(pic);
cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, picUri);
cameraIntent.putExtra("return-data", true);
startActivityForResult(cameraIntent, LOAD_IMAGE_CAMERA);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
} 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.createChooser(intent, "Select Picture"), LOAD_IMAGE_GALLARY);
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean("profile", false);
editor.commit();
// finish();
}
}
});
builder.show();
}
});
if (requestCode == LOAD_IMAGE_CAMERA && resultCode == RESULT_OK) {
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean("profile", false);
editor.commit();
Intent in = new Intent(CategoryActivity.this, ImageCrop.class);
in.putExtra("URI", picUri);
in.putExtra("cat", type);
in.putExtra("contest_idadd", contestid_i);
startActivity(in);
} else if (requestCode == LOAD_IMAGE_GALLARY) {
if (data != null) {
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean("profile", false);
editor.commit();
picUri = data.getData();
Intent in = new Intent(CategoryActivity.this, ImageCrop.class);
in.putExtra("URI", picUri);
in.putExtra("cat", type);
in.putExtra("contest_idadd", contestid_i);
startActivity(in);
}
}
And crop using the below code
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
picUri = (Uri) bundle.get("URI");
cat_value = (String) bundle.get("cat");
}
protected void CropImage() {
try {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(picUri, "image/*");
Log.d("piccccc",picUri+"");
intent.putExtra("crop", "true");
intent.putExtra("outputX", 200);
intent.putExtra("outputY", 200);
intent.putExtra("aspectX", 3);
intent.putExtra("aspectY", 3);
intent.putExtra("scaleUpIfNeeded", true);
intent.putExtra("return-data", true);
startActivityForResult(intent, CROP_IMAGE);
} catch (ActivityNotFoundException e) {
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CROP_IMAGE) {
if (data != null) {
// get the returned data
Bundle extras = data.getExtras();
// get the cropped bitmap
Bitmap photo = extras.getParcelable("data");
image_array.add(photo);
crop.setImageBitmap(photo);
if (pic != null) {
if (pic.delete()) {
}
}
}
else
{
Log.d("cropppppppppppppp", requestCode + "");
}
}
In marshmallow devices images not found in saved external storage path.Anyone please give me a solution.
Add proper implementation of taking
android.permission.WRITE_EXTERNAL_STORAGE
permission in your code.
You need to handle permissions in your code from marshmallow and above:
http://developer.android.com/training/permissions/index.html
http://developer.android.com/training/permissions/requesting.html
Or build your app with SDK 22 or below, TargetSdkVersion = 22

Why the camera intent do not return after photo taken

I am trying to write a function that allow the user select to open a camera , then when the user take one photo, it return , get the scaled photo and set that bitmap on the imageView. The problem is either the default camera or other camera apps on the market will not fire the return case after I take a pic , how to fix that ? Thanks
Take picture button:
takePic.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
File tempFile;
try {
tempFile = File.createTempFile("my_app", ".jpg");
fileName = tempFile.getAbsolutePath();
Uri uri = Uri.fromFile(tempFile);
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
After intent result
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_GET_IMAGE_ALBUM && resultCode == RESULT_OK) {
Uri selectedImage = data.getData();
Intent shareIntent = new Intent(this, SharePicForm.class);
shareIntent.putExtra("photo", Utility.getImagePath(selectedImage,this));
startActivity(shareIntent);
} else if ( requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Log.d("test1","return");
Intent shareIntent = new Intent(this, SharePicForm.class);
shareIntent.putExtra("photo", data.getStringExtra(MediaStore.EXTRA_OUTPUT));
startActivity(shareIntent);
}
}

Categories

Resources