I am stuck. I am converting a LinearLayout to a Bitmap, saving it, and then E-mailing it. The issue I have is I want to set the file name so it overwrites the previous file name. The main purpose is to email the LinearLayout as an image. From what I read you have to save it to the SD card first. I am fine with that but I only want to have one saved image at all times. I am getting a file name with what seems like a random 13 digit file name like (1329676773253.jpg) Here is the code.
void image() {
llImage.setDrawingCacheEnabled(true);
test2 = Bitmap.createBitmap(llImage.getDrawingCache());
ContentValues values = new ContentValues();
values.put(Images.Media.TITLE, "001");
values.put(Images.Media.DISPLAY_NAME, "ast.jpg");
values.put(Images.Media.DATE_ADDED, System.currentTimeMillis());
values.put(Images.Media.MIME_TYPE, "image/jpeg");
Uri uri2 = getContentResolver().insert(
Images.Media.EXTERNAL_CONTENT_URI, values);
try {
OutputStream outStream = getContentResolver()
.openOutputStream(uri2);
test2.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
Log.d("done", "done");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
final Intent emailIntent = new Intent(
android.content.Intent.ACTION_SEND);
emailIntent.setType("text/html");
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "");
emailIntent.putExtra(Intent.EXTRA_STREAM, uri2);
emailIntent.setType("image/png");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,
Html.fromHtml("" + finalEmail));
startActivity(Intent.createChooser(emailIntent, "Email:"));
}
You could simply write to file with FileOutputStream:
FileOutputStream outStream = new FileOutputStream("filename.jpg");
Related
I want to save my bitmap to cache directory.
I use this code:
try {
File file_d = new File(dir+"screenshot.jpg");
#SuppressWarnings("unused")
boolean deleted = file_d.delete();
} catch (Exception e) {
// TODO: handle exception
}
imagePath = new File(dir+"screenshot.jpg");
FileOutputStream fos;
try {
fos = new FileOutputStream(imagePath);
bitmap.compress(CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
Log.e("GREC", e.getMessage(), e);
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
}
}
it s working fine. But if I want to save different img to same path, something goes wrong. I mean it is saved to same path but I see it old image, but when I click the image I can see the correct image which I saved second time.
Maybe its come from cache but I do not want to see old image because when I want to share that image with whatsapp old image seen , if i send the image it seems correct.
I want to share saved image on whatsapp like this code:
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(imagePath));
shareIntent.setType("image/jpeg");
startActivityForResult(Intent.createChooser(shareIntent, getResources().getText(R.string.title_share)),whtsapp_result);
How can I fix it?
thanks in advance.
Finally I solved my problem like this:
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, getImageUri(context,bitmap_));
shareIntent.setType("image/jpeg");
startActivityForResult(Intent.createChooser(shareIntent, getResources().getText(R.string.title_share)),whtsapp_result);
v
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "TitleC1", null);
return Uri.parse(path);
}
This is just a guess, but since you save a new image (with the old name, or another, that’s not relevant) you should fire a media scan to that path so that the media provider is updated with new content.
See here:
MediaScannerConnection.scanFile(context, new String[]{imagePath}, null, null);
Or even better, wait for the scan to be completed:
MediaScannerConnection.scanFile(context, new String[]{imagePath}, null, new OnScanCompletedListener() {
#Override
void onScanCompleted(String path, Uri uri) {
// Send your intent now.
}
});
In any case this should be called after the new file is saved. As I said, I have not tested and this is just a random guess.
I can currently save an image in my app using the Es File explorer to a shared windows folder.
But what I want to know is, how can I eliminate the process of selecting the folder and specify it in code?
public void SaveToNetwork() {
Intent shareIntent = new Intent(Android.Content.Intent.ActionSend);
shareIntent.SetType("*/*");
shareIntent.PutExtra(Android.Content.Intent.ExtraStream, Android.Net.Uri.FromFile(new File(App._dir, App._file.Name)));
shareIntent.SetPackage("com.estrongs.android.pop");
StartActivity(shareIntent);
}
I don't want to use the file explorer. I just want to directly save it to the folder, or at least change the default selected folder to the correct one.
android provides File class and outpustream class for this purpose following is a sample code which recives the bitmap and save it to specified folder and then add you image to gallery content provider
private String savePic(Bitmap bitmapImage) {
try {
//bitmapImage=drawView.getmCanvasBitmap();
File dir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
"youfoldername");
dir.mkdir();
Calendar c = Calendar.getInstance();
if (dir.isDirectory()) {
String path = dir.getAbsolutePath() + "/youfilename"
+ c.getTimeInMillis() + ".Jpg";
FileOutputStream fos = new FileOutputStream(path);
// Use the compress method on the BitMap object to write image
// to
// the OutputStream
bitmapImage.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.close();
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
values.put(MediaStore.MediaColumns.DATA, path);
getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/Jpg");
intent.putExtra(android.content.Intent.EXTRA_STREAM,
Uri.parse("file://" + path));
startActivity(intent);
return path;
}
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
I got a camera application where, after the user takes an image, I save it to the internal storage in a directory I have made.
All that works fine, the image gets saved there and I can load it and show afterwards, but I'm having trouble saving the image to the Android gallery too.
What I want to, is after saving the image to the internal directory, copy it to the gallery.
I have tried this:
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());
values.put(MediaStore.Images.Media.MIME_TYPE, "image/png");
values.put(MediaStore.Images.Media.SIZE, file.length());
values.put(MediaStore.Images.Media.DATA, file.getAbsolutePath());
context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Where the file is the image I saved in internal storage.
With this method all I get is a broken image in the gallery.
copy it to gallery or show it in android gallery?
if you want the image show in android gallery? you can do this by scan media, this is how to scan:
Uri uri = Uri.fromFile(file);
Intent scanFileIntent = new Intent(
Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri);
sendBroadcast(scanFileIntent);
if you want just copy the file just do this :
in = new FileInputStream(sourceFile);
out = new FileOutputStream(destFile);
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
in.close();
in = null;
out.flush();
out.close();
You can use this method for saving image:
public void saveToSD(Bitmap outputImage){
File storagePath = new File(Environment.getExternalStorageDirectory() + "/MyPhotos/");
storagePath.mkdirs();
File myImage = new File(storagePath, Long.toString(System.currentTimeMillis()) + ".jpg");
try {
FileOutputStream out = new FileOutputStream(myImage);
outputImage.compress(Bitmap.CompressFormat.JPEG, 80, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
This is what might help :
Uri selectedImageURI = data.getData();
imageFile = new File(getRealPathFromURI(selectedImageURI));
After getting the path u can store the image by this :
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());
values.put(MediaStore.Images.Media.MIME_TYPE, "image/png");
values.put(MediaStore.Images.Media.SIZE, file.length());
values.put(MediaStore.Images.Media.DATA, file.getAbsolutePath());
context.getContentResolver().insert(imageFile, values);
Hope this helps.
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();
I'm trying to send a mail attaching assest folder image to the mail, mail is sent successfully, but when i checked it there was no image attached to the mail,
this is my code,
Uri uri = Uri.fromFile(new File("file:///android_asset/Hat_5.png"));
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setType("image/png");
intent.putExtra(Intent.EXTRA_EMAIL , new String[] { "some#gmail.com"});
intent.putExtra(Intent.EXTRA_SUBJECT, "New Order");
intent.putExtra(Intent.EXTRA_TEXT , "Order Id :" +imageId);
intent.putExtra(Intent.EXTRA_STREAM , uri);
startActivity(Intent.createChooser(intent, "Send mail..."));
permission,
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
You can only attach files from the SDCARD.
You'll need to copy the image to the SDCARD and change the URI the the file's path on the sdcard.
You could delete if afterwards.
You can do this if you want
String FILENAME = "avatar.png";
FileOutputStream fos = null;
try {
fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
Log.v("","FileNotFoundException: "+e1.getMessage());
}
try {
InputStream inputStream = getAssets().open("avatar.jpg");
byte buf[]=new byte[1024];
int len;
while((len=inputStream.read(buf))>0)
fos.write(buf,0,len);
fos.close();
inputStream.close();
} catch (IOException e1) {
e1.printStackTrace();
Log.v("","IOException: "+e1.getMessage());
}
// Get the file from internal storage
String filePath = getApplicationContext().getFilesDir().getAbsolutePath();//returns current directory.
File file = new File(filePath, FILENAME);
then
Uri uri = Uri.fromFile(file);
intent.putExtra(Intent.EXTRA_STREAM , uri);
And send the attachment...