android怎么从服务器上获取带中文名的图片
android怎样从服务器上获取带中文名的图片
------解决方案--------------------
设计个算法,下载英文的,转化为中文的
------解决方案--------------------
URL是有规则的啊,中文以及特殊符号是需要转换的。
------解决方案--------------------
虽然帮不上楼主,但还是顶一下。。。。
//mainactivity 很简单的根据一个连接显示一张图片,但如果图片名是中文的话就无法显示,请问这个需要怎么处理才能让它显示
public class GetAPictureFromInternetActivity extends Activity {
private EditText pathText;
private ImageView imageView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
pathText = (EditText) this.findViewById(R.id.path);
imageView = (ImageView) this.findViewById(R.id.imageView);
}
public void showimage(View v){
String path = pathText.getText().toString();
try {
Bitmap bitmap = ImageService.getImage(path);
imageView.setImageBitmap(bitmap);
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), R.string.error, 1).show();
}
}
}
//获取图片
public class ImageService {
/**
* 获取图片
* @param path 图片路径
* @return
*/
public static Bitmap getImage(String path) throws Exception{
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
if(conn.getResponseCode() == 200){
InputStream inStream = conn.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(inStream);
return bitmap;
/*byte[] data = StreamTool.read(inStream);
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
return bitmap;*/
}
return null;
}
}
public class StreamTool {
/**
* 从流中读取数据
* @param inStream
* @return
*/
public static byte[] read(InputStream inStream) throws Exception{
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while( (len = inStream.read(buffer)) != -1){
outputStream.write(buffer, 0, len);
}
inStream.close();
return outputStream.toByteArray();
}
}
Android
Bitmap
图片
------解决方案--------------------
设计个算法,下载英文的,转化为中文的
------解决方案--------------------
URL是有规则的啊,中文以及特殊符号是需要转换的。
------解决方案--------------------
虽然帮不上楼主,但还是顶一下。。。。