从android中的画廊和相机中捕获图像

Posted

技术标签:

【中文标题】从android中的画廊和相机中捕获图像【英文标题】:Capturing image from gallery & camera in android 【发布时间】:2012-02-23 22:32:01 【问题描述】:

首先我知道这是一个重复的问题,但我从画廊或相机捕捉图像没有问题。我在虚拟项目上创建了在这里检查我的代码它工作正常 但是当我在我的项目中使用相同的代码时,即使我没有收到任何错误,它也无法正常工作 一旦我开始活动以获得结果,它就会被取消,但我仍然可以从图库中看到图像,并且可以从相机中捕获图像。

当我检查 logcat 时,我发现以下警告不知道为什么会出现以及如何解决这个问题

 W/NetworkConnectivityListener(2399): onReceived() called with CONNECTED and Intent  act=android.net.conn.CONNECTIVITY_CHANGE flg=0x10000000 (has extras) 

编辑:-- 添加代码

@Override
public boolean onOptionsItemSelected(MenuItem item) 
    switch (item.getItemId()) 
        case R.id.camera:
            //define the file-name to save photo taken by Camera activity
            String fileName = "new-photo-name.jpg";
            //create parameters for Intent with filename
            ContentValues values = new ContentValues();
            values.put(MediaStore.Images.Media.TITLE, fileName);
            values.put(MediaStore.Images.Media.DESCRIPTION,"Image capture by camera");
            //imageUri is the current activity attribute, define and save it for later usage (also in onSaveInstanceState)
            imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
            //create new Intent
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
            intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
            startActivityForResult(intent, PICK_Camera_IMAGE);
            return true;

        case R.id.gallery:
            try 
                Intent gintent = new Intent();
                gintent.setType("image/*");
                gintent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(
                        Intent.createChooser(gintent, "Select Picture"),
                        PICK_IMAGE);
             catch (Exception e) 
                Toast.makeText(getApplicationContext(),
                        e.getMessage(),
                        Toast.LENGTH_LONG).show();
                Log.e(e.getClass().getName(), e.getMessage(), e);
            
            return true;
    
    return false;


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    switch (requestCode) 
            case PICK_IMAGE:
                if (resultCode == Activity.RESULT_OK) 
                    Uri selectedImageUri = data.getData();
                    String filePath = null;

                    try 
                        // OI FILE Manager
                        String filemanagerstring = selectedImageUri.getPath();

                        // MEDIA GALLERY
                        String selectedImagePath = getPath(selectedImageUri);

                        if (selectedImagePath != null) 
                            filePath = selectedImagePath;
                         else if (filemanagerstring != null) 
                            filePath = filemanagerstring;
                         else 
                            Toast.makeText(getApplicationContext(), "Unknown path",
                                    Toast.LENGTH_LONG).show();
                            Log.e("Bitmap", "Unknown path");
                        

                        if (filePath != null) 
                            decodeFile(filePath);
                         else 
                            bitmap = null;
                        
                     catch (Exception e) 
                        Toast.makeText(getApplicationContext(), "Internal error",
                                Toast.LENGTH_LONG).show();
                        Log.e(e.getClass().getName(), e.getMessage(), e);
                    
                
                break;
            case PICK_Camera_IMAGE:
                 if (resultCode == RESULT_OK) 
                    //use imageUri here to access the image
                    Toast.makeText(this, "Picture was taken", Toast.LENGTH_SHORT).show();
                    Uri selectedImageUri = imageUri;
                    String filePath = null;

                    try 
                        // OI FILE Manager
                        String filemanagerstring = selectedImageUri.getPath();

                        // MEDIA GALLERY
                        String selectedImagePath = getPath(selectedImageUri);

                        if (selectedImagePath != null) 
                            filePath = selectedImagePath;
                         else if (filemanagerstring != null) 
                            filePath = filemanagerstring;
                         else 
                            Toast.makeText(getApplicationContext(), "Unknown path",
                                    Toast.LENGTH_LONG).show();
                            Log.e("Bitmap", "Unknown path");
                        

                        if (filePath != null) 
                            decodeFile(filePath);
                         else 
                            bitmap = null;
                        
                     catch (Exception e) 
                        Toast.makeText(getApplicationContext(), "Internal error",
                                Toast.LENGTH_LONG).show();
                        Log.e(e.getClass().getName(), e.getMessage(), e);
                    
                 else if (resultCode == RESULT_CANCELED) 
                    Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT).show();
                 else 
                    Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT).show();
                
                 break;
        

谢谢

【问题讨论】:

以下问题应该对你有帮助...***.com/questions/9106486/… 【参考方案1】:

看看我的项目LinderdaumEngineActivity.java中的Linderdaum Engine:

从相机捕捉图像:

public void CapturePhoto( String FileName )

    try
    
        File f = new File(FileName);

        if ( f.exists() && f.canWrite() ) f.delete();

        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,Uri.fromFile(f));
        intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);

        startActivityForResult(intent, CAPTURE_IMAGE_CALLBACK);
    
    catch ( ActivityNotFoundException e )
    
        Log.e( TAG, "No camera: " + e );
    
    catch ( Exception e )
    
        Log.e( TAG, "Cannot make photo: " + e );
    

从图库中打开图片:

public static void OpenImage()

    try
    
        Intent intent = new Intent( Intent.ACTION_GET_CONTENT );
        intent.setType( "image/*" );
        startActivityForResult( intent, SELECT_PICTURE_CALLBACK );
    
    catch ( ActivityNotFoundException e )
    
        Log.e( TAG, "No gallery: " + e );
    

另外,不要忘记为您的清单添加权限:

<uses-feature android:name="android.hardware.camera" android:required="false"/>
<uses-permission android:name="android.permission.CAMERA" android:required="false"/>

【讨论】:

你有 在你的 AndroidManifest.xml 中? 添加以上两行 & 它仍然是相同的结果 那你需要说明你是如何处理回调的。【参考方案2】:

请检查您的 AndroidManifest,确保您拥有所有正确的权限。

【讨论】:

【参考方案3】:

请参阅以上 android 7 的文件提供程序的官方文档

getUriForFile(Context, String, File) which returns a content:// URI. For 
more recent apps targeting Android 7.0 (API level 24) and higher, passing a 
file:// URI across a package boundary causes a FileUriExposedException. 
Therefore, we now present a more generic way of storing images using a 
FileProvider.



 <application>
 ...
 <provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="com.example.android.fileprovider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths"></meta-data>
 </provider>
...

Official doc

【讨论】:

以上是关于从android中的画廊和相机中捕获图像的主要内容,如果未能解决你的问题,请参考以下文章

Android:在捕获或从图库中挑选后裁剪图像

将相机或画廊中的图像保存在 phonegap 的画布中,并在旋转和裁剪后保存

如何在特定的imageView上上传特定的图像点击来自android中的画廊和相机

如何仅在 Swift 中访问从相机拍摄的图像,就像 iOS 中的画廊一样?

在 imageview 中使用时从相机或画廊拍摄的图片其方向发生变化,有时在 Android 中会垂直拉伸

ImageView 未显示在 android 中使用相机捕获的图像