简体   繁体   English

设置个人资料图片并保存在内部存储器文件夹中-Android应用

[英]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. 我是Android新手,尝试设置个人资料图片并将带有用户名的图片保存在某个文件夹中,只要有人登录个人资料,他/她就可以查看其中的个人资料图片。 I am a beginner to android. 我是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. 将此添加到您的onActivityResult方法中。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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