I am using iText for creating pdf, i need to set background image to first pdf page but image is high resolution, how can set background image with out reduce image quality. Please help me.
there is work around for doing this by setting the image at absolute position and setting the page size equal to background image and don't forget to put the image in proper dpi folder :)
private void setBackground(Document document) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.background_img);
bitmap.compress(Bitmap.CompressFormat.JPEG , 100, stream);
Image img;
try {
img = Image.getInstance(stream.toByteArray());
img.setAbsolutePosition(0, 0);
document.add(img);
} catch (BadElementException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Related
I have a string which consists an URL of the image, in the same activity I am viewing the image through URL. But to set the same image as my wallpaper, I am converting the string to Uri and then to Bitmap to use setBitmap.But I am still getting error telling No image was chosen.
Code is below:
newString has the URL of the image.
final String myUrlStr = newString;
URL url;
Uri uri=null;
try {
url = new URL(myUrlStr);
uri = Uri.parse( url.toURI().toString() );
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
try {
image = MediaStore.Images.Media.getBitmap(this.getContentResolver(),uri);
} catch (IOException e) {
e.printStackTrace();
}
setButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
WallpaperManager wallpaperManager=WallpaperManager.getInstance(getApplicationContext());
try {
// Set the image as wallpaper
if(image!=null)
wallpaperManager.setBitmap(image);
else
Toast.makeText(getApplicationContext(), "No image was chosen.", Toast.LENGTH_LONG).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
It seems like I can't comment. So i answer here.
I am not sure about your url being on internet or local. I don't find anything wrong with your code. So, my deduction is your onclicklistener is set before it can fetch image.(might need to use asyntask to save image) Are you displaying on your imageview from same image resource?
I have implemented facebook sdk in my android application and I have spent past 3hrs in downloading profile picture from facebook and yet i'm not successful in downloading it.
This is my code.
try {
String user = Facebuk.fb.request("me");
jsonObject = Util.parseJson(user);
String id = jsonObject.optString("id");
name = jsonObject.optString("name");
URL img_url = new URL("http://graph.facebook.com/" + id
+ "/picture?type=large");
dp = BitmapFactory.decodeStream(img_url.openConnection().getInputStream());
if (dp == null)
Log.i("", "afafaffgwgwgwg");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FacebookError e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
i get notified from the logcat that BitmapFactory.decodestream returns null
There were similar posts asked in Stack Overflow and there solution didn't workout for me.. please help me out.
Try using AQuery library which provides callback in which u get bitmap.
My OutputStreamWrite refuses to append to my file. It only overwrites the first line constantly.
The String lightRowValues are sent from another method that goes through a table and gets one row at a time, sends that row data here, where that row is written to this file. Then the method loops back and gets the next row. SO I should have a list of rows but instead only have one line of the very last entry.
public static void appendToLtCSV(String lightRowValues, String CSVFinalFileName){
try {
csvfos = new FileOutputStream(CSVFinalFileName, true);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
OutputStreamWriter sensorCSVWriter = new OutputStreamWriter(csvfos);
try {
sensorCSVWriter.append(lightRowValues);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
sensorCSVWriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I have set the append flag to true and still the same problem....
i am creating the pdf by using Document class,after i am downloading the images from sdcard camera folder,i want to keep all the images in pdf file,i am taking document page is A4 sizes,i want to scale image with A4 size means i want keep image with a4 sizes images.
private void addImages(Document document) {
// TODO Auto-generated method stub
File targetDirector = new File(targetPath);
File[] files = targetDirector.listFiles();
for (File file : files) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
thumbnails = decodeFile(file.getAbsolutePath());
ExifInterface exif;
try {
exif = new ExifInterface(file.getAbsolutePath());
String orientString = exif
.getAttribute(ExifInterface.TAG_ORIENTATION);
int orientation = orientString != null ? Integer
.parseInt(orientString)
: ExifInterface.ORIENTATION_NORMAL;
int rotationAngle = 0;
if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
System.out.println("photopotart");
thumbnails = getResizedBitmap(thumbnails, 150, 150);
thumbnails
.compress(Bitmap.CompressFormat.JPEG, 100, stream);
Image myImg = null;
try {
myImg = Image.getInstance(stream.toByteArray());
} catch (BadElementException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// add image to document
try {
document.newPage();
document.add(myImg);
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
System.out.println("180 angle");
rotationAngle = 180;
System.out.println("photo180");
thumbnails = getResizedBitmap180(thumbnails, 200, 200);
thumbnails
.compress(Bitmap.CompressFormat.JPEG, 100, stream);
Image myImg = null;
try {
myImg = Image.getInstance(stream.toByteArray());
} catch (BadElementException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
myImg.setAlignment(Image.LEFT);
// add image to document
try {
document.newPage();
document.add(myImg);
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
System.out.println("other 270");
System.out.println("photo270");
thumbnails = getResizedBitmap270(thumbnails, 200, 200);
thumbnails
.compress(Bitmap.CompressFormat.JPEG, 100, stream);
Image myImg = null;
try {
myImg = Image.getInstance(stream.toByteArray());
} catch (BadElementException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// add image to document
try {
document.newPage();
document.add(myImg);
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
System.out.println("photolandscape");
System.out.println("photo0");
// thumbnails =thumbnails.createScaledBitmap(thumbnails,
// 350, 250, false);
System.out.println("thumbnailsw" + thumbnails.getWidth());
System.out.println("thumbnailsh" + thumbnails.getHeight());
String imageUrl = file.getAbsolutePath();
Image image2 = Image.getInstance(file.getAbsolutePath());
image2.scaleAbsolute(150f, 150f);
document.newPage();
document.add(image2);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BadElementException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
try something like this
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Bitmap bitmap = BitmapFactory.decodeResource(getBaseContext().getResources(), R.drawable.ic_launcher);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100 , stream);
Image myImg = Image.getInstance(stream.toByteArray());
myImg.setAlignment(Image.MIDDLE);
//add image to document
document.add(myImg);
And for scaling the image if not fill on page
Bitmap.createScaledBitmap(unscaledBitmap, wantedWidth, wantedHeight, true);
Also go through this it has a good explanation for scaling the bitmap
Im doing a program where u can draw your signature on the phone. Right now it saves one image but I would like to save more than one image since there are more than one customer that needs to sign their package. very thankful for any kind of help.
public void save() {
File sdImageMainDirectory = new File("/sdcard/mySignatures");
sdImageMainDirectory.mkdirs();
String nameFile = "newpic";
FileOutputStream out = null;
try {
out = new FileOutputStream(sdImageMainDirectory.toString() +"/" + nameFile + ".jpg");
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
out=null;
}
Rather obviously, you need to change the filename to something unique for each one. Numbering them sequentially would work. Or you let the user enter a name, and validate it for legality.