簡體   English   中英

Android應用程序-帶有相機應用程序的彈出菜單

[英]Android App- Popup Menu with the Camera App

我一直試圖讓我的第一個Android應用程序正常工作。 我有一個使用webview的活動,我用它來打開上面有html表單的網頁。

使“選擇文件”按鈕(用於文件輸入)無法正常工作,但是由於在WebView中的File Upload中發布的幫助,我終於解決了它。 從那里開始,我幾乎使用了他們在Github上可用的Main Activity java代碼

我的實際問題是,單擊文件輸入按鈕時,我沒有用戶想要使用設備的Camera的選項 起初,我認為這可能與必須為該應用程序請求“相機”權限有關,但是我實現了它,但在那個應用程序上我錯了。 這里的問題是我對獲取彈出菜單的意圖缺乏經驗,例如:

 Intent i = new Intent(Intent.ACTION_GET_CONTENT);
 i.addCategory(Intent.CATEGORY_OPENABLE);
 i.setType("image/*");

我們非常感謝您提供一些有關找到“相機”選項的方法的指導。

讓我向您展示我的意思,在Chrome上打開相同的html表單,並在2個不同的Android OS版本(4.4.4和6.0)上打開我的應用。 使用運行Android 4.4.4的 Samsung Galaxy Tab 當打開具有html表單的頁面時,在Google Chrome上 ,單擊“ 選擇文件”按鈕, 我會看到此菜單

那就是我想要在我的應用程序中擁有的

使用相同的URL並將其顯示在我的App中(在4.4.4上) ,使用Webview,單擊“ 選擇文件”按鈕時,出現此菜單

(此外,我嘗試在Android 6.0模擬器上的應用程序上單擊“選擇文件”按鈕, 它直接進入Gallery ,並且那里沒有Camera選項):

這是代碼的相關部分:

        //For Android 4.1+
        public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture){
            mUM = uploadMsg;
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            i.addCategory(Intent.CATEGORY_OPENABLE);
            i.setType("image/*");
            MainActivity.this.startActivityForResult(Intent.createChooser(i, "Choose an Image please"), MainActivity.FCR);
        }
        //For Android 5.0+
        public boolean onShowFileChooser(
                WebView webView, ValueCallback<Uri[]> filePathCallback,
                FileChooserParams fileChooserParams){
            if(mUMA != null){
                mUMA.onReceiveValue(null);
            }
            mUMA = filePathCallback;
            Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            if(takePictureIntent.resolveActivity(MainActivity.this.getPackageManager()) != null){
                File photoFile = null;
                try{
                    photoFile = createImageFile();
                    takePictureIntent.putExtra("PhotoPath", mCM);
                }catch(IOException ex){
                    Log.e(TAG, "Image file creation failed", ex);
                }
                if(photoFile != null){
                    mCM = "file:" + photoFile.getAbsolutePath();
                    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
                }else{
                    takePictureIntent = null;
                }
            }
            Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
            contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
            contentSelectionIntent.setType("image/*");
            Intent[] intentArray;
            if(takePictureIntent != null){
                intentArray = new Intent[]{takePictureIntent};
            }else{
                intentArray = new Intent[0];
            }

            Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
            chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
            chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser");
            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
            startActivityForResult(chooserIntent, FCR);
            return true;
        }

這是一個輔助功能,可同時提供“相機”和“媒體選擇器”選擇器意圖。 我認為您是在特別詢問ACTION_IMAGE_CAPTURE,即此示例代碼片段的第一部分。

private Intent getPhotoChooserIntent(String acceptType, String capture)
{
 try
 {
    //---------------------------------------------------------------------
    // camera Intent 
    //---------------------------------------------------------------------
    Intent intentCamera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    Calendar cal = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("HHmmss", Locale.US);

    // path to picture
    File dirPhotos = mContext.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File photo = new File(String.format("%s/Photo_%s.jpg", dirPhotos.getAbsolutePath(), sdf.format(cal.getTime())));
    mPhoto = Uri.fromFile(photo);
    intentCamera.putExtra (MediaStore.EXTRA_OUTPUT, mPhoto);

    // pass "camera" in this parameter for a Camera only picker 
    if (capture.equalsIgnoreCase("camera"))
    {
     if (intentCamera.resolveActivity(mContext.getPackageManager()) != null)
        return (intentCamera);
     return (null);
    }

    //---------------------------------------------------------------------
    // media picker Intent
    //---------------------------------------------------------------------
    Intent intentPicker = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

    // aggregate list of resolved intents
    List<Intent> intentsList = new ArrayList<>();

    List<ResolveInfo> resInfo = mContext.getPackageManager().queryIntentActivities(intentPicker, 0);
    for (ResolveInfo resolveInfo : resInfo)
    {
     Intent intentTarget = new Intent(intentPicker);
     intentTarget.setPackage(resolveInfo.activityInfo.packageName);
     intentsList.add(intentTarget);
    }   

    if (intentCamera.resolveActivity(mContext.getPackageManager()) != null)
     intentsList.add(intentCamera);

    if (intentsList.size() > 0)
    {
     Intent intentChooser = Intent.createChooser(intentsList.remove(intentsList.size() - 1), mContext.getResources().getString(R.string.mediapicker));
     intentChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentsList.toArray(new Parcelable[]{}));
     return (intentChooser);
    }
 }
 catch (Exception e)
 {
    e.printStackTrace();
 }
 return (null);
}

假設mContext =活動上下文。 mPhoto是類型為Uri的類變量,用於訪問onActivityResult處理程序中從相機獲取的圖片。

希望這可以幫助!

看看Mario Velasco的 回答是否對您有幫助。

好的,因此我已經找出了部分問題。

我已經在清單中聲明了所需的權限,例如:

<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

但是,我犯了一個開始的錯誤,因為沒有明確要求訪問設備的相機和存儲,如:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 1);
        }
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
        }
    }

適用於Android 5.0+的部分可以正常工作(代碼可以開始使用),並且有一個菜單,您可以在其中選擇相機或畫廊中的照片。

無論哪種方式,我都已經測試了用戶CSmith對於5.0+的建議,並確認它也可以使用,因此,如果有人遇到這種情況,可以嘗試一下。

對於5之前的Android版本,我設法單擊“選擇文件”(但沒有圖庫選項)按鈕,打開相機應用程序並進行了一些更改,如下所示:

public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture){
            mUM = uploadMsg;
            Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(i, 1);
        }

現在,我將這個“修復”用於3和4。

暫無
暫無

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

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