insert image's path and retrieve it from SQLite - android

can anyone give me a method to how save a image's path in database and how to display the image in imageView.

You can store images and show into imageview this way.
String path = Environment.getExternalStorageDirectory().toString()+ "/Directoryname";
String imageName;
File mFolder = new File();
if (!mFolder.exists()) {
mFolder.mkdir(path);
}
imageName= "yourimagename.jpg";
//now you can store imagepath into database and imageto your sdcard so we can show image as per our requirement
File file = new File (mFolder, imageName);
if (file.exists ()) file.delete ();
Bitmap thumbnail = you can convert your image to bitmap and store into database.
try {
FileOutputStream out = new FileOutputStream(file);
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
//it will be store in your sdcard.
//for displaying image into imageview
File f = new File(path+"/"+imageName);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(), options);
iv.setImageBitmap(bitmap);
If you are finding any trouble then let me know.

Related

android download the image from url store in internal memory then display that image in imageview

I want to download the image from url and store it to internal memory after storing get the image stored in internal memory and display it in imageview
You can use this code to download the image
URL url = new URL(<your url>);
InputStream in = new BufferedInputStream(url.openStream());
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int n = in.read(buf);
while (n!=-1)
{
out.write(buf, 0, n);
n=in.read(buf)
}
out.close();
in.close();
byte[] response = out.toByteArray();
And below code to save it to internal storage
FileOutputStream fos = new FileOutputStream(filePath);
fos.write(response);
fos.close();
where file path for internal storage is
String filePath=getFilesDir().getPath() + File.separator + "image_" + <some unique identifier like int or string that is different for different images>
ANd to show in imageView use
File imgFile = new File(filePath);
if(imgFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);
myImage.setImageBitmap(myBitmap);
}
Hope it helps.

Bad image quality when load/save bitmap

I'm working with bitmap and have some problems need to help:
My app works as below:
Load JPG image file(1) from SDcard to bitmap1
Save this bitmap1 to new JPG file(2).
Load new JPG image(2) file to bitmap2
Save bitmap2 to new JPG file(3) ....
.... repeat again and again
Now I can load/save bitmap to file, but problem is quality of image reduces after load/save.
So if I do load/save stuff for 10 times, so my image become ugly.
This is my code:
private void saveBitmapToFile(String imgPath) {
Log.e("Filename-----------------", imgPath);
// Decode image file to bitmap
BitmapFactory.Options options = new BitmapFactory.Options();
// options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(imgPath, options);
// Get filename
long currentMili = System.currentTimeMillis();
currentName = currentMili + "";
String filePath = FOLDER_PATH + currentMili + ".jpg";
// Save bitmap to new file
try {
File file = new File(filePath);
FileOutputStream fOut = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
} catch (Exception e) {
e.printStackTrace();
}
}
You're re-compressing a lossy file format. You're going to get image artifacts doing that. If you need to do this for some reason, use a lossless format like png.

Image capture and upload to FTP in Android

I am facing a problem I cannot solve myself.
I have to capture image in my android app, and then upload that image to FTP server. Of course, I have to resize it before sending it to FTP because 2MB is definitely unacceptable size :)
I succeded in taking picture, getting its path and upload it in its full size.
This is how I upload it to server.
File file = new File(pathOfTheImage);
String testName =System.currentTimeMillis()+file.getName();
fis = new FileInputStream(file);
// Upload file to the ftp server
result = client.storeFile(testName, fis);
Is it possible, at this point, to resize or compress image in order to reduce its size and after that, to upload it to server?
Any help would be appreciated.
P.S. Sorry for my poor English!
EDIT:
Solved thanks to Alamri.
One more time, man, thank you!!!
In my application i used this before uploading the image:
1- resize,scale and decode the bitmap
private Bitmap decodeFile(File f) {
try {
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f),null,o);
final int REQUIRED_SIZE=450;
int scale=1;
while(o.outWidth/scale/2>=REQUIRED_SIZE && o.outHeight/scale/2>=REQUIRED_SIZE)
scale*=2;
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
Bitmap bit1 = BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
return bit1;
} catch (FileNotFoundException e) {}
return null;
}
2- now let's save the resized bitmap:
private void ImageResizer(Bitmap bitmap) {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/Pic");
if(!myDir.exists()) myDir.mkdirs();
String fname = "resized.im";
File file = new File (myDir, fname);
if (file.exists()){
file.delete();
SaveResized(file, bitmap);
} else {
SaveResized(file, bitmap);
}
}
private void SaveResized(File file, Bitmap bitmap) {
try {
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
after saving the resized, scaled image. upload it using your code :
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/Pic/resized.im");
String testName =System.currentTimeMillis()+file.getName();
fis = new FileInputStream(file);
// Upload file to the ftp server
result = client.storeFile(testName, fis);

Unable to store Image into internal storage directory

I wanted to create a folder and store image into the phone's internal storage. I tried using the code below to download an image from the url. It managed to load the image into imageView but unable to store and create folder in the internal storage. Plus I got no warning or error message. Any idea what's wrong code below?
Bitmap bm = null;
InputStream in;
try{
in = new java.net.URL("http://blogs.computerworld.com/sites/computerworld.com/files/u177/google-nexus-4.jpg").openStream();
bm = BitmapFactory.decodeStream(new PatchInputStream(in));
File mydir = this.getDir("mydir", Context.MODE_PRIVATE);
mydir.mkdirs();
File fileWithinMyDir = new File(mydir, "myfile");
FileOutputStream out = new FileOutputStream(fileWithinMyDir);
bm.compress(Bitmap.CompressFormat.JPEG, 85, out);
}
catch(Exception e1){
e1.printStackTrace();
}
ImageView img = (ImageView) findViewById(R.id.image_display);
img.setImageBitmap(bm);
once you have your bitmap bm:
FileOutputStream fos;
try {
fos = openFileOutput("file_Name", Context.MODE_PRIVATE);
bm.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
}catch (Exception e){
...
}
will save your file to internal storage
use the following:
String path=Environment.getExternalStorageDirectory()
.toString() + File.separator
to get the directory and save the image.

VuDroid pdf viewer cannot find Bitmap to render or it doesnt render

I am trying to use the VuDroid PDF viewer and I need to take the rendered bitmap and store it as a byte[]. Then I need to convert it back into a Bitmap that can be displayed on a view using something like "canvas.drawBitmap(bitmap, 0, 0, paint);".
I have spent many hours trying to access the Bitmap and I might have done it already, but even if I get the byte[] to return something it still wont render as a Bitmap on the canvas.
Could someone please help me here, I must be missing something. Thank you so much.
I believe it is supposed to accessed via...
PDFPage.java .... public Bitmap renderBitmap(int width, int height, RectF pageSliceBounds)
-or-
through Page.java -or- DocumentView.java -or- DecodeService.java
Like I said I have tried all of these and have gotten results I just cannot see where I am going wrong since I cannot render it to see if the Bitmap was called correctly.
Thank you again :)
The doc says the method returns "null if the image could not be decode." You can try:
byte[] image = services.getImageBuffer(1024, 600);
InputStream is = new ByteArrayInputStream(image);
Bitmap bmp = BitmapFactory.decodeStream(is);
I think This will help you:-
Render a byte[] as Bitmap in Android
How does Bitmap.Save(Stream, ImageFormat) format the data?
Copy image with alpha channel to clipboard with custom background color?
if you want to get each pdf page as independent bitmap you should consider that
VuDroid render the pages,
PDFView only display them.
you should use VuDroid functions.
now you can use this example and create your own codes
Example code : for make bitmap from a specific PDF page
view = (ImageView)findViewById(R.id.imageView1);
pdf_conext = new PdfContext();
PdfDocument d = pdf_conext.openDocument(Environment.getExternalStorageDirectory() + "your PDF path");
PdfPage vuPage = d.getPage(1); // choose your page number
RectF rf = new RectF();
rf.bottom = rf.right = (float)1.0;
Bitmap bitmap = vuPage.renderBitmap(60, 60, rf); //define width and height of bitmap
view.setImageBitmap(bitmap);
for writing this bitmap on SDCARD :
try {
File mediaImage = new File(Environment.getExternalStorageDirectory().toString() + "your path for save thumbnail images ");
FileOutputStream out = new FileOutputStream(mediaImage);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
for retrieve saved image:
File file = new File(Environment.getExternalStorageDirectory().toString()+ "your path for save thumbnail images ");
String path = file.getAbsolutePath();
if (path != null){
view = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(path), YOUR_X, YOUR_Y, false);
}
Try this code to check whether bitmap is properly generating or not
PdfContext pdf_conext = new PdfContext();
PdfDocument d = (PdfDocument) pdf_conext.openDocument(pdfPath);
PdfPage vuPage = (PdfPage) d.getPage(0);
RectF rf = new RectF();
Bitmap bitmap = vuPage.renderBitmap(1000,600, rf);
File dir1 = new File (root.getAbsolutePath() + "/IMAGES");
dir1.mkdirs();
String fname = "Image-"+ 2 +".jpg";
File file = new File (dir1, fname);
if (file.exists ())
file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}

Categories

Resources