简体   繁体   English

如何在Android设备上访问相机胶卷/照片?

[英]how can i access the camera roll/Photos on Android?

I am having difficulty finding a way to opening a users camera roll/photo gallery on a button click on Android. 我在通过Android按钮单击时无法找到打开用户相机胶卷/照片库的方法。 The solution I am looking for includes accessing the camera roll/photo galley, allowing the user to select an image, saving that selected image to a file, then lastly saving that file to a database such as parse.com. 我正在寻找的解决方案包括访问相机胶卷/照片厨房,允许用户选择图像,将所选图像保存到文件,然后最后将该文件保存到诸如parse.com的数据库。 Please help and thank you in advance! 请帮助并提前感谢您!

Please refer to the link below. 请参考下面的链接。 I would like for the implementation to be similar to the one found on IOS. 我希望实现类似于IOS上的实现。 https://drive.google.com/file/d/0B4jjwQsKhqZRcU1FX05xeThlS0U/view?usp=sharing https://drive.google.com/file/d/0B4jjwQsKhqZRcU1FX05xeThlS0U/view?usp=sharing

Refer this given code. 请参考此给定的代码。 Its perfect to solve your problem. 完美解决您的问题。 It is calling image gallery and also saving image and displaying it again. 它正在调用图像库,还保存图像并再次显示它。

 package br.liveo.ndrawer; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.graphics.Bitmap; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.provider.MediaStore; import android.support.v7.app.ActionBar; import android.support.v7.app.ActionBarActivity; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.TextView; import java.io.IOException; import br.liveo.navigationliveo.NavigationLiveo; public class Profile extends ActionBarActivity { private RelativeLayout editLayout; private SharedPreferences preferences; private boolean edit = false; private ImageView profilePic; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.profile); ActionBar actionBar = getSupportActionBar(); actionBar.setHomeButtonEnabled(true); actionBar.setDisplayHomeAsUpEnabled(true); editLayout = (RelativeLayout) findViewById(R.id.profile_edit); editLayout.setVisibility(View.INVISIBLE); preferences = getApplicationContext().getSharedPreferences("User", Context.MODE_PRIVATE); String userName = preferences.getString("userName", "No User"); String userEmail = preferences.getString("userEmail", getString(R.string.samplemail)); String userPhoto = preferences.getString("userPhoto", "NULL"); String userAbout = preferences.getString("userAbout", getResources().getString(R.string.about)); TextView name = (TextView) findViewById(R.id.userName); name.setText(userName); TextView email = (TextView) findViewById(R.id.mail); email.setText(userEmail); TextView about = (TextView) findViewById(R.id.about); about.setText(userAbout); profilePic = (ImageView) findViewById(R.id.userPhoto); if (userPhoto.equals("NULL")) { profilePic.setBackgroundResource(R.drawable.ic_no_user); } else { //Bitmap bitmap = Profile.decodeBase64(userPhoto); Bitmap bitmap; try { bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(userPhoto)); } catch (IOException e) { profilePic.setBackgroundResource(R.drawable.ic_no_user); return; } profilePic.setImageBitmap(bitmap); } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.profile_edit, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.profile_edit: if (edit) { saveProfile(); edit = false; finish(); } else { item.setIcon(R.drawable.ic_action_accept); edit = true; profileEdit(); } return true; default: finish(); return super.onOptionsItemSelected(item); } } private void saveProfile() { TextView name = (TextView) findViewById(R.id.edit_name); TextView mail = (TextView) findViewById(R.id.edit_mail); TextView about = (TextView) findViewById(R.id.edit_about); SharedPreferences.Editor editor = preferences.edit(); if (name.getText().toString().length() > 0) editor.putString("userName", name.getText().toString()); if (mail.getText().toString().length() > 0) editor.putString("userEmail", mail.getText().toString()); if (about.getText().toString().length() > 0) editor.putString("userAbout", about.getText().toString()); editor.apply(); } private void profileEdit() { RelativeLayout layout = (RelativeLayout) findViewById(R.id.profile_display); layout.setVisibility(View.INVISIBLE); editLayout.setVisibility(View.VISIBLE); } public void pickUser(View view) { Intent intent; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); intent.addCategory(Intent.CATEGORY_OPENABLE); } else { intent = new Intent(Intent.ACTION_GET_CONTENT); } intent.setType("image/*"); startActivityForResult(intent, 3645); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent resultData) { if (requestCode == 3645 && resultCode == NavigationLiveo.RESULT_OK && resultData != null) { Uri selectedImage = resultData.getData(); profilePic.setImageURI(selectedImage); SharedPreferences preferences = getSharedPreferences("User", Context.MODE_PRIVATE); SharedPreferences.Editor editor = preferences.edit(); editor.putString("userPhoto", selectedImage.toString()); editor.apply(); } } } 

暂无
暂无

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

相关问题 如何在Android手机上访问相机? - How do I access the camera on Android phones? 在Android Studio中获取相机胶卷 - Get camera roll in Android Studio 如何从SD卡,相机和内部存储中选择照片并通过android studio中的电子邮件发送? 另外,我如何通过电子邮件发送电话号码? - How to select photos from SD-card, camera & internal storage and send them via email in android studio? Also, how can i send phone number via email? 如何从我的 android 设备中获取所有照片 - How can I get all photos from my android device 如何正确地将图像下载到我的相机胶卷? - How do I properly download an image to my camera roll? 如何在不保存的情况下在 Android 工作室中以 bitmap 或 ByteBuffer 的形式访问流式摄像机视频?(Java) - How can I access the streaming camera video as bitmap or ByteBuffer in Android studio without saving it?(Java) Android:如何使用相同的相机意图捕获多张照片 - Android: How to capture multiple photos with the same camera intent OpenGL:如何旋转(滚动)相机? - OpenGL : How to rotate (roll) the camera? 如何在Android Studio中将从相机拍摄的照片添加到文档模板中 - How to add photos taken from camera to a document template in android studio 我知道如何打开相机的按钮,但是我不知道如何更改拍摄照片的位置,我该怎么做? - I know how to make a button open the camera, but I don't how to change the location of the photos that are taken, how can I do this?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM