[英]Save string as an image on device internal storage
I'm developing an Android app that connects to facebook and save user's profile image on device internal storage. 我正在开发一个Android应用程序,该应用程序可以连接到Facebook,并将用户的个人资料图片保存在设备内部存储中。
This is my code: 这是我的代码:
private void getFacebookUserProfilePicture(String userAccessToken)
{
String url = Constants.FB_PROFILE_IMAGE_URL + userAccessToken;
AsyncHttpClient client = new AsyncHttpClient();
client.get(url, new AsyncHttpResponseHandler()
{
@Override
public void onSuccess(String response)
{
saveUserProfileImage(response.getBytes());
}
});
}
private void saveUserProfileImage(byte[] imageBytes)
{
Log.v(TAG, "Save user image");
FileOutputStream fOut = null;
try
{
fOut = openFileOutput(Constants.FB_PROFILE_IMAGE_FILE_NAME, Context.MODE_PRIVATE);
fOut.write(imageBytes);
fOut.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
I'm testing using Android emulator and I have used Eclipse DDMS to save the file to my PC, but I can't open it. 我正在使用Android模拟器进行测试,并且已经使用Eclipse DDMS将文件保存到PC上,但是无法打开它。
What am I doing wrong? 我究竟做错了什么? I have saved with a jpg extension. 我保存了jpg扩展名。
Or maybe, the question could be: How can I save a byte[]
as a JPG image? 也许问题可能是: 如何将byte[]
保存为JPG图像?
If I test the url
on the browser, I get an image with 2,47KB . 如果我在浏览器上测试该url
, 则会得到一个2,47KB的图像。 And the image I have copied from the device is 4,26KB . 我从设备复制的图像为4,26KB 。
You can convert string into byte array and then write this array to your filename. 您可以将字符串转换为字节数组,然后将此数组写入您的文件名。 You this method to write array: 你用这种方法写数组:
public void WriteByteToFile(byte[] mybytes, String filename){
try {
FileOutputStream FOS = openFileOutput(filename, MODE_PRIVATE);
FOS.write(mybytes);
FOS.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
Convert string into byte array like below: 将字符串转换为字节数组,如下所示:
byte[] bytes = string.getBytes("UTF-8");
Try it hopr this solve your problem.. 尝试一下,这可以解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.