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

时间:2015-12-22 11:15:06

标签: java android

我是Android的新手并尝试设置个人资料图片并使用用户名保存在某个文件夹中,每当有人登录个人资料时,他/她就可以查看他们的个人资料照片。我是android的初学者。有任何建议,请问我该怎么做。我到现在为止已经尝试过了:

代码

<?
extract($_POST);
// This will count total images selected.
$TotalImage = count($_FILES['userfile']['name']);

for($i = 0;$i<$TotalImage;$i++)
{ 
  $image_name = $_FILES['userfile']['name'][$i];
    $target_path = "uploads/".$imagename;

  if(move_uploaded_file($_FILES['userfile']['tmp_name'][$i], $target_path))
  {
    //Write Query Here To Insert Into Database
  }
}
?>

2 个答案:

答案 0 :(得分:2)

尝试使用此功能保存图像

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

答案 1 :(得分:1)

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