I am having exactly the same problem as described here.
I am trying to use this Intent:
android.provider.ContactsContract.Intents.ATTACH_IMAGE
Starts an Activity that lets the user pick a contact to attach an image to.
Sounds suitable to me but unfortunately results in an ActivityNotFoundException.
Code:
import android.provider.ContactsContract;
...
try {
Intent myIntent = new Intent();
myIntent.setAction(ContactsContract.Intents.ATTACH_IMAGE);
myIntent.setData(imageUri);
startActivity(myIntent);
} catch (ActivityNotFoundException anfe) {
Log.e("ImageContact",
"Firing Intent to set image as contact failed.", anfe);
showToast(this, "Firing Intent to set image as contact failed.");
}
I cannot find any error in the code above. The imageUri is correct for following code is working perfectly:
Code:
try {
Intent myIntent = new Intent();
myIntent.setAction(Intent.ACTION_ATTACH_DATA);
myIntent.setData(imageUri);
startActivity(myIntent);
} catch (ActivityNotFoundException anfe) {
Log.e("ImageContact",
"Firing Intent to set image as contact failed.", anfe);
showToast(this, "Firing Intent to set image as contact failed.");
}
As mentioned in the link this results in a another menu before getting to the contacts. That is acceptable but not perfect.
If you already know the file path you can use:
values.put(Images.Media.DISPLAY_NAME, fileName);
values.put(Images.Media.DATE_ADDED, currentTime);
values.put(Images.Media.MIME_TYPE, "image/jpeg");
values.put(Images.Media.ORIENTATION, 0);
values.put(Images.Media.DATA, filePath);
values.put(Images.Media.SIZE, size);
getContentResolver().insert(Images.Media.EXTERNAL_CONTENT_URI, values);
This way no nead to open a bitmap stream if you already have the file.
I'm having this problem too. I've had a little more success using by setting the Uri using the following code taken from http://developer.android.com/guide/topics/providers/content-providers.html
However, after selecting a contact and cropping the image the new contact icon still is not set?
// Save the name and description of an image in a ContentValues map.
ContentValues values = new ContentValues(3);
values.put(Media.DISPLAY_NAME, "road_trip_1");
values.put(Media.DESCRIPTION, "Day 1, trip to Los Angeles");
values.put(Media.MIME_TYPE, "image/jpeg");
// Add a new record without the bitmap, but with the values just set.
// insert() returns the URI of the new record.
Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
// Now get a handle to the file for that record, and save the data into it.
// Here, sourceBitmap is a Bitmap object representing the file to save to the database.
try {
OutputStream outStream = getContentResolver().openOutputStream(uri);
sourceBitmap.compress(Bitmap.CompressFormat.JPEG, 50, outStream);
outStream.close();
} catch (Exception e) {
Log.e(TAG, "exception while writing image", e);
}
Related
I use this code below, but it works partly - set wallpapers only on my real device api23 nexus5, in another devices in no way not setting. Also cant set wallpapers to icon of contact.
My actions:
tap button set wallpaper
in the opened window select service:
if choosed 'Contact photo' - open the service and if choose any contact
Actual result: just return to my wallpapers app without set
Expected result: must open 'crop picture' the image and then tap to set this image to contact icon.
if choosed 'Wallpaper'
Actual reuslt: just return to my wallpapers app without set and show message 'Can not load the image'(work only api 23 on my Nexus5)
Expected result: open the service and tap to set
Intent intent = new Intent(Intent.ACTION_ATTACH_DATA); // attach services
intent.addCategory(Intent.CATEGORY_DEFAULT);
file = new File(getFolderStorageDirectory(), getFileName()); // create temp file
if (isExternalStorageWritable()) { // check whether available external storage
try {
FileOutputStream out = new FileOutputStream(file);
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out); // write image
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG, "File not saved");
}
} else {
showToast(getString(R.string.sd_card));
}
intent.setDataAndType(Uri.parse(file.getAbsolutePath()), "image/*");
intent.putExtra("mimeType", "image/*");
intent.putExtra("jpg", "image/*");
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse(file.getAbsolutePath()));
startActivity(Intent.createChooser(intent, "Select service:"));
Why not work?
I solved this problem.
Instead
intent.setDataAndType(Uri.parse(file.getAbsolutePath()), "image/*");
use
setAs.setDataAndType(Uri.fromFile(mFile), "image/*"); and it will work.
All code:
private void setAsUseServices() {
onCreateFileListener(); // here will create file e.g. in Picture directory
Intent setAs = new Intent(Intent.ACTION_ATTACH_DATA);
setAs.addCategory(Intent.CATEGORY_DEFAULT);
Uri sourceUri = Uri.fromFile(mFile);
setAs.setDataAndType(sourceUri, "image/*");
setAs.putExtra("mimeType", "image/*");
setAs.putExtra("save_path", sourceUri);
startActivity(Intent.createChooser(setAs, "Select service:"));
}
I want to capture and share a view as an image.
My code runs without errors. The sharing part works well, but the capture does not work.
Any help is welcome.
Here the code i use.
linel1.buildDrawingCache();
Bitmap captureView = linel1.getDrawingCache();
FileOutputStream fos;
try {
fos = new FileOutputStream(Environment.getExternalStorageDirectory().toString()+"/DCIM/capture.jpeg");
captureView.compress(Bitmap.CompressFormat.JPEG, 100, fos);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Toast.makeText(getApplicationContext(), "Captured!", Toast.LENGTH_LONG).show();
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_SUBJECT, "title");
Uri uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory().toString()+"/DCIM/capture.jpeg"));
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.setType("image/*");
startActivity(Intent.createChooser(intent, "This picture is shared"));
I think you forget linel1.setDrawingCacheEnabled(true) before linel1.buildDrawingCache();
You can read more about drawing cache in this answer on SO.
can someone help me how to send an image to another application in android?
I want to send an image to be used as wallpaper, BBM display picture, and other applications that can use it (like WhatsApp, contacts, etc.)
I use this code, but it can only be used for sending text and not images as I want
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(sendIntent);
Then I tried to use this code to set the wallpaper
public void setAsWallpaper(Bitmap bitmap) {
try {
WallpaperManager wm = WallpaperManager.getInstance(_context);
wm.setBitmap(bitmap);
//disinimas
Toast.makeText(_context,
_context.getString(R.string.toast_wallpaper_set),
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(_context,
_context.getString(R.string.toast_wallpaper_set_failed),
Toast.LENGTH_SHORT).show();
}
}
the problem with the code, bitmap images directly applied as wallpaper. I wanted like sending text above, the user can choose to use another application. So I want a bitmap image that can later be used for wallpaper, BBM display picture, or other applications that support it
bitmap variable already contains the image that I want to, the images obtained from the internet with this code:
Bitmap bitmap = ((BitmapDrawable) fullImageView.getDrawable())
.getBitmap();
I use this code and its work, but give me a message BBM: File not found, WhatsApp: File is not an image:
Bitmap icon = bitmap;
Intent share = new Intent(Intent.ACTION_ATTACH_DATA);
share.setType("image/jpeg");
ContentValues values = new ContentValues();
values.put(Images.Media.TITLE, "title");
values.put(Images.Media.MIME_TYPE, "image/jpeg");
Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI,
values);
OutputStream outstream;
try {
outstream = getContentResolver().openOutputStream(uri);
icon.compress(Bitmap.CompressFormat.JPEG, 100, outstream);
outstream.close();
} catch (Exception e) {
System.err.println(e.toString());
}
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share Image"));
Thanks for your help Antrromet, finally I solved my problem with the following code:
Bitmap icon = bitmap;
// Intent share = new Intent(Intent.ACTION_SEND);
// share.setType("image/*");
ContentValues values = new ContentValues();
values.put(Images.Media.TITLE, "title");
values.put(Images.Media.MIME_TYPE, "image/*");
Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI,
values);
OutputStream outstream;
try {
outstream = getContentResolver().openOutputStream(uri);
icon.compress(Bitmap.CompressFormat.JPEG, 100, outstream);
outstream.close();
} catch (Exception e) {
System.err.println(e.toString());
}
Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setDataAndType(uri, "image/*");
intent.putExtra("mimeType", "image/*");
this.startActivity(Intent.createChooser(intent, "Set as:"));
Now the image can be used as Wallpaper, BBM profile picture, WA display Picture, Coontact display picture, etc. Thanks
Did you try using the following code? It is use to send binary data to other apps.
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);
shareIntent.setType("image/jpeg");
startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)));
Or if you dont have the URI with you but have the Bitmap instead, then try using the code as given here.
UPDATE:
Setting wallpaper and profile picture for BBM are totally different things and there is no common intent for them. For setting the wallpaper, you can try the following as given here.
Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setDataAndType(uri, "image/jpeg");
intent.putExtra("mimeType", "image/jpeg");
this.startActivity(Intent.createChooser(intent, "Set as:"));
For BBM, the APIs are not open for changing the user image. So you cannot do that through your app.
I am trying to share an image from within my app and the image is on the external storage of the device. The problem is that the user can still opt to share the image if they have manually deleted it from external storage. How do I check if they have deleted it first? Here is my share method:
#SuppressLint("NewApi")
private void shareImage(){
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/*");
//create new files
File f = new File(mExternalImagePath);
if (f.exists()) {
//Do action
f.setReadable(true, false);
//create new file in the system
try {
f.createNewFile();
} catch (IOException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
//create new file object from the absolute path
File f1 = f.getAbsoluteFile();
f1.setReadable(true, false);
Uri path = Uri.fromFile(f1);
intent.putExtra(Intent.EXTRA_STREAM, path );
Intent mailer = Intent.createChooser(intent, null);
//mailer.setType("image/*");
startActivity(mailer);
}else{
Log.d("not exist", "not exist");
}
}
It works but always shares so if the image was manually deleted, it will try sending a blank image.
I'm not sure whether your issue is because you're creating a new file by calling f.createNewFile() or something else at large.
Either way, you should be able to greatly simplify your code to just get the stream and send the intent like so:
private void shareImage(){
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/*");
//create new files
File f = new File(mExternalImagePath);
if (f.exists()) {
Uri path = Uri.fromFile(f);
intent.putExtra(Intent.EXTRA_STREAM, path);
Intent mailer = Intent.createChooser(intent, null);
startActivity(mailer);
}else{
Log.d("not exist", "not exist");
}
}
This will hopefully either clear up your issue, or make the issue more apparent and help you on your way.
You can also add a check for the file length, to ensure that it exists in some sort of readable form before you send your intent by calling .length() on f.
if (f.length() == 0){
Log.d("File Empty", "File does not have any content");
}else{
// create the intent and send
}
As #FD_ said, the createNewFile() was the problem. Be sure to add the mailer.setType("image/*"), so other apps can handle your sharing request.
#SuppressLint("NewApi")
private void shareImage(){
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/*");
File f = new File(mExternalImagePath);
if (f.exists()) {
Uri path = Uri.fromFile(f);
intent.putExtra(Intent.EXTRA_STREAM, path );
Intent mailer = Intent.createChooser(intent, null);
mailer.setType("image/*");
startActivity(mailer);
}else{
Log.d("not exist", "not exist");
}
}
I'm working on an app that, among other things, uses the device's camera to take a photo and then share it through email.
The problem I'm having is that I can't get the app to take the full sized picture. It always sends a reduced in resolution version of the photo, although the camera is set to 5MP and quality when compressing is set to 100. Below you have my code:
private void takePicture(){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 1);
}
public void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == CAMERA_PIC_REQUEST && resultCode == Activity.RESULT_OK){
picture = (Bitmap) data.getExtras().get("data");
pictureView.setImageBitmap(picture);
ContentValues values = new ContentValues();
values.put(Images.Media.TITLE, "Picture");
values.put(Images.Media.BUCKET_ID, "picture_ID");
values.put(Images.Media.DESCRIPTION, "");
values.put(Images.Media.MIME_TYPE, "image/jpeg");
pictureUri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
OutputStream outstream;
try{
outstream = getContentResolver().openOutputStream(pictureUri);
picture.compress(Bitmap.CompressFormat.JPEG, 100, outstream);
outstream.close();
}catch(FileNotFoundException e){
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
.....
share.setOnClickListener(new OnClickListener(){
public void onClick(View view){
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, selectedType);
intent.putExtra(Intent.EXTRA_TEXT,Notes + "\nLocation: " + selectedLocation+"\nOwner: " + selectedOwner
+ "\nStatus: " + selectedStatus);
intent.putExtra(Intent.EXTRA_STREAM, pictureUri);
try{
startActivity(Intent.createChooser(intent, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(getApplicationContext(), "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
}
});
There is an extra for the ACTION_IMAGE_CAPTURE intent with the key MediaStore.EXTRA_OUTPUT which takes an URI to a file as the value. If you don't supply this extra, a size-reduced version of the taken image is returned to onActivityResult() with the data intent.
The reason for this is that a full-sized camera picture is simply too big for the intent system to handle (it might work in theory, but slows down the whole intent processing a lot - intents should be as small as possible in general). So it can't be delivered like the small-size version.
To use this extra modify your takePicture() method, e.g. like this:
private void takePicture() {
File outputFile = new File(Environment.getExternalStorageDirectory(),
"image.jpg");
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(outputFile));
startActivityForResult(intent, 1);
}
This does work like your method above, with the exception that the camera app has written a full-size copy of the image into the file you specified when onActivityResult() is called.
This means you don't have to write the image to the disk on your own, just open it from there when your onClickListener() is executed like you did already.