0

I am new to android and trying to set a profile picture and save the picture with the username in some folder and whenever a person logins profile he/she can view the profile pic of them. I am a beginner to android. Any suggestions please how can i do this. I have tried up till now is here :

Code

public class homeprofile extends AppCompatActivity implements View.OnClickListener{
    public static int i = 1;
    ImageView coverpic;
    Button Buploadcover;
    String pathToImage;
    String path;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);
        coverpic = (ImageView) findViewById(R.id.coverpic);
        Buploadcover = (Button) findViewById(R.id.Buploadcover);
        coverpic.setOnClickListener(this);
        Buploadcover.setOnClickListener(this);
      }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.coverpic:
                Intent galleryintent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(galleryintent, i);

                break;
               case R.id.Buploadcover:
                break;
               default:
                break;

        } }


  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == i && resultCode == RESULT_OK && data != null) {
            Uri selectedImage = data.getData();

            coverpic.setImageURI(selectedImage);
            pathToImage = selectedImage.getPath();
           //stuff to do on click button upload cover??
        }
    }

}
tabia
  • 631
  • 2
  • 10
  • 33

2 Answers2

2

Try this function for save image

 public static void saveImage(Bitmap bitmap) {
        OutputStream output;
        String recentImageInCache;
        File filepath = Environment.getExternalStorageDirectory();

        // Create a new folder in SD Card
        File dir = new File(filepath.getAbsolutePath()
                + "/YOUR_APP/profile");
        dir.mkdirs();

        // Create a name for the saved image
        File file = new File(dir, username+".jpg");
        try {

            output = new FileOutputStream(file);

            // Compress into png format image from 0% - 100%
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, output);
            output.flush();
            output.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

After select image from gallery

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == i && resultCode == RESULT_OK && data != null) {
            Uri selectedImage = data.getData();

            saveImage(yourbitmap);
            coverpic.setImageURI(selectedImage);
            pathToImage = selectedImage.getPath();
           //stuff to do on click button upload cover??
        }
    }
Vimal Gajera
  • 497
  • 3
  • 9
1

Add this in your onActivityResult Method.

Bundle extras = data.getExtras();
Bitmap profilePic = extras.getParcelable("data");

String path = Environment.getExternalStorageDirectory().toString();
File imgDirectory = new File(path + "/Profile Images/");
if (!imgDirectory.exists()) imgDirectory.mkdir();
OutputStream fOut = null;
File file = new File(path);

file = new File(path, "/Profile Images/" + UserName+"_"+System.currentTimeMillis()+ ".png"); 

try
{
  if (!file.exists()) file.createNewFile();
  fOut = new FileOutputStream(file);      
  Bitmap bitmap = profilePic.copy(Bitmap.Config.ARGB_8888, true);
  bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
  fOut.flush();
  fOut.close();
  MediaStore.Images.Media.insertImage(getContentResolver(),
        file.getAbsolutePath(), file.getName(), file.getName());
}
catch (Exception e)
{
  Log.e("Error","File Exception"+e.getMessage());
}

Get Image after save

File image_file = new File(url);

    if (image_file.exists()) {
        Bitmap bitmap = null;
        try {
            bitmap = BitmapFactory.decodeFile(image_file.getAbsolutePath());
        } catch (Exception e) {
           Log.e("Error","Bitmap Exception"+e.getMessage());
        }

   imageview.setImageBitmap(bitmap);

Add below permission in manifest file.

   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
   <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
  • Can you please tell me which url you have specified in `File(url)` argument? – tabia Dec 22 '15 at 15:55
  • It is your store file path . Sorry by mistake i set url , it is storage path like /storage/sdcard0//Profile Images/UserName_1425367484812.png . – Himani Mehta Dec 23 '15 at 05:28
  • @HimaniMehta pls tell how to get UserName which is used in storagefile path – frooty Apr 24 '20 at 07:31
  • @HimaniMehta also my directory path is coming /storage/emulated/0/ProfileImages/.... and code is not able to access this location. kindly sugest solution to get image from this path – frooty Apr 24 '20 at 09:12