서버가 있습니다 (GlassFish 사용). http를 사용하여 Json 또는 XML 등을 Android 장치에 보낼 수 있습니다. 내 안드로이드 장치에서 서버로 사진을 업로드하는 예제를 보았습니다. 선택한 이미지를 바이트로 변환하고 문자열로 변환 한 다음 서버에서 다시 변환합니다. 그래서 내 PC (서버)에 넣을 수 있습니다. 이제는 그 반대입니다. 내 PC에서 사진을 가져오고 URL을 사용하여 이미지 (여기에서 비트 맵)를 imageview로 가져옵니다. 그러나 디버깅으로 bmp는 "null"인 것 같습니다. Google은 내 이미지가 유효한 비트 맵이 아니기 때문에 말합니다 (서버 인코딩에 문제가 있습니까?). 이 코드를 작동하려면 무엇을 변경해야합니까?
서버 코드 :
public class getImage{
String imageDataString = null;
@GET
@Path("imageid/{id}")
public String findImageById(@PathParam("id") Integer id) {
//todo: schrijf een query voor het juiste pad te krijgen!
System.out.println("in findImageById");
File file = new File("C:\\Users\\vulst\\Desktop\\MatchIDImages\\Results\\R\\Tensile_Hole_2177N.tif_r.bmp");
try{
// Reading a Image file from file system
FileInputStream imageInFile = new FileInputStream(file);
byte imageData[] = new byte[(int) file.length()];
imageInFile.read(imageData);
// Converting Image byte array into Base64 String
imageDataString = Base64.encodeBase64URLSafeString(imageData);
imageInFile.close();
System.out.println("Image Successfully Manipulated!");
} catch (FileNotFoundException e) {
System.out.println("Image not found" + e);
} catch (IOException ioe) {
System.out.println("Exception while reading the Image " + ioe);
}
return imageDataString;
}
}
그리고 이것은 안드로이드 측 (안드로이드 스튜디오)입니다.
public class XMLTask extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... urls) {
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
java.net.URL url = new URL(urls[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
return buffer.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(String line) {
super.onPostExecute(line);
byte[] imageByteArray = Base64.decode(line , Base64.DEFAULT);
try {
Bitmap bmp = BitmapFactory.decodeByteArray(imageByteArray, 0, imageByteArray.length);
ivFoto.setImageBitmap(bmp);
}catch (Exception e){
Log.d("tag" , e.toString());
}
}
}
시도해 보셨습니까 HttpURlConnection
?
다음은 샘플 코드입니다.
private class SendHttpRequestTask extends AsyncTask<String, Void, Bitmap> {
@Override
protected Bitmap doInBackground(String... params) {
try {
URL url = new URL("http://xxx.xxx.xxx/image.jpg");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
}catch (Exception e){
Log.d(TAG,e.getMessage());
}
return null;
}
@Override
protected void onPostExecute(Bitmap result) {
ImageView imageView = (ImageView) findViewById(ID OF YOUR IMAGE VIEW);
imageView.setImageBitmap(result);
}
}
내가 도울 수 있기를 바랍니다
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다