How I will display latest image to ImageView Another Activity? - java

My Application takes a picture from custom camera and save image to external storage.I save image by use code this.
SimpleDateFormat formatter = new SimpleDateFormat("dd_MM_yyyy_HH_mm", Locale.KOREA);
Date now = new Date();
String path = (Environment.getExternalStorageDirectory()+"/test"+".jpg");
String Newpath = (Environment.getExternalStorageDirectory()+"/"+"test_"+formatter.format(now)+".jpg");
Bitmap photo = BitmapFactory.decodeFile(path);
photo = Bitmap.createScaledBitmap(photo,480,640,false);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 70,bytes);
File f = new File(Newpath);
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
fo.close();
File file = new File(path);
file.delete();
} catch (IOException e) {
e.printStackTrace();
}
example result > "test_13_12_2019_15_24" , "test_13_12_2019_15_26" , "test_13_12_2019_15_28"
It's time to now in external storage after take a picture but I want it show "test_13_12_2019_15_28" (latest picture) to imageView in another activity.

You can read your file like this.
public StringBuilder fromInternal(String filename) {
StringBuilder sb = new StringBuilder();
try {
FileInputStream fileInputStream = context.openFileInput(filename);
InputStreamReader inputStreamReader = new
InputStreamReader(fileInputStream, "UTF-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String line;
while ((line = bufferedReader.readLine()) != null) {
sb.append(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return sb;
}

Related

Images corrupt on local storage after bringing them in from a URL

So I have it that when an in-app item is purchased the image they purchased is downloaded onto the phone. Works fine on external storage but I want it on INTERNAL storage. I'm using Picasso to load it into a bitmap then put into a PNG file. I'm not sure why it's getting corrupted along the way on internal storage. When the SD card is perfectly fine.
Anyone have any ideas why this is happening? Thanks!
public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
new Thread(new Runnable() {
#Override
public void run() {
//Internal storage
String path = getFilesDir().getAbsolutePath();
File imageFile = new File(path, "image.png");
FileOutputStream fos = null;
//TO SD Storage
File file = new File(
Environment.getExternalStorageDirectory().getPath());
File fileName = new File(file, "scrone.png");
if (isSDPresent) {
if (!fileName.getName().equals("scrone.png")) {
try {
FileOutputStream ostream = new FileOutputStream(fileName);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, ostream);
ostream.close();
} catch (Exception e) {
e.printStackTrace();
}
} else if (fileName.getName().equals("scrone.png")) {
try {
fileName = new File(file, "scrtwo.png");
FileOutputStream ostream = new FileOutputStream(fileName);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, ostream);
ostream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
//I'm sorry for the mess you're about to witness
else if(!imageFile.exists()){
try {
Log.d(TAG, "File doesn't exist");
fos = new FileOutputStream(imageFile);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
else if(imageFile.exists())
{
try
{
Log.d(TAG, "File does exist");
imageFile = new File("image2.png");
fos = new FileOutputStream(imageFile);
bitmap.compress(Bitmap.CompressFormat.PNG, 75, fos);
fos.close();
}catch(Exception e)
{
e.printStackTrace();
}
}
}
}).start();
}

Java save mp3 file

I am trying to create import images and mp3 files from one directory using a file chooser and save them to another . The images went fine but I cant seem to find out how to save the mp3 file .
Images
#Override
public void saveFile(File file) {
//Get image path
String imagePath = file.getAbsolutePath();
String imageName = file.getName();
System.out.println(imagePath);
//Read image
try {
bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
bufferedImage = ImageIO.read(new File(imagePath));
System.out.println("Reading complete.");
} catch (IOException e) {
System.out.println("Error: " + e);
}
//write image
try {
f = new File("H:\\TestFolder\\images\\" + imageName); //output file path
ImageIO.write(bufferedImage, "jpg", f);
System.out.println("Writing complete.");
} catch (IOException e) {
System.out.println("Error: " + e);
}
}
Mp3
#Override
public void saveFile(File file) {
try{
f = new File(file, "H:\\TestFolder\\test.mp3"); //file.getAbsolutePath();
}catch (Exception e) {
e.printStackTrace();
}
}
Use Files.copy(source, target, REPLACE_EXISTING);
https://docs.oracle.com/javase/tutorial/essential/io/copy.html
Try it like this:
File f = new File("H:\\TestFolder\\test.mp3");
InputStream is = new FileInputStream(f);
OutputStream outstream = new FileOutputStream(new File("H:\\TestFolder2\\blabla.mp3"));
byte[] buffer = new byte[4096];
int len;
while ((len = is.read(buffer)) > 0) {
outstream.write(buffer, 0, len);
}
outstream.close();

Read form ZipDecryptInputStream and write it to OutputStream

public void decrypt(String inputFile, String password) {
ZipDecryptInputStream zipDecrypt = null;
try {
zipDecrypt = new ZipDecryptInputStream(new FileInputStream(
inputFile), password);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
File file = new File("outouttfile.tsv");
OutputStream fop = null;
try {
fop = new FileOutputStream(file);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
try {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = zipDecrypt.read(buffer)) != -1) {
fop.write(buffer, 0, bytesRead);
System.out.println("Written");
}
} catch (IOException e) {
e.printStackTrace();
}
}
My while loop becomes an infinite loop and does not stop reading the file even its read once. Any idea why?

How to convert a file to pdf using pdfbox in java?

I want to extract all the content from a .docx or .txt file and then create a new pdf document which will contain the exact content. The problem is that I want to extract the content with the exact font style , if it's bolded or italic like so...This is what I've done so far:
public class Main {
public static void main(String[] args) {
JFileChooser fileChooser = new JFileChooser();
File file = null;
do {
int returnedValue = fileChooser.showOpenDialog(null);
if (returnedValue == JFileChooser.APPROVE_OPTION) {
file = fileChooser.getSelectedFile();
} else break;
} while (!file.getAbsolutePath().endsWith(".txt") &&
!file.getAbsolutePath().endsWith(".docx"));
Controller controller = new Controller();
controller.createPDF(file);
}
}
public class Controller {
public void createPDF(File file) {
String text = readFileContent(file);
System.out.println(text);
try {
newPdf(text, file);
} catch (IOException e) {
e.printStackTrace();
} catch (COSVisitorException e) {
e.printStackTrace();
} catch (FontFormatException e) {
e.printStackTrace();
}
}
private void newPdf(String text, File file) throws IOException, COSVisitorException, FontFormatException {
PDDocument doc = new PDDocument();
PDPage page = new PDPage();
doc.addPage(page);
PDPageContentStream contentStream = new PDPageContentStream(doc, page);
PDFont font = PDType1Font.COURIER;
contentStream.setFont(font, 14);
contentStream.beginText();
contentStream.setNonStrokingColor(Color.black);
contentStream.moveTextPositionByAmount(20, 750);
contentStream.drawString(text);
contentStream.endText();
contentStream.close();
doc.save("doc.pdf");
doc.close();
}
private String readFileContent(File file) {
String finalText = new String();
try {
FileReader fileReader = new FileReader(file.getAbsolutePath());
BufferedReader br = new BufferedReader(fileReader);
String line;
while ((line = br.readLine()) != null)
finalText += line;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return finalText;
}
}

making request spotify

I need help with getting information from spotify. How from this link : https://api.spotify.com/v1/search?q=Songs+of+Innocence&type=album
Can I take url:
"height" : 64,
"url" : "https://i.scdn.co/image/eb740cef5aa3d5e119baf868bdff2dbb5cc1a59b",
"width" : 64
And assign url to variable s in my code below:
public void getCover(String album) {
String query = "https://api.spotify.com/v1/search?q="+ encodeField(album)+"=album";
java.net.URL url = null;
try {
BufferedImage image = null;
url = new java.net.URL(query);
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
InputStream is = null;
try {
is = url.openStream();
} catch (IOException e) {
e.printStackTrace();
}
// get the text from the stream as lines
java.io.BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String s;
try {
s = reader.readLine()) // read link with image 64x64 resolution
} catch (IOException e) {
e.printStackTrace();
}
try {
//URL url2 = new URL("https://i.scdn.co/image/eb740cef5aa3d5e119baf868bdff2dbb5cc1a59b");
URL url2 = new URL(s);
image = ImageIO.read(url2);
ImageIcon icon = new ImageIcon(image);
jLabel2.setIcon(icon);
} catch (IOException e) {
e.printStackTrace();
}
}

Categories