How to create File from Image Url without download Image - android

How to create File from Imageurl?
I know How to
Download Image from Url using AsyncTask
I want to know about how can I create File object without download Image from url.
I tried like below.
File file = new File("http://www.planwallpaper.com/static/images/Winter-Tiger-Wild-Cat-Images.jpg");
I want to use this file in status.setMedia(file)
StatusUpdate status = new StatusUpdate(twitterMessage);
status.setMedia(file );
try {
twitter4j.Status response = twitter.updateStatus(status);
return response.toString();
} catch (TwitterException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
But no success.
Please help me about create File direct from imageurl without download Image.

How to create File from Imageurl?
You don't. File is for local files. A URL is for something that is stored on a server, and particularly in this case, that server is not the device itself, and so the URL does not point to a local file... unless you download a copy of that content to a local file.
I want to use this file in status.setMedia(file)
See if that API offers other parameter types than File, like Uri, that the library would use to support network-based sources of the media. If not, you have no choice but to download the data and provide a File pointing to your local copy.

you means show the image with a default image,but not really download.
then,when click on it,download it.right?
you can request JSON info from server then store the ImagerUrls into a list.when the user click the imageView ,then you can take out the ImagerUrl and download the image with volley or AsyncTask.

Related

How to get the progress of copy operation in AWS copy operation in android?

I try to copy one file from one folder to other folder inside the same bucket. For copying the file i use CopyObjectRequest class. But i do not know how can i get the progress of copy operation?
So please help me in it , how can i get the progress status of copy operation.
For copy operation i followed this way-
try {
AmazonS3 s3Client =new AmazonS3Client(credentials,cc);
// Copy the object into a new object in the same bucket.
CopyObjectRequest copyObjRequest = new CopyObjectRequest(bucketName, sourceKey, "wedorias-new", "test/test111/logo.png"/*destinationKey*/);
s3Client.copyObject(copyObjRequest);
if (copyObjRequest.isRequesterPays()){
System.out.println("sadfbgnh==");
}
} catch (AmazonServiceException e) {
// The call was transmitted successfully, but Amazon S3 couldn't process
// it, so it returned an error response.
e.printStackTrace();
System.out.println("exception==!"+e.getErrorMessage());
System.out.println("exception==!"+e.getErrorCode());
System.out.println("exception==!"+e.getMessage());
} catch (Exception e) {
e.printStackTrace();
System.out.println("exception==!"+e.getMessage());
}
If you want to copy one object from one folder to another one (Or other Buckets if it's the case) and check the progress, you will have to use Amazon S3 Multipart Upload.
AWS docs have a nice example called Copy an Object Using the AWS SDK for Java Multipart Upload API, it shows you what you have to do to set it up and check the progress of the copy.
// Get the object size to track the end of the copy operation.
GetObjectMetadataRequest metadataRequest = new GetObjectMetadataRequest(sourceBucketName, sourceObjectKey);
ObjectMetadata metadataResult = s3Client.getObjectMetadata(metadataRequest);
long objectSize = metadataResult.getContentLength();
Notice
Multipart upload is a must when the object is greater than 5GB.
Multipart upload is recommended when the object is greater than 100MB.
Multipart upload doesn't work if the object is less than 5MB.

How to cache audio for offline use in Android Studio

I am developing an Android application in which I need to get the specified audio file from my website when the user plays it, but I don't want to stream it or download it every time, just the first time. So I was thinking of caching it and play offline whenever the user is in need. So please suggest any method to do so. Or if exists any other method rather than caching like downloading the actual file to file storage and play whenever needed.
If you need to cache files, you should use createTempFile(). For example, the following method extracts the file name from a URL and creates a file with that name in your app's internal cache directory:
private File getTempFile(Context context, String url) {
File file;
try {
String fileName = Uri.parse(url).getLastPathSegment();
file = File.createTempFile(fileName, null,
context.getCacheDir());
} catch (IOException e) {
// Error while creating file
}
return file;
}
You can also see here for more about caching files.
https://developer.android.com/training/data-storage/files.html#WriteCacheFileInternal
Hope this will help.

Google Drive API - download quota exceeded

I'm getting an error:
The download quota for this file has been exceeded
This is a response from the drive api. I'm trying to download a file simply via following url:
String url = "https://www.googleapis.com/drive/v2/files/" + media.getId() + "?alt=media\n"
My file is a few MBs big and I just can't download it. I'm using scribe and oauth2, I create a request, sign it and send it. The response shows me that the signature works, but I don't know why I always get the above error response from google...
Other things like retrieving a list of all my files and my user work just fine and work multiple times as well...
Since you're using Android, why not try to call it using the Java API client, as discussed in the Download Files section of the documentation.
private static InputStream downloadFile(Drive service, File file) {
if (file.getDownloadUrl() != null && file.getDownloadUrl().length() > 0) {
try {
// uses alt=media query parameter to request content
return service.files().get(file.getId()).executeMediaAsInputStream();
} catch (IOException e) {
// An error occurred.
e.printStackTrace();
return null;
}
} else {
// The file doesn't have any content stored on Drive.
return null;
}
}
Another alternative is to use the webContentLink to retrieve the download URL.
Hope this helps!
Log in multcloud
Add Cloud (Google Drive) & Login and Allow Drive
select the file and Share get public url

How can I copy a remote image over http to gallery folder in android?

I want to copy a remote image, for example "http://example.com/example.jpg" to the android user phone built gallery...How can I do it?
To that, you should download the image and save it in internal memory.
You can download the image by yourself:
public static Bitmap getBitmap(String url) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Bitmap d = BitmapFactory.decodeStream(is);
is.close();
return d;
} catch (Exception e) {
return null;
}
}
Code from here But you will have memory problems with large images. I strongly recommended you to use a build library like Android Universal Image Loader or Picasso from square
Here you can find an example of how to use the Android DownloadManager to download your file.
The destination path can be determined using the contants defined in the Environment class. Constant DIRECTORY_DCIM points to the parent directory under which all Activities can create a custom folder where they store their images. You could make your own child folder as destination folder
When your image finishes downloading, you will notice that it will not be listed in the default gallery application, this is because Android builds an index with all the media files and is still unaware of your new downloaded image. This index is updated each time you boot your Android device, but since it's a bit unconvienient to reboot your device each time a file is added, you can also codewise inform the indexing service that a new file is created and needs indexing using this piece of code:
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(file));
sendBroadcast(intent);
This scanning should also occur after a file has been erased.

get size of file before download from amazon s3 in android app

I have developed an app in which I have an ASyncTask that downloads a file from Amazon s3 server.
As the related files are quite large sized, I need to show a progress bar when the download happens and a spiral progress bar doesn't look good here when it keeps rotating and irritates the user.
I need to use a horizontal style progress bar for which I need to know the size of the file to be downloaded.
Does anybody know of any way to know the file size before downloading it from amazon s3.
This is the downloading code that I have used..
#Override
protected Void doInBackground(Void... params) {
TransferManager manager = new TransferManager(access);
File file = new File(Environment.getExternalStorageDirectory()+"/Downloads", downFile);
fileDownloaded = "/Downloads/"+ downFile;
file.setWritable(true);
Download down = manager.download("files", fileToDown, file );
try {
down.waitForCompletion();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
'downFile' is the file location where I am saving it on external directory.
'fileToDown' is the file path on server that is to be downloaded.
'/Downloads/' is the new directory that I have created to store files on device.
I would assume you are using a code something like this,
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
// getting file length
int lengthOfFile = conection.getContentLength();
// input stream to read file - with 8k buffer
InputStream input = new BufferedInputStream(url.openStream(), 8192);
the method getContentLength() will return to you the size of the file.
UPDATE 1:
I checked the java docs of Amazon about the TransferManager and Download class. There were method and listener that might be useful to display your progress and try using them.
http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/s3/transfer/Transfer.html#getProgress()
http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/s3/transfer/TransferProgress.html
getProgress() //This will return TransferProgress object. See the links above
http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/s3/transfer/Transfer.html#addProgressListener(com.amazonaws.services.s3.model.ProgressListener)
addProgressListener
This will able you I think to set up a progressListener.
UPDATE 2:
TransferProgress is an object which has bunch of informations about the progress of your download. It has bytes transferred, total bytes transferred and transfer percentage. Look at the methods detail.

Categories

Resources