图像未保存在个人资料图片中(Image is not saving in profile picture)

编程入门 行业动态 更新时间:2024-10-26 14:33:49
图像未保存在个人资料图片中(Image is not saving in profile picture)

这是使用的链接...... http://javatechig.com/android/writing-image-picker-using-intent-in-android问题是图像没有保存。当我选择图片时它会进来ImageView.but当我从这个活动以及应用程序出来时,它没有保存...请帮助我。

任何帮助,将不胜感激。

pickImage.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { Intent photoPickerIntent = new Intent(Intent.ACTION_PICK); photoPickerIntent.setType("image/*"); startActivityForResult(photoPickerIntent, SELECT_PHOTO); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { super.onActivityResult(requestCode, resultCode, imageReturnedIntent); switch(requestCode) { case SELECT_PHOTO: if(resultCode == RESULT_OK){ try { final Uri imageUri = imageReturnedIntent.getData(); final InputStream imageStream = getContentResolver().openInputStream(imageUri); final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream); imageView.setImageBitmap(selectedImage); } catch (FileNotFoundException e) { e.printStackTrace(); } } } }

This is the link which is used...http://javatechig.com/android/writing-image-picker-using-intent-in-android Problem is that images is not saving .When i am selecting picture it is coming in ImageView.but when I came out from this activity as well as from app ,it is not saving...Please help me.

Any help would be appreciated.

pickImage.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { Intent photoPickerIntent = new Intent(Intent.ACTION_PICK); photoPickerIntent.setType("image/*"); startActivityForResult(photoPickerIntent, SELECT_PHOTO); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { super.onActivityResult(requestCode, resultCode, imageReturnedIntent); switch(requestCode) { case SELECT_PHOTO: if(resultCode == RESULT_OK){ try { final Uri imageUri = imageReturnedIntent.getData(); final InputStream imageStream = getContentResolver().openInputStream(imageUri); final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream); imageView.setImageBitmap(selectedImage); } catch (FileNotFoundException e) { e.printStackTrace(); } } } }

最满意答案

你可以这样做:

制作全局变量

SharedPreferences sp;

在获取imageview参考后的onCreate()中

sp=getSharedPreferences("profilePicture",MODE_PRIVATE); if(!sp.getString("dp","").equals("")){ byte[] decodedString = Base64.decode(sp.getString("dp", ""), Base64.DEFAULT); Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); imageView.setImageBitmap(decodedByte); }

在onActivityResult方法中,在imageView中设置图像后,编写以下代码:

ByteArrayOutputStream baos = new ByteArrayOutputStream(); selectedImage.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object byte[] b = baos.toByteArray(); String encodedImage = Base64.encodeToString(b, Base64.DEFAULT); sp.edit().putString("dp", encodedImage).commit();

这段代码应该有效。 请尝试。 如果它不起作用,我可以给你完整的活动代码。

You can do like this:

Make global variable

SharedPreferences sp;

In onCreate() after getting reference of your imageview

sp=getSharedPreferences("profilePicture",MODE_PRIVATE); if(!sp.getString("dp","").equals("")){ byte[] decodedString = Base64.decode(sp.getString("dp", ""), Base64.DEFAULT); Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); imageView.setImageBitmap(decodedByte); }

In onActivityResult method, after setting the image in imageView, write this code:

ByteArrayOutputStream baos = new ByteArrayOutputStream(); selectedImage.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object byte[] b = baos.toByteArray(); String encodedImage = Base64.encodeToString(b, Base64.DEFAULT); sp.edit().putString("dp", encodedImage).commit();

This code should work. Please try. If it does not work, i can give you full activity code.

更多推荐

本文发布于:2023-07-19 20:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1186764.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像   个人资料   图片   Image   profile

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!