第一种:
在进行第三方登录获取用户头像以及用户名之后,我们进行跳转并且,进行传值,由于第三方登陆的当中,我们的得到的头像的类型是bitmap类型的 就像这样 Bitmap bitmap = getbitmap(json.getString(“figureurl_qq_2”));最后我们将得到图片类型展示到页面上去* info_img.setImageBitmap(bitmap);
如果我们需要进行页面跳转并且传递我们获取到的头像信息的话通过以下方式,代码来代替口述:
**跳转页面之前做的工作**
Intent intent = new Intent(ShowActivity.this,TouxiangActivity.class);
Bundle bundle = new Bundle();
intent.putExtra("text",info_text.getText().toString().trim());
bundle.putParcelable("bitmao",bitmap);
*这里的bitmap就是刚才我们得到的那个图片类型*
intent.putExtras(bundle);
startActivity(intent);
**跳转之后做的工作**
final Intent intent = getIntent();
String text = intent.getStringExtra("text");
Bundle bundle = intent.getExtras();
*利用Bitmap进行接受*
Bitmap bitmap = bundle.getParcelable("bitmao");
textView.setText(text);
imageview.setImageBitmap(bitmap);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
第二种功能:
在选择头像时候有两种方式
1:从相册里面进行获取
//跨进程通信
private void openPic() {
//隐式意图激活
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
startActivityForResult(intent, 200);
}
//获取回传数据
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//进行获取相册的图片进行添加头像
if (requestCode == 200 && resultCode == RESULT_OK ) {
getPicData(data);
}
if(requestCode==IMAGE_REQUEST){
Bitmap bitmap = data.getParcelableExtra("data");
imageview.setImageBitmap(bitmap);
}
/* //点击进行拍照获取头像的数据
if (requestCode==IMAGE_REQUEST) {
Bitmap bitmap = data.getParcelableExtra("data");
imageview.setImageBitmap(bitmap);
}*/
}
//获取图片
private void getPicData(Intent data) {
if (data != null) {
//Bitmap bitmap =data.getParcelableExtra("data");
Uri uri = data.getData();
Bitmap bitmap = getBitmap(uri);
imageview.setImageBitmap(bitmap);
file = getFile(bitmap);
}
}
//把bitmap转换成file
private File getFile(Bitmap bitmap) {
File file = new File(Environment.getExternalStorageDirectory(), "a.jpg");
try {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
bos.flush();
bos.close();
return file;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//把uri转换成Bitmap
private Bitmap getBitmap(Uri uri) {
try {
//把uri转换成Bitmap
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
return bitmap;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
2:利用拍照变头像
//跨进程通信
private void openPic() {
//隐式意图激活
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
startActivityForResult(intent, 200);
}
//获取回传数据
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//进行获取相册的图片进行添加头像
if (requestCode == 200 && resultCode == RESULT_OK ) {
getPicData(data);
}
if(requestCode==IMAGE_REQUEST){
Bitmap bitmap = data.getParcelableExtra("data");
imageview.setImageBitmap(bitmap);
}
/* //点击进行拍照获取头像的数据
if (requestCode==IMAGE_REQUEST) {
Bitmap bitmap = data.getParcelableExtra("data");
imageview.setImageBitmap(bitmap);
}*/
}
//获取图片
private void getPicData(Intent data) {
if (data != null) {
//Bitmap bitmap =data.getParcelableExtra("data");
Uri uri = data.getData();
Bitmap bitmap = getBitmap(uri);
imageview.setImageBitmap(bitmap);
file = getFile(bitmap);
}
}
//把bitmap转换成file
private File getFile(Bitmap bitmap) {
File file = new File(Environment.getExternalStorageDirectory(), "a.jpg");
try {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
bos.flush();
bos.close();
return file;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//把uri转换成Bitmap
private Bitmap getBitmap(Uri uri) {
try {
//把uri转换成Bitmap
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
return bitmap;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
第三个功能:
将我们得到的图片信息保存到sd里面,
简单代码实现,倒是这里我出现一个小问题,在我将图片保存到文件家里面的时候,图片的大小为0B,就是有文件但是没内容,哪位大神知道,麻烦告诉我一下:
public class MainActivity extends AppCompatActivity {
private String path = "http://image.wufazhuce.com/FuPgOcbGDD9__fyuCdPBXb5pbA1C";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//进行网络下载图片,并且保存到sd卡里面去
loadFirstImage(path);
}
private void loadFirstImage(final String url) {
/**
* 在子线程中开启下载图片的任务
*/
new Thread(new Runnable() {
@Override
public void run() {
URL imageUrl;
try {
if (!TextUtils.isEmpty(url)) {
imageUrl = new URL(url);
InputStream is = imageUrl.openStream();
Bitmap bitmap = BitmapFactory.decodeStream(is);
saveFile(bitmap, System.currentTimeMillis() + ".jpg"); //将当前的时间作为图片的名字保存起来
Log.i("xxx",System.currentTimeMillis()+"");
is.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}).start();
}
private void saveFile(Bitmap bitmap, String fileName) {
try {
String filePath = "";
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) { //判断SD卡是否挂载
filePath = Environment.getExternalStorageDirectory() + "/renruiJob/";
} else {
return;
}
File dirFile = new File(filePath);
if (!dirFile.exists()) {
dirFile.mkdir();
}
File myPictureFile = new File(filePath + fileName);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myPictureFile));
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos);
bos.flush();
bos.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
-
顶
- 1
-
踩
- 0