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();
}
}
Related
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;
}
I have a .tar.gz file and I want to untar that same file.
To do that I have this function in java:
private void unTarFile(String tarFile, File destFile) {
TarArchiveInputStream tis = null;
FileInputStream fis = null;
GZIPInputStream gzipInputStream = null;
BufferedInputStream bis = null;
try {
fis = new FileInputStream(tarFile);
bis = new BufferedInputStream(fis);
// .gz
gzipInputStream = new GZIPInputStream(bis);
// .tar.gz
tis = new TarArchiveInputStream(gzipInputStream);
TarArchiveEntry tarEntry = null;
while ((tarEntry = tis.getNextTarEntry()) != null) {
System.out.println(" tar entry- " + tarEntry.getName());
if (tarEntry.isDirectory()) {
continue;
} else {
// In case entry is for file ensure parent directory is in place
// and write file content to Output Stream
File outputFile = new File(destFile + File.separator + tarEntry.getName());
outputFile.getParentFile().mkdirs();
IOUtils.copy(tis, new FileOutputStream(outputFile));
}
}
} catch (IOException ex) {
System.out.println("Error while untarring a file- " + ex.getMessage());
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (gzipInputStream != null) {
try {
gzipInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (tis != null) {
try {
tis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
This works and I can untar my .tar.gz successfully.
I can edit the files of the untarred directory to, but when I try to delete my directory I cant:
Did I forgot to close something?
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?
Yeah, so I am using this code but whenever I run it, it doesnt upload anything. I have a similar code in another program the works perfect. What have I missed?
public void upload(){
new Thread(new Runnable() {
public void run() {
if (Looper.myLooper() == null)
{
Looper.prepare();
}
FTPClient ftpClient = new FTPClient();
FileInputStream inputStream = null;
int Upload = sharedPreferences.getInt("Upload", 1);
if (Upload == 1) {
try {
ftpClient.connect(InetAddress.getByName("XXX.net"));
ftpClient.login("XXX", "XXX");
ftpClient.changeWorkingDirectory("/public_html/Images/Cross");
ftpClient.setFileType(FTP.BINARY_FILE_TYPE, FTP.BINARY_FILE_TYPE);
File file = new File(getApplicationInfo().dataDir + "/files/" + "temp" + ".jpg");
inputStream = new FileInputStream(file);
ftpClient.storeFile("temporary.jpg", inputStream);
file.delete();
ftpClient.logout();
ftpClient.disconnect();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
...
}
For some reason whenever I try to load a URL gotten from a GET request to a server it won't load but If i try to load the string directly it works. Here is my code:
new Thread(new Runnable() {
#Override public void run() {
try {
URL obj = new URL(url1 + overallid);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
con.addRequestProperty("User-Agent", "Mozilla/4.76");
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(response.toString().replaceAll("\\s", ""));
System.out.println("Set pic to: " + pic);
Picasso.with(LoginActivity.this).load(pic).into(image);
i++;
overallid++;
} catch (Exception ex) {
System.out.println(ex);
}
}
}).start();
If I make pic = a imgur link straight up it works but if I grab it from the GET it doesn't work. Any ideas?
Thanks,
Quinn(Fusion)
Picasso should be called from the Main Thread... try out this code:
Handler mainThreadHandler=new Handler();
new Thread(new Runnable() {
#Override
public void run() {
try {
URL obj = new URL(url1 + overallid);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
con.addRequestProperty("User-Agent", "Mozilla/4.76");
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(response.toString().replaceAll("\\s", ""));
System.out.println("Set pic to: " + pic);
mainThreadHandler.post(new Runnable() {
#Override
public void run() {
Picasso.with(LoginActivity.this).load(pic).into(image);
}
});
i++;
overallid++;
} catch (Exception ex) {
System.out.println(ex);
}
}
}).start();
Here is an example to handle the bitmap downloading.
BufferedInputStream inputStream = null;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
URL url = new URL("the image url to load");
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
inputStream = new BufferedInputStream(connection.getInputStream());
byte[] buffer = new byte[8192];
int n = -1;
while ((n = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, n);
}
final Bitmap bitmap = BitmapFactory.decodeByteArray(
outputStream.toByteArray(), 0, outputStream.size());
runOnUiThread(new Runnable() {
#Override
public void run() {
// TODO handle image here
}
});
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}