这是使用的链接...... 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.
更多推荐
发布评论