[英]Set profile picture and save in internal memory folder-android app
我是Android新手,嘗試設置個人資料圖片並將帶有用戶名的圖片保存在某個文件夾中,只要有人登錄個人資料,他/她就可以查看其中的個人資料圖片。 我是android的初學者。 任何建議,請問我該怎么做。 我已經嘗試到現在為止:
碼
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??
}
}
}
嘗試使用此功能保存圖像
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();
}
}
從圖庫中選擇圖像后
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??
}
}
將此添加到您的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());
}
保存后獲取圖像
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);
在清單文件中添加以下權限。
<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.