Display an image without saving it - android

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.

Related

Not getting full length of bytes from the Video

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");
}

Android make preview from images on web

I'm trying to make preview as Thumbnail from images on web before downloading, it means i have more item on RecyclerView and i'm trying to view simple blurred preview from images but i dont know whats solution, i use this below code but i think this code download full image, like with social application such as whatsApp or Telegram how can i make preview image on web and show that to users?
new Thread(new Runnable() {
#Override
public void run() {
//First create a new URL object
URL url = null;
try {
if (checkNetWorkConnection()) {
url = new URL("http://wallpaperwarrior.com/wp-content/uploads/2016/09/Wallpaper-2.jpeg");
//Next create a file, the example below will save to the SDCARD using JPEG format
File file = new File(APP.DIR_APP + APP.IMAGE + "/" + "Wallpaper-2.jpg");
//Next create a Bitmap object and download the image to bitmap
Bitmap bitmap = BitmapFactory.decodeStream(url.openStream());
//Finally compress the bitmap, saving to the file previously created
bitmap.compress(Bitmap.CompressFormat.JPEG, 50, new FileOutputStream(file));
}
} catch (MalformedURLException e) {
e.printStackTrace();
Log.e("Error ", e.getMessage());
} catch (FileNotFoundException e) {
e.printStackTrace();
Log.e("Error ", e.getMessage());
} catch (IOException e) {
e.printStackTrace();
Log.e("Error ", e.getMessage());
}
}
}).start();
You can use picasso lib.
Picasso
.with(context)
.load("http://wallpaperwarrior.com/wp-content/uploads/2016/09/Wallpaper-2.jpeg")
.resize(600, 200)
.centerInside()
.into(imageViewResizeCenterInside);
You can give circle shape to image using Transformation api of picasso.
refer this Picasso
You can try implement with this zoom effect. It is being use by many application. With the example you can create your own background like full screen black background but you need to implement it yourself which is pretty simple but kinda lot of work to do.
Beside why not try to use Glide or Picasso to download and display the image? It help you save a lot work such as caching, download in asyncs,resize etc.
When you work with image especially with recycleview or listview there is some problem you need to always remember. Every time you scroll the listView/RecycleView it actually re-download the image so you have to really check wether it already exist in your sdcard to avoid this problem beside there is better to cache is memory although you already save it on sd card but still you can avoid memory crash by cache it in memory. Those hard work done by using either one of the library I just show you.

Sent Image to another ImageView (in other activity)

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);

uploading high quality image on parse.com

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.

how to capture listview data as Jpg image

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.

Categories

Resources