I'm currently developing an Android Application. My current progress is that I successful develop custom android camera. I followed this step (http://courses.oreillyschool.com/android2/CameraAdvanced.html) The tutorial given saved the picture taken into the gallery, but I want to insert the name, description, and other information of the Image because I'm going to save the image along with the details that the user enter into my database.
Here for example on my interface:
a) Success taken Image:
b) The image that need to be pass to another Imageview (red circle):
I want the save button able to pass the image that had been taken to the ImageView(red circle) and not to store the image into the gallery. And here's are the code on the save button:
private View.OnClickListener mSaveImageButtonClickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
File saveFile = openFileForImage();
if (saveFile != null) {
saveImageToFile(saveFile);
} else {
Toast.makeText(Capturingimage.this, "Unable to open file to save the image.", Toast.LENGTH_LONG).show();
}
}
};
This is save image to file method:
private void saveImageToFile(File file) {
if (mCameraBitmap != null) {
FileOutputStream outStream = null;
try {
outStream = new FileOutputStream(file);
if (!mCameraBitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream)) {
Toast.makeText(Capturingimage.this, "Unable to save image to file.",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(Capturingimage.this, "Saved image to: " + file.getPath(),
Toast.LENGTH_LONG).show();
}
outStream.close();
} catch (Exception e) {
Toast.makeText(Capturingimage.this, "Unable to save image to file.",
Toast.LENGTH_LONG).show();
}
}
}
My ImageView(redCircle) id is :
#+id/image_view_after_capture
If you guys aren't very clear with the codes, here's the link on the full source code (http://courses.oreillyschool.com/android2/CameraAdvanced.html) on MainActivity.java. I'm sorry if my question is a lil bit messy and less explanation on the codes. I'm new to android programming, I hope you guys can teach me. I really appreciate your time and help to consider to help me.
Thank you in Advance!
when you save image than hold your captured image path and pass to another activity where you want to show
String mCaptureImagePath="";
private void saveImageToFile(File file) {
if (mCameraBitmap != null) {
FileOutputStream outStream = null;
try {
outStream = new FileOutputStream(file);
if (!mCameraBitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream)) {
Toast.makeText(Capturingimage.this, "Unable to save image to file.",
Toast.LENGTH_LONG).show();
} else {
mCaptureImagePath=file.getPath(); //**** this is your save image path
Toast.makeText(Capturingimage.this, "Saved image to: " + file.getPath(),
Toast.LENGTH_LONG).show();
}
outStream.close();
} catch (Exception e) {
Toast.makeText(Capturingimage.this, "Unable to save image to file.",
Toast.LENGTH_LONG).show();
}
}
}
After show image where you want like this
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(mCaptureImagePath,bmOptions);
yourImageView.setImageBitmap(bitmap);
Related
I'm trying to save the frame of the image from the live stream video. So, I'm able to show live stream video from my android application as well as saving it in my local storage. Now, I want to use some sort of delay in my saveImage function so that images get saved after some specific time. I have used both Handler and TimerTask. The image is getting saved in my local directory after some delay but the length of the image that I get is sometimes very small, the other time normal. I want my saved image to exactly the length of the video stream that I am getting in my application.
I hope the question I asked is easy to understand. I am a beginner in both android and stack overflow.
P.S - I used mjpeg library for showing the video. The video I am getting is from an IP Camera.
Code for saving the images
public void saveImage(){
try {
photo =
new File(Environment.getExternalStorageDirectory(),
"Download/photos/photo" + Instant.now().getEpochSecond() + ".jpg");
if (photo.exists()) {
photo.delete();
}
System.out.println("Photo " + photo);
FileOutputStream fos = new FileOutputStream(photo.getPath());
System.out.println("Image_length" +image.length);
fos.write(image);
fos.close();
} catch (IOException e) {
Log.e("PictureDemo", "Exception in photoCallback", e);
}
}
Code where saveImage is called
final Bitmap outputImg = BitmapFactory.decodeByteArray(image, 0, image.length);
if (outputImg != null) {
if (run) {
newFrame(outputImg);
// Saving frames in internal Storage
new Timer().schedule(
new TimerTask() {
#Override
public void run() {
saveImage();
}
},5000
);
}
} else {
Log.e(tag, "Read image error");
}
My app can download an image from a raspberry. it works fine. This is the code
public void downloadFile() {
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect("******");
ftpClient.login("****","*****");
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
String remoteFile1;
File downloadFile1 = new File(filePath);
OutputStream outputStream1 = new BufferedOutputStream(new FileOutputStream(downloadFile1));
boolean success = ftpClient.retrieveFile(remoteFile1, outputStream1);
outputStream1.close();
if (success) {
System.out.println("File #1 has been downloaded successfully.");
} else {
System.out.println("Error in downloading file !");
}
boolean logout = ftpClient.logout();
if (logout) {
System.out.println("Connection close...");
}
} catch (IOException ex) {
System.out.println("Error: " + ex.getMessage());
ex.printStackTrace();
} finally {
try {
ftpClient.disconnect();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
And then I can display it so the user of my app can see it. For the image loading, Im using this code and it works too.
private void loadImage(String imagePath) {
Uri imageUri;
String fullImagePath;
Drawable image;
ImageView imageDisplay;
imageUri = Uri.parse(imagePath);
fullImagePath = imageUri.getPath();
image = Drawable.createFromPath(fullImagePath);
imageDisplay=(ImageView) findViewById(R.id.imageDisplay);
imageDisplay.setImageDrawable(image);
}
Now I want to display the image without downloading it in my gallery. But I can't figure out how to do this.
Can someone help me please.
You cannot show an image without download it. Actually when you see something "remotely", you are downloading it.
If you mean that the image is too large and you don't want to download, but want a mechanism for the user can view it. One possible solution is make a thumbnail (reduced image) in server side and show that "preview" to the user. Then if the user want to download it to the gallery you could get the original image.
If you want to display an image without downloading it, it has to be uploaded in a image hosting site or alike so you will just use the link instead of the whole FTP Client.
Basically, you are using a code that is intended for saving an image. And the one you are using for loading the images fetches data from the Drawable. So you are in the wrong path.
Situation:
I have an activity that contains an image picker function to select images. Once selected, these images are loaded/displayed in a GridView. I load the images using ImageLoader library with the following method:
ImageLoader.getInstance().displayImage("file://"+image.path, Utility.displayImageOptions);
In the same activity i have a Preview button which clicked lead to an another new activity. This new activity also contains a GridView using the same adapter code as in the previous activity. However, this GridView contains the compressed version of the images that were selected previously.
In my compression i save these bitmaps and create new files that are loaded in the second activity.
saveBitmapToFile(bitmap, index);
private void saveBitmapToFile(Bitmap bitmap, int imageIndex){
try {
bitmapFile = getPermanentFile(imageIndex);
FileOutputStream fos = new FileOutputStream(bitmapFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
Images image = new Images(bitmapFile.getAbsolutePath(), true, false);
compressedImageList.add(image);
}
catch (IOException e){
e.printStackTrace();
}
}
private static File getPermanentFile(int imageIndex) {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
File file = new File(Environment.getExternalStorageDirectory(), TEMP_PHOTO_FILE + String.valueOf(imageIndex) + ".jpg");
try {
if (file.exists()){
Log.v("File exists", file.getName());
file.delete();
file = new File(Environment.getExternalStorageDirectory(), TEMP_PHOTO_FILE + String.valueOf(imageIndex) + ".jpg");
}
file.createNewFile();
} catch (IOException e) {
}
return file;
} else {
return null;
}
}
The problem i have is that say I start with a fresh app (nothing stored in cache), select 4 images and click Preview, i get the compressed version of these four images. But then i close the app and start it again and i again select 4 different images and hit Preview BUT i get the same 4 compressed images that I got at the first time. Clearing the cache/data by going in settings resolves the problem but how can i do this in code.
I delete these files onBackPressed() but still the problem remains when i click back and then select images again.
#Override
public void onBackPressed(){
super.onBackPressed();
for (Images file : compressedImageList){
File f = new File(file.cardPath);
boolean deleted = f.delete();
if (deleted){
Log.v("File deleted : ", file.cardPath);
}
}
compressedImageList.clear();
}
Have You tried
imageLoader.clearMemoryCache();
and
imageLoader.clearDiscCache();
Whenever i upload any image to my database on parse.com, its size gets reduced. I have tried all possible ways to fix this issue. i tried saving the image in my sdcard and there, it was of proper size but when i try to view it in data browser of parse, it shows me a very small image, say of 50x50px. how can i fix this?
my saving to sd card code:
private void saveImage(Bitmap imgmap,ImageView imgview)
{
Calendar ci= Calendar.getInstance();
fileNameStr="sdcard/Sudhaar/"+ci.get(Calendar.YEAR)+"-"+(ci.get(Calendar.MONTH)+1)+"-"+ci.get(Calendar.DAY_OF_MONTH)+"_"+ci.get(Calendar.HOUR_OF_DAY)+"-"+ci.get(Calendar.MINUTE)+"-"+ci.get(Calendar.SECOND)+"-"+ci.get(Calendar.MILLISECOND)+".JPG";
try
{
FileOutputStream imgout=new FileOutputStream(fileNameStr);
imgData.compress(Bitmap.CompressFormat.JPEG,100,imgout);
imgout.close();
}
catch (FileNotFoundException e) {
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
}
}
My upload to parse code:
public void send_img(Bitmap imgData, String description)
{
ByteArrayOutputStream stream = new ByteArrayOutputStream();
imgData.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] img = stream.toByteArray();
ParseFile file = new ParseFile("image.jpg", img);
file.saveInBackground();
ParseObject complain = new ParseObject("complaint");
complain.put("description",description);
complain.put("image", file);
complain.saveInBackground(new SaveCallback() {
public void done(ParseException e) {
if (e == null) {
Toast.makeText(getApplicationContext(),"Complaint Posted!", Toast.LENGTH_SHORT).show();
pDialog.dismiss();
finish();
} else {
pDialog.dismiss();
Toast.makeText(getApplicationContext(),"Sorry ! Please Try Again", Toast.LENGTH_SHORT).show();
}
}
});
}
}
Use a curl CLI interface to manually POST a file to parse.com
consult the 'rest api docs' section='uploading files'
Use curl/rest api to upload your file.
Save the parse file url that will be in the response from the prior POST.
use that url to download and to check the file length.
you will see that they are not changing the file size in a POST.
Then, you can either shift to the REST API in your app OR figure out where the SDK or your app code is doing the compression that you note in your post.
i need to capture listview data and convert it into jpg or png image format , then saved into sd card. I captured only the data which is visible in the screen , but i am unable to capture the data which is avaialble in the scrollview.
So, please guide me guide me how to implement this.
i am using the following code to capture the visible data.
View v1=btnCapture.getRootView();
public void gettingRootView(View v1)
{
if( v1 != null)
{
v1.setDrawingCacheEnabled(true);
v1.buildDrawingCache();
Bitmap bm = v1.getDrawingCache();
try
{
if ( bm != null )
{
Log.e("file","filepath");
savePhoto(bm);
}
}
catch(Exception e){e.printStackTrace();}
}
}
public void savePhoto(Bitmap bmp)
{
Log.e("save photo","save photo");
File fileFolder=new File(Environment.getExternalStorageDirectory(),"SMSREADING");
fileFolder.mkdir();
Calendar c=Calendar.getInstance();
try
{
File fileName=new File(fileFolder,c.getTimeInMillis()+".jpg");
FileOutputStream output=new FileOutputStream(fileName);
bmp.compress(Bitmap.CompressFormat.PNG,100,output);
}
catch(Exception ex){
ex.printStackTrace();
}
}
You can create HTML from your data using one of the many templating libraries out there like, if you have a String list Apache's Velocity might work well. After you create your HTML you can use java-html2image to convert your html to an image.