I want to add an audio file to an image. So when someone gets that image he can extract the sound from it. I think we can add additional data to image header. But I don't know how to do that sort of header processing in Android. Can you please guide me...
Using the below code you can add a small comment to the JPEG header in Android. It's only for JPEG.
final static String EXIF_TAG = "UserComment";
public static boolean setComment(String imagePath, String comment) {
try {
ExifInterface exif = new ExifInterface(imagePath);
exif.setAttribute(EXIF_TAG, comment);
exif.saveAttributes();
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
But the problem is you can't put a audio file there because the data stream is way too big to fit in to the header.
Related
Im having a strange kind of problem, Im uploading an image to a folder and saving the name(the id of the user) and the extension in the database and its working fine but when i change the image, its changing in the folder but the image in the phone not changing though i am editing my SharedPrefManger which save the image name and the extension.
here is how Im changing the image in the folder and its working fine
String uploadId = UUID.randomUUID().toString();
String path = getPath(filepath);
try {
new MultipartUploadRequest(this,uploadId,Constants.URL_UPLOADPIC)
.addFileToUpload(path,"image")
.addParameter("id",SharedPrefManager.getInstance(this).getKeyUserId())
.setNotificationConfig(new UploadNotificationConfig())
.setMaxRetries(2)
.startUpload();
String extention = path.substring(path.lastIndexOf("."));
String id = SharedPrefManager.getInstance(getApplicationContext()).getKeyUserId();
SharedPrefManager.getInstance(getApplicationContext()).uploadpicmanager(id+extention);
Toast.makeText(Profile.this, path, Toast.LENGTH_LONG).show();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Here is the the uploadpicman which change the image name
public boolean uploadpicmanager(String id){
SharedPreferences sharedPreferences3 = mCtx.getSharedPreferences(SHARED_PREF_NAME,Context.MODE_PRIVATE);
SharedPreferences.Editor editor3 = sharedPreferences3.edit();
editor3.putString(KEY_IMAGE,id);
editor3.apply();
return true;
}
Here is how im reading the image and its working fine for the first upload but when i change the pic it doesn't change. Im wondering from where its getting the image if it is not in the folder anymore??
Picasso.with(getApplicationContext())
.load("http://hello.000webhostapp.com/Shop&Go/customers/"+
SharedPrefManager.getInstance(getApplicationContext()).getKeyImage())
.into(profilepic);
Probably Picasso load a cached version of the image.
Try to add
.memoryPolicy(MemoryPolicy.NO_CACHE)
on the builder
Of course this is not a good practise, you should change the path name of the image (and remove the memory policy).
I have a job that is rotating and trimming video files.
I do trimming the video but couldn't rotate it.
I use following code snippet to rotate but the result video is the same with the source video .Also there isn't any error messeage.
videoPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/download/cvbenim/islenecek.mp4";
try {
String rotatedPath = videoPath.replace(".mp4", "cvbenim_is_ilanı_rotated.mp4");
Movie result = MovieCreator.build(videoPath);
File file = new File(rotatedPath);
if (file.exists()) {
file.delete();
}
Container out = new DefaultMp4Builder().build(result);
MovieHeaderBox mvhd = Path.getPath(out, "moov/mvhd");
mvhd.setMatrix(Matrix.ROTATE_90);
out.writeContainer(new FileOutputStream(rotatedPath).getChannel());
playVideoFromPath(rotatedPath);
} catch (Exception e) {
e.printStackTrace();
}
I appriciate any help.
You don't state how you present your video in playVideoFromPath but as https://stackoverflow.com/a/17395134/3233251
says.
When you playback the video on Android with the help of the VideoView you might notice that the matrix is not taken into account. I'm not entirely sure if this is done on purpose or not but the workaround is to use a TextureView that applies the transformation.
So you should try to use the TextureView as recommended if you are not already doing so.
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.
My application captures video footage and saves it as .mp4 file. I would like to extract one frame from this video and also save it to file. Since I haven't found nothing better, I've decided to use MediaMetadataRetriever.getFrameAtTime() for that. It happens inside the class that inherits from AsyncTask. Here is how my code looks like my doInBackground():
Bitmap bitmap1 = null;
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
try {
retriever.setDataSource(src);
bitmap1 = retriever.getFrameAtTime(timeUs, MediaMetadataRetriever.OPTION_CLOSEST_SYNC);
if (Utils.saveBitmap(bitmap1, dst)) {
Log.d(TAG, "doInBackground Image export OK");
} else {
Log.d(TAG, "doInBackground Image export FAILED");
}
} catch (RuntimeException ex) {
Log.d(TAG, "doInBackground Image export FAILED");
} finally {
retriever.release();
if (bitmap1 != null) {
bitmap1.recycle();
}
}
And the saveBitmap() method:
File file = new File(filepath);
boolean result;
try {
result = file.createNewFile();
if (!result) {
return false;
}
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, ostream);
ostream.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
Now, the problem is that the quality of the exported image is noticeably worse then the video quality. I don't think that this should happen with PNG output format. You can see the difference below:
The first image was extracted from the video using ffmpeg on my desktop. The second one was extracted using the code above on Samsung Galaxy S6. The result looks pretty much the same on every Android device I was using.
Can someone tell how can I improve the quality of the exported picture?
I found other solution for the issue. You can use bigflake's example to build mechanism for extracting video frame. The one thing you will have to add is seeking mechanism. This works well, keeps the exact quality and does not require any third-party libraries. Only downside I've noticed so far is that it will result in longer execution time than the original idea.
I got this little function that allows me to load an image from a path, named name. It works, the problem is that I have to call this many times, una tantum. Let's say, a dozen of times at the load of a specific activity. It takes few seconds to load them all.
Is it optimal? Is there a lighter way to achieve the same result?
public static Bitmap loadImageFrom(File path, String name)
{
try {
File f = new File(path, name);
return BitmapFactory.decodeStream(new FileInputStream(f));
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
}
I tried adding if(!f.exists()) return null; like this:
public static Bitmap loadImageFrom(File path, String name)
{
try {
File f = new File(path, name);
if(!f.exists()) return null;
return BitmapFactory.decodeStream(new FileInputStream(f));
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
}
I know that is redundant, but I can't remove the Try Catch clause, cause it giving me an error if I do. However, no speed up noticed.
Any suggestion?
It's not that your code is inefficient, as far as I know, loading pictures manually on the main thread will hurt your app's performance. Especially when you have a large amount of pictures and/or using them to populate your ListView or GridView.
Have a look at Picasso or Universal Image Loader, these libraries should help load your pictures efficiently thus speeding up your app.