package com.k1.doctor.media;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;
import com.k1.doctor.media.DownloadManager.CompleteLintening;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
public class SaveManager {
public final static String ROOT_PATH = "data/data/com.k1.doctor/files";
public static void saveBitmap(final Context content, final byte[] b, final String id) {
final String name = UUID.randomUUID().toString();
new Thread(new Runnable() {
public void run() {
BufferedOutputStream stream = null;
// File rootfile = content.getFilesDir();
File imageFolder = new File(ROOT_PATH, "image/");
if (!imageFolder.exists()) {
imageFolder.mkdir();
}
File file = null;
if (id != null) {
file = new File(imageFolder + "/" + id + ".jpg");
} else {
file = new File(imageFolder + "/" + name + ".jpg");
}
FileOutputStream fstream = null;
try {
fstream = new FileOutputStream(file);
stream = new BufferedOutputStream(fstream);
stream.write(b);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (stream != null) {
stream.close();
fstream.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}).start();
}
//byte[] 转文件
public static void saveVoice(final Context content, final byte[] b, final String id) {
final String name = UUID.randomUUID().toString();
new Thread(new Runnable() {
public void run() {
BufferedOutputStream stream = null;
// File rootfile = content.getFilesDir();
File imageFolder = new File(ROOT_PATH, "voice/");
if (!imageFolder.exists()) {
imageFolder.mkdir();
}
File file;
if (id != null) {
file = new File(imageFolder + "/" + id + ".amr");
} else {
file = new File(imageFolder + "/" + name + ".amr");
}
FileOutputStream fstream = null;
try {
fstream = new FileOutputStream(file);
stream = new BufferedOutputStream(fstream);
stream.write(b);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (stream != null) {
stream.close();
fstream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}).start();
}
//文件转byte[]
public static byte[] getBytes(File file) {
byte[] buffer = null;
try {
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
byte[] b = new byte[1000];
int n;
while ((n = fis.read(b)) != -1) {
bos.write(b, 0, n);
}
fis.close();
bos.close();
buffer = bos.toByteArray();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return buffer;
}
public static String getImage(String path, String name, CompleteLintening l) {
File imageFolder = new File(ROOT_PATH, "image/");
if (!imageFolder.exists()) {
imageFolder.mkdir();
}
File file = new File(imageFolder + "/" + name + ".jpg");
if (file.length() == 0) {
file.delete();
DownloadManager.startLoad(path, "image", l);
return null;
} else {
return file.getAbsolutePath();
}
}
public static String getVoice(String path, String name, CompleteLintening l) {
File imageFolder = new File(ROOT_PATH, "voice/");
if (!imageFolder.exists()) {
imageFolder.mkdir();
}
File file = new File(imageFolder + "/" + name + ".amr");
if (file.length() == 0) {
file.delete();
DownloadManager.startLoad(path, "voice", l);
return null;
} else {
return file.getAbsolutePath();
}
}
}
package com.k1.doctor.media;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.os.Environment;
public class DownloadManager {
public interface CompleteLintening {
void completed(Object o);
}
private String Url = "";
public static void startLoad(final String path, final String flag, final CompleteLintening l) {
new Thread(new Runnable() {
@Override
public void run() {
try {
if (flag.equals("image")) {
byte[] b = load(path);
SaveManager.saveBitmap(null, b,"imager");
l.completed(b);
} else if (flag.equals("voice")) {
loadVoice(path);
// l.completed(b);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
public static void loadVoice(String path) throws Exception {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5 * 1000);
conn.setRequestMethod("GET");
InputStream inStream = conn.getInputStream();
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
}
}
public void streamToBytes(InputStream inStream, String flag) {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] data = new byte[5 * 1024];
int count = -1;
try {
while ((count = inStream.read(data, 0, 5 * 1024)) != -1)
outStream.write(data, 0, count);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
data = null;
if(flag.equals("voice")){
SaveManager.saveVoice(null, outStream.toByteArray(),"");
}else if(flag.equals("image")){
SaveManager.saveBitmap(null, outStream.toByteArray(),null);
}
}
public static byte[] load(String path) throws Exception {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5 * 1000);
conn.setRequestMethod("GET");
InputStream inStream = conn.getInputStream();
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
return readStream(inStream);
}
return null;
}
public static byte[] readStream(InputStream inStream) throws Exception {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
outStream.close();
inStream.close();
return outStream.toByteArray();
}
public Object getDate(String id) {
return Url;
}
}