使用相机意图获取拍摄图像的路径机器人 [英] Getting path of captured image in android using camera intent

查看:148
本文介绍了使用相机意图获取拍摄图像的路径机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图让以删除图像拍摄图像的路径,发现了许多答案的计算器,但他们都不是为我工作,我得到了以下的答案

 私人字符串getLastImagePath(){
        最终的String [] imageColumns = {MediaStore.Images.Media._ID,
                MediaStore.Images.Media.DATA};
        最后弦乐imageOrderBy = MediaStore.Images.Media._ID +DESC;
        光标imageCursor = POS.this.getContentResolver()查询(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,imageColumns,
                NULL,NULL,imageOrderBy);
        如果(imageCursor.moveToFirst()){
            // INT ID = imageCursor.getInt(imageCursor
            // .getColumnIndex(MediaStore.Images.Media._ID));
            字符串FULLPATH = imageCursor.getString(imageCursor
                    .getColumnIndex(MediaStore.Images.Media.DATA));
            返回完整路径;
        } 其他 {
            返回 ;
        }
    }
 

但这code工作在三星的标签,但在联想的标签和i-球标签不起作用。 因此,谁能帮助我找到其他的解决办法,以做? 任何帮助将AP preciated。谢谢你。

这是我的onActivityResult

 如果(要求code == CmsInter.CAMERA_REQUEST和放大器;&安培;结果code == RESULT_OK){
            //位图的照片= NULL;
            //照片=(位图)data.getExtras()获得(数据);

            字符串的txt =;
            如果(我!= NULL){
                字符串结果=;
                //im.setImageBitmap(photo);
                im.setTag(2);
                INT指数= im.getId();
                字符串路径= getLastImagePath();
                尝试 {
                    bitmap1 = BitmapFactory.de codeFILE(路径选择);
                    位图=位图
                            .createScaledBitmap(bitmap1,512,400,FALSE);
                    ByteArrayOutputStream BAOS =新ByteArrayOutputStream();
                    bitmap.com preSS(Bitmap.Com pressFormat.PNG,100,BAOS);
                    byte []的bytData = baos.toByteArray();
                    尝试 {
                        baos.close();
                    }赶上(IOException异常E){
                        e.printStackTrace();
                    }
                    结果= Base64.en code(bytData);
                    bytData = NULL;
                }赶上(OutOfMemoryError错误OOM){
                    的System.out.println(内存不足异常----->中+ OOM);
                    bitmap1.recycle();
                    bitmap.recycle();

                } 最后 {
                    bitmap1.recycle();
                    bitmap.recycle();
                }
}
 

解决方案

试试这样

像下面

通行证相机意向

 意向意图=新的意图(本);
startActivityForResult(意向,REQ_CAMERA_IMAGE);
 

和捕捉图像后写的 OnActivityResult 如下

 保护无效onActivityResult(INT申请code,INT结果code,意图数据){
    如果(要求code == CAMERA_REQUEST和放大器;&安培;结果code == RESULT_OK){
        。位图照片=(位图)data.getExtras()获得(数据);
        imageView.setImageBitmap(照片);
        knop.setVisibility(Button.VISIBLE);


        //调用这个方法来得到URI从位图
        乌里tempUri = getImageUri(getApplicationContext(),照片);

        //调用此方法来获得实际的路径
        文件finalFile =新的文件(getRealPathFromURI(tempUri));

        的System.out.println(mImageCaptureUri);
    }
}

公众开放的getImageUri(上下文inContext的,位图inImage){
    ByteArrayOutputStream字节=新ByteArrayOutputStream();
    inImage.com preSS(Bitmap.Com pressFormat.JPEG,100个字节);
    字符串路径= Images.Media.insertImage(inContext.getContentResolver(),inImage,标题,NULL);
    返回Uri.parse(路径);
}

公共字符串getRealPathFromURI(URI URI){
    光标光标= getContentResolver()查询(URI,NULL,NULL,NULL,NULL);
    cursor.moveToFirst();
    INT IDX = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
    返回cursor.getString(IDX);
}
 

和检查日志

I have been trying to get path of captured image in order to delete image , found many answer on stackoverflow but none of them is working for me , i got the following answer

private String getLastImagePath() {
        final String[] imageColumns = { MediaStore.Images.Media._ID,
                MediaStore.Images.Media.DATA };
        final String imageOrderBy = MediaStore.Images.Media._ID + " DESC";
        Cursor imageCursor = POS.this.getContentResolver().query(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, imageColumns,
                null, null, imageOrderBy);
        if (imageCursor.moveToFirst()) {
            // int id = imageCursor.getInt(imageCursor
            // .getColumnIndex(MediaStore.Images.Media._ID));
            String fullPath = imageCursor.getString(imageCursor
                    .getColumnIndex(MediaStore.Images.Media.DATA));
            return fullPath;
        } else {
            return "";
        }
    }

but this code works in samsung tab but doesn't work in lenovo tab and i-ball tab. so, Can anyone help me in finding other solution to do the same ? Any help will be appreciated. Thank you.

This is my onActivityResult

if (requestCode == CmsInter.CAMERA_REQUEST && resultCode == RESULT_OK) {
            //Bitmap photo = null;
            //photo = (Bitmap) data.getExtras().get("data");

            String txt = "";
            if (im != null) {
                String result = "";
                //im.setImageBitmap(photo);
                im.setTag("2");
                int index = im.getId();
                String path = getLastImagePath();
                try {
                    bitmap1 = BitmapFactory.decodeFile(path, options);
                    bitmap = Bitmap
                            .createScaledBitmap(bitmap1, 512, 400, false);
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
                    byte[] bytData = baos.toByteArray();
                    try {
                        baos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    result = Base64.encode(bytData);
                    bytData = null;
                } catch (OutOfMemoryError ooM) {
                    System.out.println("OutOfMemory Exception----->" + ooM);
                    bitmap1.recycle();
                    bitmap.recycle();

                } finally {
                    bitmap1.recycle();
                    bitmap.recycle();
                }
}

解决方案

Try like this

Pass Camera Intent like below

Intent intent = new Intent(this);
startActivityForResult(intent, REQ_CAMERA_IMAGE);

And after capturing image Write an OnActivityResult as below

protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
        Bitmap photo = (Bitmap) data.getExtras().get("data"); 
        imageView.setImageBitmap(photo);
        knop.setVisibility(Button.VISIBLE);


        // CALL THIS METHOD TO GET THE URI FROM THE BITMAP
        Uri tempUri = getImageUri(getApplicationContext(), photo);

        // CALL THIS METHOD TO GET THE ACTUAL PATH
        File finalFile = new File(getRealPathFromURI(tempUri));

        System.out.println(mImageCaptureUri);
    }  
}

public Uri getImageUri(Context inContext, Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
    return Uri.parse(path);
}

public String getRealPathFromURI(Uri uri) {
    Cursor cursor = getContentResolver().query(uri, null, null, null, null); 
    cursor.moveToFirst(); 
    int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); 
    return cursor.getString(idx); 
}

And check log

这篇关于使用相机意图获取拍摄图像的路径机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆