簡體   English   中英

在android中從畫廊和相機捕獲圖像

[英]Capturing image from gallery & camera in android

首先我知道這是重復的問題,但我在從畫廊或相機捕獲圖像時沒有問題。 我創建了一個虛擬項目來檢查我的代碼在這里它工作正常但是當我在我的項目中使用相同的代碼時它不起作用即使我在開始活動后沒有收到任何錯誤結果它被取消但我仍然可以查看圖庫中的圖像,我可以從相機捕獲圖像。

當我檢查 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;
        }
}

謝謝

看看LinderdaumEngineActivity.java從我的項目Linderdaum引擎

從相機捕獲圖像:

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,確保您擁有所有正確的權限。

請參閱上述 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>
...

官方文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM