【问题标题】:android studio taking photos安卓工作室拍照
【发布时间】:2023-04-08 10:01:02
【问题描述】:

嘿,我按照教程在 android studios 项目中拍照,但我的应用程序崩溃而没有任何日志错误。任何帮助,将不胜感激。如果有人知道如何将图库中的图片添加到 imageview 中,那就太好了。

public class MainActivity extends Activity {
    Button bn;
    int REQUEST_CODE = 1;
    ImageView IMG;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bn = (Button) findViewById(R.id.bn);
        IMG = (ImageView) findViewById(R.id.img);
        bn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                if(i.resolveActivity(getPackageManager())!= null){
                    startActivityForResult(i,REQUEST_CODE);
                }
            }
        });
        }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    public void onActivityResult(int requestcode, int resultcode, Intent data){
        if(requestcode == REQUEST_CODE && resultcode == RESULT_OK)
        {
            Bundle extras = data.getExtras();
            Bitmap imageBitmap = (Bitmap) extras.get("data");
            IMG.setImageBitmap(imageBitmap);
        }
    }

} 

【问题讨论】:

    标签: android android-camera android-camera-intent


    【解决方案1】:

    您好,我想您要从图库或相机中拍照,然后您想将该图像设置为 ImageView。如果我是对的,请按照以下代码进行操作

    private void openCamera() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_HH_mm_ss");
    String currentTimeStamp = dateFormat.format(new Date());
    f = new File(android.os.Environment.getExternalStorageDirectory(), currentTimeStamp);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
    startActivityForResult(intent, 1);
    

    }

    private void openGallery() {
    Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    intent.setType("image/*");
    startActivityForResult(Intent.createChooser(intent, "Select File"), REQUEST_GALLERY);
    

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        if (requestCode == 1) {
            try {
                String click = f.getAbsolutePath();
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                ContentValues values = new ContentValues();
                values.put(User_Detail.KEY_ALBUM_IMAGE_PATH, click);
                values.put(User_Detail.USER_ID, Utlity.user_id);
                values.put(User_Detail.KEY_ID_ALBUM_REF, album_id);
                long loginStat = db.insert(User_Detail.TABLE_4, null, values);
                loadData(album_id);
    
            } catch (Exception e) {
                e.printStackTrace();
            }
     } else if (requestCode == 2) {
    
            Uri selectedImage = data.getData();
            String[] filePath = { MediaStore.Images.Media.DATA };
            Cursor c = getContentResolver().query(selectedImage, filePath, null, null, null);
            c.moveToFirst();
            int columnIndex = c.getColumnIndex(filePath[0]);
            picturePath = c.getString(columnIndex);
            if (picturePath != null && picturePath.length() > 1) {
                ContentValues va = new ContentValues();
            } else {
            }
            SQLiteDatabase db = dbHelper.getWritableDatabase();
            String image_path = picturePath;
            ContentValues values = new ContentValues();
            values.put(User_Detail.KEY_ALBUM_IMAGE_PATH, image_path);
            values.put(User_Detail.USER_ID, Utlity.user_id);
            values.put(User_Detail.KEY_ID_ALBUM_REF, album_id);
            long loginStat = db.insert(User_Detail.TABLE_4, null, values);
            loadData(album_id);
            c.close();
            Log.w("path of image from gallery......******************.........", picturePath + "");
        }
    }
    

    }

    现在您将获得所选图像或捕获图像的 ImagePath。现在您需要在 Image View 中设置然后创建一个扩展 BaseAdapter 的适配器类,然后在该适配器类中您可以通过将 Imagepath 转换为 Bitmap 来设置此图像

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-08
      • 2017-04-17
      • 2014-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多