私はサーバーを持っています(私はGlassFishを使用しています)。私は私のアンドロイドデバイスにhttpでJsonやXMLなどを送ることができます。私はアンドロイドデバイスからサーバーに画像をアップロードする例を見ました。これは、私の選択した画像をバイトに変換し、Stringに変換して、私のサーバに戻します。だから私は自分のPC(サーバー)に置くことができます。 今私はちょうど反対が欲しい:私のPCから画像を取得し、URLを使って画像(ビットマップ)を画像ビューに取得します。しかし、デバッグbmpと "null"と思われる。私のイメージは有効なビットマップではないので、googleはそのことを言います(私のサーバーのエンコーディングでは何かが間違っていますか?)。 動作させるには、このコードを変更する必要がありますか?サーバーからアンドロイドにイメージをダウンロードしてイメージビューで表示
Serverコード:
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;
}
}
、これは、Android側(アンドロイドスタジオ)である:あなたがGlideを使用していないのはなぜ
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());
}
}
}
にコードのこの部分を追加することを忘れないでください。あなたのログ! –
https://github.com/square/picassoを使用してください。ピカソは使いやすいです。 – MASh
エンコードされた画像を返すポイントが表示されません。なぜあなたはAndroidの部分でURLを返すだけで、あなたはそれを読み込むことができますか? –