i want to share a photo that is at my xml view but any sharing intenet requires that photo should be saved on memory card. how can i use that photo?
this is my xml:
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginLeft="17dp"
android:text="Button" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/ellyfish" />
You can try something like this:
First get the bitmap from imageview:
Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();
Create a new file using this bitmap:
public File createImageFromBitmap(Bitmap bitmap) {
File f ;
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
// you can create a new file name "test.jpg" in sdcard folder.
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator + "rwd_temp.jpg");
f.createNewFile();
// write the bytes in file
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
// remember close de FileOutput
fo.close();
} catch (Exception e) {
e.printStackTrace();
return null;
}
return f;
}
What I am doing is getting the bitmap from the imageview and saving that bitmap in a file named rwd_temp.jpg. After you have the file you can upload it to server or share it.
You will require WRITE_EXTERNAL_STORAGE permission for this.
Related
I tried to take a screenshot of a imageview programatically! When the image is saved to folder it is producing black image! when i tried to take screenshot of my entire scroll view, I am able to take screenshot. Please help:
my Code is:
private void saveop(){
RelativeLayout parent = (RelativeLayout) findViewById(R.id.opimagelayout);// which includes your view or buttons?
Bitmap bitmap = Bitmap.createBitmap(384,
100, Bitmap.Config.ARGB_8888);
Canvas bitmapHolder = new Canvas(bitmap);
parent.draw(bitmapHolder);
try {
final File f = savebitmaps(bitmap,"operator","");
} catch (IOException e) {
e.printStackTrace();
}
}
public static File savebitmaps(Bitmap bmp,String transno, String currentDateandTime) throws IOException {
Bitmap out = BITMAP_RESIZER(bmp, 384,112);
File folder = new File(Environment.getExternalStorageDirectory() + "/MobeeLoad");
File file = new File(folder, transno+".jpg");
FileOutputStream fOut;
try {
fOut = new FileOutputStream(file);
out.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
fOut.flush();
fOut.close();
bmp.recycle();
out.recycle();
} catch (Exception e) {
}
bmp.recycle();
return file;
}
Layout xml:
<RelativeLayout
android:id="#+id/opimagelayout"
android:background="#ffffff"
android:layout_width="match_parent"
android:layout_below="#+id/date"
android:layout_height="90dp">
<ImageView
android:id="#+id/opimage"
android:layout_width="90dp"
android:src="#drawable/check"
android:layout_centerHorizontal="true"
android:layout_height="90dp" />
</RelativeLayout>
I have a bitmap file which i need to upload to my php server but as the file is very large I decided to resize it and save it. Later on I try to read it back to display resized image. But this time I am not getting the same image
Below is code for writing image and returning File
public static File savebitmap(Bitmap bmp) throws IOException {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator + "testimage.jpg");
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
fo.close();
return f;
}
Below is code for reading and displaying
File file=ImageUtil.savebitmap(this.bitmap);
this.imgChoosenImage.setImageURI(Uri.parse(file.getAbsolutePath()));
Please tell me what exactly is going wrong here
first check the images are saved in ur path as defined, and Make sure ur giving correct path for retriving image.
I have used this below code for saving imge in gallery
String iconsStoragePath = Environment.getExternalStorageDirectory()+ File.separator;
File sdIconStorageDir = new File(iconsStoragePath);
//create storage directories, if they don't exist
sdIconStorageDir.mkdirs();
try {
String filePath = null;
filePath = Environment.getExternalStorageDirectory() + File.separator + "testimage" + ".jpg";
FileOutputStream fileOutputStream = new FileOutputStream(filePath);
BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
bmp.compress(Bitmap.CompressFormat.JPG, 100, bos);
bos.flush();
bos.close();
} catch (IOException e) {
Log.w("TAG", "Error saving image file: " + e.getMessage());
Toast.makeText(getApplicationContext(), "Failed to Create folder",
Toast.LENGTH_SHORT).show();
}
For bitmap display in imageview :
File imgFile = new File("/sdcard/Images/testimage.jpg");
//Here File file = ur file path
if(imgFile.exists())
{
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);
myImage.setImageBitmap(myBitmap);
}
Permissions:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
My application has a requirement where the user can write their signature on the screen. The signature should be able to be extracted into an image.
How should I go about this ?
Is there an existing android process/observer for this ? Or are they any libraries / plugins that might allow the functionality ?
use this code in Activity:
try {
GestureOverlayView gestureView = (GestureOverlayView) findViewById(R.id.signaturePad);
gestureView.setDrawingCacheEnabled(true);
Bitmap bm = Bitmap.createBitmap(gestureView.getDrawingCache());
File f = new File(Environment.getExternalStorageDirectory()+ File.separator + "signature.png");
f.createNewFile();
FileOutputStream os = new FileOutputStream(f);
os = new FileOutputStream(f);
//compress to specified format (PNG), quality - which is ignored for PNG, and out stream
bm.compress(Bitmap.CompressFormat.PNG, 100, os);
os.close();
} catch (Exception e) {
Log.v("Gestures", e.getMessage());
e.printStackTrace();
}
}
In xml:
<android.gesture.GestureOverlayView
android:id="#+id/signaturePad"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="5"
android:background="#android:color/white"
android:eventsInterceptionEnabled="true"
android:fadeEnabled="false"
android:gestureColor="#android:color/black"
android:gestureStrokeLengthThreshold="0.1"
android:gestureStrokeType="multiple"
android:orientation="vertical"/>
I am facing some strange problem with my android code,
I have an image in Bitmap variable and want to save that file to SD card.
I code as follow,
Bitmap IMAGE // Loaded from internet servers.;
try {
File _sdCard = Environment.getExternalStorageDirectory();
File _picDir = new File(_sdCard, "MyDirectory");
_picDir.mkdirs();
File _picFile = new File(_picDir, "MyImage.jpg");
FileOutputStream _fos = new FileOutputStream(_picFile);
IMAGE.compress(Bitmap.CompressFormat.JPEG, 100, _fos);
_fos.flush();
_fos.close();
Toast.makeText(this, "Image Downloaded", 7000).show();
} catch (Exception ex) {
ex.printStackTrace();
Toast.makeText(this, ex.getMessage(), 7000).show();
}
I am using Sony Experia Arc as my testing device, when the phone is connected to my computer, the code works nice, it stores image and also displays in gallery. But when I disconnect phone from my computer and test the app, it doesn't save picture and doesn't show any exception.
use this function
void saveImage() {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_images");
String fname = "Image.jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
myBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Check this answer will give more details Android saving file to external storage
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
//4
File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg");
try {
file.createNewFile();
FileOutputStream fo = new FileOutputStream(file);
//5
fo.write(bytes.toByteArray());
fo.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
**This Code Cover the Following Topics**
1. Save a bitmap Image on sdcard a jpeg
2. Create a folder on sdcard
3. Create every file Separate name
4. Every file save with date and time
5. Resize the image in very small size
6. Best thing image Quality fine not effected from Resizing
The following method is used to create an image file using the bitmap
public void createImageFromBitmap(Bitmap bmp) {
FileOutputStream fileOutputStream = null;
try {
// create a File object for the parent directory
File wallpaperDirectory = new File("/sdcard/Capture/");
// have the object build the directory structure, if needed.
wallpaperDirectory.mkdirs();
//Capture is folder name and file name with date and time
fileOutputStream = new FileOutputStream(String.format(
"/sdcard/Capture/%d.jpg",
System.currentTimeMillis()));
// Here we Resize the Image ...
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100,
byteArrayOutputStream); // bm is the bitmap object
byte[] bsResized = byteArrayOutputStream.toByteArray();
fileOutputStream.write(bsResized);
fileOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
}
and add this in manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I need to take a screen shot and save the screen shot. I need to do this without using any connection to PC or without unrooting the phone. I need to do this whenever a event is triggered . For example when an ad is shown in a game ... or when the game ends and shows the scores in snake etc. Can you please let me know How Can i do this. I saw some tutorilas and they gave the code but that doesnt seem to work
private void getScreen()
{
View content = findViewById(R.id.layoutRoot);
Bitmap bitmap = content.getDrawingCache();
File file = new File("/sdcard/test.png");
try
{
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 100, ostream);
ostream.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
You have to enable the cache first, before calling getDrawingCache().
View ve = findViewById(R.id.loyout);
ve.setDrawingCacheEnabled(true);
Bitmap b = ve.getDrawingCache();
String extr = Environment.getExternalStorageDirectory().toString()
+ "/SaveCapture";
myPath = new File(extr);
if (!myPath.exists()) {
boolean result = myPath.mkdir();
Toast.makeText(this, result + "", Toast.LENGTH_SHORT).show();
}
myPath = new File(extr, getString(R.string.app_name) + ".jpg");
Toast.makeText(this, myPath.toString(), Toast.LENGTH_SHORT).show();
FileOutputStream fos = null;
try {
fos = new FileOutputStream(myPath);
b.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
MediaStore.Images.Media.insertImage(getContentResolver(), b,
"Screen", "screen");
}
catch (FileNotFoundException e) {
Log.e("Error", e + "");
}
catch (Exception e) {
Log.e("Error", e + "");
}
Can you give more information as to what does not work when you run that code ? Does it not capture what you want ? Does it crash ?
Make sure you change the R.id.layoutroot correctly with your root layout... Beside that it seems like it would work...
<com.example.android.snake.SnakeView
android:id="#+id/snake"
android:layout_width="match_parent"
android:layout_height="match_parent"
tileSize="24"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/text"
android:text="#string/snake_layout_text_text"
android:visibility="visible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center_horizontal"
android:textColor="#ff8888ff"
android:textSize="24sp"/>
</RelativeLayout>
Edit...
So for example, if you use that layout you just put there, you should change the R.id.layout into R.id.snake, that's because of this line : android:id="#+id/snake".
I don't think there is an easy way to find /get the id of the "root" layout of a view (if you wanted to take screenshot of anything the phone is showing.
I just checked the launcher and another application, seems like most app are placed into a FrameLayout with id/content, so you can try to use android.R.id.content, but there is no guaranty this will work every time...