android: попытка завершить входное событие, но приемник входного события уже настроен на открытие камеры - PullRequest
0 голосов
/ 28 октября 2018

Я сталкиваюсь с такого рода ошибкой при попытке открыть собственное приложение камеры устройства, нажав кнопку с помощью метода onClick в макете кнопки xml https://i.imgur.com/71SAuOrb.png

это следующий код этого приложения

    package com.shankaryadav.www.fireemoji;
    import android.Manifest;
    import android.content.Intent;
    import android.content.pm.PackageManager;
    import android.graphics.Bitmap;
    import android.net.Uri;
    import android.provider.MediaStore;
    import android.support.annotation.NonNull;
    import android.support.design.widget.FloatingActionButton;
    import android.support.v4.app.ActivityCompat;
    import android.support.v4.content.ContextCompat;
    import android.support.v4.content.FileProvider;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
   import android.widget.Button;
     import android.widget.ImageView;
    import android.widget.TextView;
     import android.widget.Toast;
   import java.io.File;
   import java.io.IOException;

 public class MainActivity extends AppCompatActivity {
private static final int REQUEST_STORAGE_PERMISSION = 1;
private static final int REQUEST_IMAGE_CAPTURE = 1;

private static final String FILE_PROVIDER_AUTHORITY = 
"com.example.android.fileprovider";
private static String mTempPhotoPath;

FloatingActionButton clearFab, saveFab, shareFab;
TextView mtextView;
ImageView imageView;
Button mButton;

private static  Bitmap mResultsBitmap;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    clearFab = (FloatingActionButton) findViewById(R.id.clearMe);
    saveFab = (FloatingActionButton) findViewById(R.id.saveMe);
    shareFab = (FloatingActionButton) findViewById(R.id.shareMe);

    mtextView = findViewById(R.id.textView);
    imageView = findViewById(R.id.imageView);
    mButton = findViewById(R.id.clickMeButton);
}






/**
 * OnClick method for "Emojify Me!" Button. Launches the camera app.
 */

public void checkPermission(View view) {
    // Check for the external storage permission
    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.WRITE_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED) {

        // If you do not have permission, request it
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                REQUEST_STORAGE_PERMISSION);
    } else {
        // Launch the camera if the permission exists
        launchCamera();
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                       @NonNull int[] grantResults) {
    // Called when you request permission to read and write to external storage
    switch (requestCode) {
        case REQUEST_STORAGE_PERMISSION: {
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // If you get permission, launch the camera
                launchCamera();
            } else {
                // If you do not get permission, show a Toast
                Toast.makeText(this, "Permission denied", Toast.LENGTH_SHORT).show();
            }
            break;
        }
    }
}

/**
 * Creates a temporary image file and captures a picture to store in it.
 */
private void launchCamera() {

    // Create the capture image intent
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    // Ensure that there's a camera activity to handle the intent
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        // Create the temporary File where the photo should go
        File photoFile = null;
        try {
            photoFile = BitmapUtils.createTempImageFile(this);
        } catch (IOException ex) {
            // Error occurred while creating the File
            ex.printStackTrace();
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {

            // Get the path of the temporary file
            mTempPhotoPath = photoFile.getAbsolutePath();

            // Get the content URI for the image file
            Uri photoURI = FileProvider.getUriForFile(this,
                    FILE_PROVIDER_AUTHORITY,
                    photoFile);

            // Add the URI so the camera can store the image
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);

            // Launch the camera activity
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    }
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // If the image capture activity was called and was successful
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        // Process the image and set it to the TextView
        processAndSetImage();
    } else {

        // Otherwise, delete the temporary image file
        BitmapUtils.deleteImageFiile(this, mTempPhotoPath);
    }
}

/**
 * Method for processing the captured image and setting it to the TextView.
 */
private void processAndSetImage() {

    // Toggle Visibility of the views
    mButton.setVisibility(View.GONE);
    mtextView.setVisibility(View.GONE);

    // Resample the saved image to fit the ImageView
    mResultsBitmap = BitmapUtils.resamplePic(this, mTempPhotoPath);




    // Set the new bitmap to the ImageView
    imageView.setImageBitmap(mResultsBitmap);
}


/**
 * OnClick method for the save button.
 */

public void saveMe(View view) {
    // Delete the temporary image file
    BitmapUtils.deleteImageFiile(this, mTempPhotoPath);

    // Save the image
    BitmapUtils.saveImage(this, mResultsBitmap);
}

/**
 * OnClick method for the share button, saves and shares the new bitmap.
 */

public void shareMe(View view) {
    // Delete the temporary image file
    BitmapUtils.deleteImageFiile(this, mTempPhotoPath);

    // Save the image
    BitmapUtils.saveImage(this, mResultsBitmap);

    // Share the image
    BitmapUtils.shareImage(this, mTempPhotoPath);
}

/**
 * OnClick for the clear button, resets the app to original state.
 */

public void clearMe(View view) {
    // Clear the image and toggle the view visibility
    imageView.setImageResource(0);
    mButton.setVisibility(View.VISIBLE);
    mtextView.setVisibility(View.VISIBLE);

    // Delete the temporary image file
    BitmapUtils.deleteImageFiile(this, mTempPhotoPath);
}
    }

XML-файл макета, соответствующий операции: -

         <RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/activity_main"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView"
    android:text="Fire Picture"
    android:layout_above="@+id/clickMeButton"
    android:layout_centerHorizontal="true"
    android:layout_margin="16dp"
    android:textSize="30sp"
    android:textAppearance="@style/TextAppearance.AppCompat.Display1"
    />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/clickMeButton"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="Click me"
    android:textSize="25sp"
    android:onClick="checkPermission"
    android:textAppearance="@style/TextAppearance.AppCompat.Display1"/>
    <!--android:onClick="checkPermissionn"-->


<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/imageView"
    android:scaleType="fitStart"
    android:layout_margin="16dp"
    android:visibility="gone"/>

    <android.support.design.widget.FloatingActionButton
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
    android:id="@+id/clearMe"
  android:onClick="clearMe"
  android:layout_alignParentEnd="true"
   android:layout_alignParentRight="true"
  android:layout_alignParentTop="true"
   android:src="@android:drawable/ic_menu_close_clear_cancel"
   android:visibility="visible"
  app:backgroundTint="@android:color/white"
    app:fabSize="mini"
  />

   <android.support.design.widget.FloatingActionButton
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
  android:id="@+id/saveMe"
   android:layout_alignParentBottom="true"
  android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
   android:layout_marginBottom="@dimen/fab_margins"
  android:layout_marginEnd="@dimen/fab_margins"
  android:layout_marginRight="@dimen/fab_margins"
 android:src="@android:drawable/ic_menu_save"
  android:visibility="visible"
 android:onClick="saveMe"
  android:clickable="true"
 android:focusable="true"/>

     <android.support.design.widget.FloatingActionButton
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:id="@+id/shareMe"
   android:layout_alignParentBottom="true"
   android:layout_alignParentLeft="true"
   android:layout_alignParentStart="true"
   android:layout_marginBottom="@dimen/fab_margins"
         android:layout_marginLeft="@dimen/fab_margins"
         android:layout_marginStart="@dimen/fab_margins"
         android:src="@android:drawable/ic_menu_share"
         android:visibility="visible"
         android:onClick="shareMe"
         android:clickable="true"
         android:focusable="true"/>


      </RelativeLayout>

Класс BitmapUtils: -

           package com.shankaryadav.www.fireemoji;

   import android.content.Context;
  import android.content.Intent;
   import android.graphics.Bitmap;
   import android.graphics.BitmapFactory;
   import android.net.Uri;
   import android.os.Environment;
   import android.support.v4.content.FileProvider;
   import android.util.DisplayMetrics;
   import android.view.WindowManager;
   import android.widget.Toast;

   import java.io.File;
   import java.io.FileOutputStream;
   import java.io.IOException;
   import java.io.OutputStream;
   import java.text.SimpleDateFormat;
   import java.util.Date;
   import java.util.Locale;

     class BitmapUtils {

private static final String FILE_PROVIDER_AUTHORITY = "com.example.android.fileprovider";

/*
* Resample the captured photo to fit the screen for better memory usage.
*
* param is context
* param is imagePath
* returns bitmap
* */

static Bitmap resamplePic(Context context, String imagePath){

    //get device screen size information
    DisplayMetrics metrics = new DisplayMetrics();
    WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    manager.getDefaultDisplay().getMetrics(metrics);

    int targetH = metrics.heightPixels;
    int targetW = metrics.widthPixels;

    // get the dimensions of the original bitmap
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(imagePath,bmOptions);

    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;

    //scaling down the image
    int scaleFactor = Math.min( photoW/targetW, photoH/targetH);

    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor;

    return BitmapFactory.decodeFile(imagePath);
 }

 /*
 * Creates the temporary image file in the cache directory
 *
 * @return the temporary image files
 * @throws IOException Thrown if there is an error creating the file
  */

 static File createTempImageFile(Context context) throws IOException {
     String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
             Locale.getDefault()).format(new Date());
     String imageFileName = "JPEG_" + timeStamp + "_";
     File storageDir = context.getExternalCacheDir();

     return File.createTempFile(
             imageFileName,
             ".jpg",

             storageDir
     );
 }

 /*
 * delete image file for a given path
 *
 * @param context  The application context.
 * @param imagePath the path of the photo to be deleted
 * **/

 static  boolean deleteImageFiile(Context context, String imagepath) {
     // Get the file
     File imageFile = new File(imagepath);

     // Delete the image
     boolean deleted = imageFile.delete();

     // checks the error
     if (!deleted){
         Toast.makeText(context, "File is not deleted", Toast.LENGTH_SHORT).show();
     }

     return deleted;
 }

 /*
 * Helper method for adding photo into the device
 * Gallery so that
 * other apps can use that photo
 *
 * **/
 private static void galleryAddPic(Context context, String imagePath){

     Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
     File f = new File(imagePath);
     Uri contentUri = Uri.fromFile(f);

     intent.setData(contentUri);;
     context.sendBroadcast(intent);
 }

 /*
 * Helper method for saving image.
 *
 * @param context the application context
 * @param Bitmap the image to be saved
 * @return The path of the saved image
 * **/

 static String saveImage(Context context,Bitmap image){

     String savedImagePath = null;

     // create the new file in the external storage

     String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
             Locale.getDefault()).format(new Date());
     String imageFileName = "JPEG_" + timeStamp + ".jpg";

     File file = new File(
             Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
             + "/FireEmoji");

     boolean success = true;

     if (!file.exists()){
         success = file.mkdirs();
     }

     //save the new Bitmap

     if (success) {
         File imageFile = new File(file,imageFileName);
         savedImagePath = imageFile.getAbsolutePath();

         try {
             OutputStream fOut = new FileOutputStream(imageFile);
             image.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
             fOut.close();
         }catch (Exception e){
             e.printStackTrace();
         }

         // Add the image to the gallery
         galleryAddPic(context, savedImagePath);


         // Show a Toast with the save location
         Toast.makeText(context, "Saved Location " + savedImagePath, Toast.LENGTH_SHORT).show();
     }
     return savedImagePath;
 }

/*
* Helper method for sharing an image
*
* @param context the image context
* @param imagePath The path of the image to be shared
* **/

  static void shareImage(Context context, String imagePath){
  // creating the share intent and start the share activity
  File imageFile = new File(imagePath);
  Intent shareIntent = new Intent(Intent.ACTION_SEND);
  shareIntent.setType("image/*");
  Uri photoUri = FileProvider.getUriForFile(context, FILE_PROVIDER_AUTHORITY, imageFile);
  shareIntent.putExtra(Intent.EXTRA_STREAM,photoUri);
  context.startActivity(shareIntent);
    }

 }

Файл манифеста: -

<uses-feature
     android:name="android.hardware.camera"
    android:required="true"/>

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

   <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <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>

    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>


    </activity>



     </application>

спасибо заранее :)

1 Ответ

0 голосов
/ 28 октября 2018

ссылка на изображение не работает, упростите ваши коды, пожалуйста.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...