简体   繁体   中英

Set profile picture and save in internal memory folder-android app

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??
        }
    }

}

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??
        }
    }

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"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM