I am trying to get an image from phone's secondary storage.
public void downloadImage(View v) {
Bitmap myImage = GetImageBitmapFromUrl();
String path = System.getenv("SECONDARY_STORAGE") + "/";
OutputStream out = null;
try
{
File file = new File(path, "nameImage.jpg");
out = new FileOutputStream(file);
myImage.compress(Bitmap.CompressFormat.JPEG, 85, out);
out.flush();
out.close();
}
catch (Exception e)
{
}
}
When I run with debug why I get this error and it didn't receive the file also.
Error at:
myImage.compress(Bitmap.CompressFormat.JPEG, 85, out);
In the manifests file:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
String photoPath = Environment.getExternalStorageDirectory()+"/photo.jpg";
and get bitmap by using code below.
The photo.jpg is the the name of the image you want to read.
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(photoPath, options);
You can also use picasso image library
Picasso.with(context).load("file:///android_asset/DvpvklR.png").into(imageView2);
Picasso.with(context).load(new File(...)).into(imageView3);
Related
I want to save an image to SECONDARY_STORAGE (Not saved to phone memory, which android understands is SdCard ), this is code:
In the manifests file:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
Java code:
public void Clickdownload(View v) {
Bitmap myImage = GetImageBitmapFromUrl();
String path = System.getenv("SECONDARY_STORAGE") + "/";
OutputStream out = null;
try {
File file = new File(path, "nameImage.jpg");
out = new FileOutputStream(file);
myImage.compress(Bitmap.CompressFormat.JPEG, 85, out);
out.flush();
out.close();
} catch (Exception e) {
}
}
When I run with Debug I get this error
Error at:
myImage.compress(Bitmap.CompressFormat.JPEG, 85, out);
and it didn't save.
This is the code for reading image from sd card :
String path =System.getenv("SECONDARY_STORAGE")+"/myImage/";
File imgFile = new File(path);
if(imgFile.exists())
{
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView imageView=(ImageView)findViewById(R.id.imageView);
imageView.setImageBitmap(myBitmap);
}
When I use:
String path = Environment.getExternalStorageDirectory().toString();
I can save an image to the phone memory.
I can save images from my camera and copy images from my computer on my SdCard, so I do not think this is a Read only SdCard. I tried with many types SdCards but it does not work.
I am using android 5.1.1.
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" />
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.
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);
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.