I want to fetch image from table 1 to update image in table 2 on parse here is my code
ParseFile image= ParseUser.getCurrentUser().getParseFile("image");
if(image==null)
{
}
else
{
try {
byte[] data=image.getData();
Bitmap bmp = BitmapFactory
.decodeByteArray(
data, 0,
data.length);
// Set the Bitmap into the
// ImageView
image1.setImageBitmap(bmp);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
This code is working perfect it retrieves and set image properly now I want this "image" to upload in my Second table I am doing this
ParseQuery<ParseObject> query = ParseQuery.getQuery("XYZ");
String user_id=ParseUser.getCurrentUser().getObjectId();
query.getInBackground(user_id, new GetCallback<ParseObject>() {
#Override
public void done(ParseObject pdata, ParseException e) {
// TODO Auto-generated method stub
pdata.put("image",image); // this line throw NullPointerException
pdata.saveInBackground();
}
});
What I am doing wrong anyone please help?
You need ParseFile object. Try the following code. (It works fine, I just checked it)
byte[] pfArray = getBytesFromBitmap(bmp);
ParseFile file = new ParseFile("abc.png", pfArray);
// Upload the image into Parse Cloud
file.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
System.out.println("saved");
}
});
public byte[] getBytesFromBitmap(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 70, stream);
return stream.toByteArray();
}
hope it helps.
Related
I am taking a photo with the camera and saving the photo value into a bitmap. I would like to use that photo in itext to generate an pdf.
This is the code I have so far.
Bitmap bitmap;
public void Picture()
{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,0);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
bitmap=(Bitmap)data.getExtras().get("data");
PDF();
}
public void PDF()
{
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
Image img = Image.getInstance(bitmap);
Document document = new Document();
PdfWriter.getInstance(document, out);
document.open();
document.add(new Paragraph("Example"));
document.close();
}
bitmap=(Bitmap)data.getExtras().get("data");
ByteArrayOutputStream stream3 = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream3);
Image maimg = Image.getInstance(stream3.toByteArray());
maimg.setAbsolutePosition(490, 745);
maimg.scalePercent(40);
document.add(maimg);
you should download itextpdf-5.3.2.jar file and attach in your project.
You can use it as an example:
public class WritePdfActivity extends Activity
{
private static String FILE = "mnt/sdcard/FirstPdf.pdf";
static Image image;
static ImageView img;
Bitmap bmp;
static Bitmap bt;
static byte[] bArray;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
img=(ImageView)findViewById(R.id.imageView1);
try
{
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(FILE));
document.open();
addImage(document);
document.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
private static void addImage(Document document)
{
try
{
image = Image.getInstance(bArray); ///Here i set byte array..you can do bitmap to byte array and set in image...
}
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();
}
// image.scaleAbsolute(150f, 150f);
try
{
document.add(image);
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Here is the reference, Please check
enter link description here
One Important thing better to use PDFBox library for convert Image to PDF
This question already has answers here:
How I can convert a bitmap into PDF format in android [closed]
(3 answers)
Closed 2 months ago.
I am trying to convert an activity to pdf format.I have taken a screenshot of the same as a bitmap.PLease help me to convert that sreenshot to the required pdf file.Thank you.
Please provide the code.Thank you.
This is the code I used...
1) MainActivity
public class MainActivity extends Activity {
//Button btn_getpdf;
private LinearLayout linearLayout;
private Bitmap myBitmap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// btn_getpdf = (Button) findViewById(R.id.button_pdf);
linearLayout = (LinearLayout) findViewById(R.id.linear1);
linearLayout.post(new Runnable() {
public void run() {
//take screenshot
myBitmap = captureScreen(linearLayout);
Toast.makeText(getApplicationContext(), "Screenshot captured..!", Toast.LENGTH_LONG).show();
try {
if (myBitmap != null) {
//save image to SD card
saveImage(myBitmap);
}
Toast.makeText(getApplicationContext(), "Screenshot saved..!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
public Bitmap captureScreen(View v) {
Bitmap screenshot = null;
try {
if (v != null) {
screenshot = Bitmap.createBitmap(v.getMeasuredWidth(), v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(screenshot);
v.draw(canvas);
}
} catch (Exception e) {
// Log.d("ScreenShotActivity", "Failed to capture screenshot because:" + e.getMessage());
}
return screenshot;
}
public void saveImage(Bitmap bitmap) throws IOException {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 40, bytes);
File f = new File(root.getAbsolutePath() + "/DCIM/Camera/bitmap.jpg");
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
fo.close();
}
}
2)WritePdfActivity
public class WritePdfActivity extends Activity
{
private static String FILE ="DCIM/Camera/GenPdf.pdf";
static Image image;
static ImageView img;
Bitmap bmp;
static Bitmap bt;
static byte[] bArray;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
img=(ImageView)findViewById(R.id.imageView1);
try
{
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(FILE));
document.open();
addImage(document);
document.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
private static void addImage(DocumentsContract.Document document)
{
try
{
image = Image.getInstance(bArray); ///Here i set byte array..you can do bitmap to byte array and set in image...
}
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();
}
// image.scaleAbsolute(150f, 150f);
try
{
document.add(image);
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Plz try this So question:
How I can convert a bitmap into PDF format in android
pdf-format-in-android/14393561#14393561
or also include itextpdf-5.3.2.jar in your project
#Swagatam Dutta use
File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM/Camera/bitmap.jpg");
I'm trying to implement a simple file upload and associate the file with the current user.
I followed the guide on parse.com and checked with several questions on here with no luck.
The saveinBackground operation is successful, no exceptions are being thrown but I can't see the file in Parse.com data browser.
Here's my code.
final long time1 = Time;
//Image part
//upload the .jpg file for user's history
//Parse
byte[] data = filePath.getBytes();
final ParseFile file = new ParseFile("asdasd.jpg", data);
file.saveInBackground(new SaveCallback() {
public void done(ParseException e) {
// Handle success or failure here ...
if ( e == null){
ParseObject rentedTime = new ParseObject("Time");
rentedTime.put("duration", renttime1);
rentedTime.put("owner", ParseUser.getCurrentUser());
rentedTime.put("id", id);
rentedTime.put("image", file);
rentedTime.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null)
Log.d("upload successful" + file.getName(), null);
}
});
} else {
Log.d("terrible", e.getMessage());
}
}
}, new ProgressCallback() {
public void done(Integer percentDone) {
// Update your progress spinner here. percentDone will be between 0 and 100.
}
});
}
What am I missing?
That's because you are NOT uploading the file..see
byte[] data = filePath.getBytes();
just getting the bytes from the file path wont work reasonably.
First get the bitmap and then convert it to byte[] see how..
Bitmap bitmap = BitmapFactory.decodeFile(filePath);
byte[] data = getBytes(bitmap);
private byte[] getBytes(Bitmap bitmap) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
byte[] data = outputStream.toByteArray();
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
return data;
}
And you can now use the data..
final ParseFile file = new ParseFile("asdasd.png", data);
I've changed the file name to .png format if you wish ti save in jpg format then compress in .JPG format as well.
Hope you find this helpful! :)
can someone help me?
I want to take a screenshot and post this to the facebook wall (with a message)!
I have read several topics and forums but i dont find something that worked for me!
I already have the facebook SDK!
Thanks a lot!
capture images of view using this way.
View v = view.getRootView();
v.setDrawingCacheEnabled(true);
Bitmap b = v.getDrawingCache();
String extr = Environment.getExternalStorageDirectory().toString();
File myPath = new File(extr, "yourImageName.jpg");
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) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
here v is root layout and than post photo in facebook using SDK 3.5.
like this way
private SimpleFacebook mSimpleFacebook;
mSimpleFacebook = SimpleFacebook.getInstance(this);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(photoPath, options);
// create Photo instace and add some properties
Photo photo = new Photo(bitmap);
photo.addDescription("Screenshot from sample application");
photo.addPlace("110619208966868");
// publish
mSimpleFacebook.publish(photo, new OnPublishListener()
{
#Override
public void onFail(String reason)
{
mProgress.hide();
// insure that you are logged in before publishing
Log.w(TAG, "Failed to publish");
}
#Override
public void onException(Throwable throwable)
{
mProgress.hide();
Log.e(TAG, "Bad thing happened", throwable);
}
#Override
public void onThinking()
{
// show progress bar or something to the user while publishing
mProgress = ProgressDialog.show(this, "Thinking",
"Waiting for Facebook", true);
}
#Override
public void onComplete(String id)
{
mProgress.hide();
toast("Published successfully. The new image id = " + id);
}
});
I need to create a bitmap from sdcard images so after getting uri of image i am getting byte data by opening inputStream as follows
public byte[] getBytesFromFile(InputStream is)
{
byte[] data = null;
ByteArrayOutputStream buffer=null;
try
{
buffer = new ByteArrayOutputStream();
int nRead;
data = new byte[16384];
while ((nRead = is.read(data, 0, data.length)) != -1)
{
buffer.write(data, 0, nRead);
}
buffer.flush();
return buffer.toByteArray();
} catch (IOException e)
{
return null;
}
finally
{
if(data!=null)
data = null;
if(is!=null)
try {
is.close();
} catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
if(buffer!=null)
{
try {
buffer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
buffer = null;
}
System.gc();
}
after this i am just creating bitmap of that data by following code
byte[] data = getBytesFromFile(is);
Bitmap bm= BitmapFactory.decodeByteArray(data, 0, data.length);
but it gives me outofmemory error. many peoples guide me to check for memory leakages but, this is the first step in my app., i mean thru intent filter i start my app from gallery menu option "share" which invokes my app with the image uri..
plz guide me guys if i am wrong... and also this exception comes on the devices which have high resolution images(size excceds 1MB), but any how it should create Bitmap...
Try to use the BitmapFactory.decodeStream() method instead of first loading the bytearray into memory. Also check out this question for more info on loading bitmaps.