Android, how to Share an image file with messenger - android

Hello evrybody i'm tryng to share an image on messenger but i don't know why my code doesen't work, I've followed the official guide, https://developers.facebook.com/docs/messenger/android
someone can told me why doese'nt work?
public void sendMessage(){
Bitmap adv= takePic(HomeActivity.livelloCurrent.getNumeroLivello());
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
adv.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File f = new File(Environment.getExternalStorageDirectory()+ File.separator + "temporary_file.jpg");
try {
f.createNewFile();
new FileOutputStream(f).write(bytes.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
String mimeType = "image/jpeg";
Intent sendIntent = new Intent();
sendIntent.setType(mimeType);
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_STREAM,Uri.parse(Environment.getExternalStorageDirectory() + File.separator + "temporary_file.jpg"));
sendIntent.putExtra(Intent.EXTRA_TEXT, "<---MY TEXT--->.");
sendIntent.setPackage("com.facebook.orca");
try {
startActivity(sendIntent);
}
catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(getApplicationContext(),"Please Install Facebook Messenger", Toast.LENGTH_LONG).show();
}
/** //withSDK-->// ShareToMessengerParams shareToMessengerParams = ShareToMessengerParams.newBuilder(ContentUri, mimeType).build();
MessengerUtils.shareToMessenger(this, REQUEST_CODE_SHARE_TO_MESSENGER, shareToMessengerParams);**/
}
Im sure that the file creation work cause i have tested it . in testing I get the following error from messenger "Sorry, Messenger was not able to process the file".
how can i solve ?

Replace:
Uri.parse(Environment.getExternalStorageDirectory() + File.separator + "temporary_file.jpg")
with:
Uri.fromFile(f)

Related

NoFileSpecified error when trying to upload a file to OneDrive

I'm using Android and I am following this tutorial:
https://github.com/OneDrive/onedrive-picker-android/blob/master/README.md
public void uploadToOneDrive(String content,Context context){
final String filename = "Temp.xml";
final File f = new File(context.getFilesDir(), filename);
try {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput("Temp.xml", Context.MODE_PRIVATE));
outputStreamWriter.write(content);
outputStreamWriter.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
Log.e("Exception", "File write failed: " + e.toString());
}
System.out.println("File " + f.toString());
mSaver = Saver.createSaver(ONEDRIVE_APP_ID);
mSaver.startSaving((Activity)context,filename, Uri.fromFile(f));
}
This is my method that returns ERROR TYPE NoFileSpecified. I am trying to just create a new temporary file, put a string into it and then delete it.
This is how I call it:
oneDriveUpload.getSaver().handleSave(requestCode, resultCode, data);
05-29 15:03:18.649 10649-10649/david.projectclouds I/System.out: File /data/user/0/david.projectclouds/files/Temp.xml
05-29 15:03:18.702 10649-10649/david.projectclouds I/System.out: URI: file:///data/user/0/david.projectclouds/files/Temp.xml
So there is a file and the URI is something that is accepted. I'm lost.
EDIT: I've tried using content content://david.projectclouds.MainActivity/file/diabetix/Temp.xml
But I still have the same error.
There are apparently some issues with Android 7.0 and the SDK. So I just used my own intents.
Intent intentOD = new Intent(Intent.ACTION_SEND);
intentOD.setType("text/*");
intentOD.setPackage("com.microsoft.skydrive");
intentOD.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intentOD.putExtra(Intent.EXTRA_STREAM, FileProvider.getUriForFile(getContext(), "david.projectclouds.MainActivity", f));
getContext().startActivity(Intent.createChooser(intentOD, "title"));

Android open PDF : File not found

I try to open a PDF in my application.
First, I create the PDF like that :
String filename = Environment.getExternalStorageDirectory().toString()+"/mypdf.pdf";
File file = new File(filename);
try {
FileOutputStream bos = new FileOutputStream(file);
bos.write(Base64.decode(base64, 0));
bos.flush();
bos.close();
} catch (IOException e) {
Log.e(TAG, "IOError with PDF");
e.printStackTrace();
}
Intent intent = new Intent(this, PdfActivity.class);
intent.putExtra("file", filename);
startActivity(intent);
The file is well created and readable, I can open this with ESExplorer application.
This file is located in /storage/emulated/0/myfile.pdf
in the PdfActivity I try to open the PDF :
Bundle extras = getIntent().getExtras();
String url = extras.getString("file");
File file = new File(url);
try {
if (file.exists()) {
Uri path = Uri.parse(url);
Intent objIntent = new Intent(Intent.ACTION_VIEW);
objIntent.setDataAndType(path, "application/pdf");
objIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(objIntent);
} else {
Toast.makeText(this, "File NotFound", Toast.LENGTH_SHORT).show();
}
} catch (ActivityNotFoundException e) {
Toast.makeText(this, "No Viewer Application Found", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
file.exists() return true, Intent start, but my PDF reader says : "File not found"
I've added read and write permissions on external storage.
Does someone have any idea why it can't access to my file ?
objIntent.setDataAndType(url, "application/pdf");
use above line in your second snippest also declare String url as a global variable hope it will help you if it will not work try to use hardcode value of file path and see is it working or not ?
and are you sure file is exist? check this scenario tooo :)
I found the solution.
I've replace the Uri like that:
Uri path = Uri.fromFile(file);
And it works!

how to send images to gallery for setting the wallpaper?

i was trying to integrate set as wallpaper option using default gallery app, I don't know how to send the image to gallery using intent. I am attaching fb app samples how its look like.
String root = Environment.getExternalStorageDirectory().toString();
new File(root + "/"+Constants1.APPNAME).mkdirs();
File fileForImage = new File(root + "/"+Constants1.APPNAME, pos + ".jpg");
if (fileForImage.exists()) fileForImage.delete();
try {
FileOutputStream out = new FileOutputStream(fileForImage);
arg0.compress(Bitmap.CompressFormat.PNG, 100, out);
Toast.makeText(getApplicationContext(), "Image is saved to "+Constants1.APPNAME+" folder", Toast.LENGTH_SHORT).show();
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
Intent emailIntent = new Intent(Intent.ACTION_ATTACH);
emailIntent.setType("image/png");
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(fileForImage));
startActivityForResult(Intent.createChooser(emailIntent, pos+".jpg"),0);
The problem is that the image is not passing to the gallery..
Intent emailIntent = new Intent(Intent.ACTION_ATTACH_DATA);
emailIntent.setDataAndType(Uri.fromFile(fileForImage), "image/*");
//emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(fileForImage));
startActivity(emailIntent);
Toast.makeText(getApplicationContext(), "Image is saved to "+Constants1.APPNAME+" folder", Toast.LENGTH_SHORT).show();

Email apps cannot read the file I'm trying to attach

I write a file into the SD. I Know the file is OK because using ASTRO app or Gmail app or Yahoo app, I can see it and I can also attach it from them but when I try to attach the file from my app the thing is quite different. When I choose gmail or yahoo app they can't read the file. But if I choose the default app the email is sent correctly with the file attached.
Here is my code. THANKS!!
Inside AndroidManifest
uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
Where I make the file
File ruta_sd = Environment.getExternalStorageDirectory();
File f;
// Creo la carpeta;
File folder = new File(ruta_sd.getAbsolutePath() + "/Torno");
folder.mkdirs();
f = new File(ruta_sd.getAbsolutePath() + "/Torno/","Torno.xml");
// Just trying
f.canRead();
OutputStreamWriter fout = new OutputStreamWriter(new FileOutputStream(f));
fout.write(c); <-- c is a String in xml format
// Just trying
fout.flush();
fout.close();
Now, where I try to attach it
String ruta = Environment.getExternalStorageDirectory().getPath() + "/Torno/Torno.xml";
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType(getMimeType(ruta));
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(ruta));
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "el tema");
sendIntent.putExtra(Intent.EXTRA_TEXT, "el cuerpo del mensaje");
// Just trying
sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(sendIntent, "Title:"));`
Presumably there is something wrong with your path. Change:
String ruta = Environment.getExternalStorageDirectory().getPath() + "/Torno/Torno.xml";
...
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(ruta));
to:
File ruta = new File(Environment.getExternalStorageDirectory(), "/Torno/Torno.xml");
...
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(ruta));
and see if that helps (along with getting rid of FLAG_GRANT_READ_URI_PERMISSION, and perhaps trying sync()).
At the end, It worked!!! I don't really know what was happening. I was trying with my phone as emulator with eclipse without installing the app and the e.getMessage() of the exception was: "/storage/sdcard/Torno/Torno.xml: open failed: ENOENT (No such file or directory)"
Finally, I thought to install the app and it worked!! This is the current code:
btnAceptar.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String contenido="";
File fichero = null;
if (rdXML.isChecked()){
contenido = creaFichero(1,Integer.parseInt("" + txtAno.getText()),Integer.parseInt("" + txtMes.getText()));
fichero = grabarFichero(contenido, "Torno.xml");
}
else{
contenido = creaFichero(2,Integer.parseInt("" + txtAno.getText()),Integer.parseInt("" + txtMes.getText()));
fichero = grabarFichero(contenido, "Torno.txt");
}
if (fichero==null){
Toast toast = Toast.makeText(getApplicationContext(),"No hay datos para ese mes !!", Toast.LENGTH_LONG);
toast.show();
}
else{
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType(getMimeType(fichero.getAbsolutePath()));
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(fichero));
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "el tema");
sendIntent.putExtra(Intent.EXTRA_TEXT, "el cuerpo del mensaje");
startActivity(Intent.createChooser(sendIntent, "Title:"));
}
}
});
public static String getMimeType(String url){
String type = null;
String extension = MimeTypeMap.getFileExtensionFromUrl(url);
if (extension != null) {
MimeTypeMap mime = MimeTypeMap.getSingleton();
type = mime.getMimeTypeFromExtension(extension);
}
return type;
}
private File grabarFichero(String c,String n){
File file = null;
try {
// Creo la carpeta;
File folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Torno");
folder.mkdirs();
file = new File(Environment.getExternalStorageDirectory() + "/Torno", n);
OutputStreamWriter outw = new OutputStreamWriter(new FileOutputStream(file));
outw.write(c);
outw.close();
}
catch (Exception e) {}
return file;
}
THANKS!!!

How to send MMS with image attachment and some text in Android?

I want to attach image within some text to MMS in Android.I found a lot here on SO as well as on Google but still not get the right solution yet.My code is as:
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("image/png");
sendIntent.putExtra("sms_body",
getResources().getText(R.string.Message));
// sendIntent.setType("vnd.android-dir/mms-sms");
Uri mms_uri = Uri.parse("android.resource://"
+ getPackageName() + "/" + R.drawable.app_logo);
sendIntent.putExtra(Intent.EXTRA_STREAM, mms_uri.toString());
startActivity(Intent.createChooser(sendIntent, ""));
Please Help me for my this Issue.
Have you had any luck with this? I tried a similar approach, and found that
Uri mms_uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.drawable.app_logo);
is not universal. I managed to do it by making a copy of the image file in the assets folder and converting it into a File. You could do something like this:
File f = new File(getCacheDir()+"/app_logo.png");
if (!f.exists()) try {
InputStream is = getAssets().open("R.drawable.app_logo");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
FileOutputStream fos = new FileOutputStream(f);
fos.write(buffer);
fos.close();
} catch (Exception e) { throw new RuntimeException(e); }
sharePicture = f.getPath();

Categories

Resources