onRequestPermissionsResult()在onViewCreated()行为

时间:2016-12-05 22:17:02

标签: android-fragments permissions request

public class PdfRendererBasicFragment extends Fragment实现View.OnClickListener {

/**
 * Key string for saving the state of current page index.
 */
private static final String STATE_CURRENT_PAGE_INDEX = "current_page_index";
public static final int EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE = 2;

/**
 * File descriptor of the PDF.
 */


/**
 * {@link android.graphics.pdf.PdfRenderer} to render the PDF.
 */
private PdfRenderer mPdfRenderer;

/**
 * Page that is currently shown on the screen.
 */
private PdfRenderer.Page mCurrentPage;

/**
 * {@link android.widget.ImageView} that shows a PDF page as a {@link android.graphics.Bitmap}
 */
private ImageView mImageView;

/**
 * {@link android.widget.Button} to move to the previous page.
 */
private Button mButtonPrevious;

/**
 * {@link android.widget.Button} to move to the next page.
 */
private Button mButtonNext;


private Bundle copySavedInstanceState;


public PdfRendererBasicFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_pdf_renderer_basic, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    // Retain view references.
    mImageView = (ImageView) view.findViewById(R.id.image);
    mButtonPrevious = (Button) view.findViewById(R.id.previous);
    mButtonNext = (Button) view.findViewById(R.id.next);
    // Bind events.
    mButtonPrevious.setOnClickListener(this);
    mButtonNext.setOnClickListener(this);
    mImageView.setOnClickListener(this);

    copySavedInstanceState = savedInstanceState;


    while (true) {

        int rc = ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE);

        if (rc == PackageManager.PERMISSION_GRANTED)
            break;
    }
    // Show the first page by default.
    int index = 0;
    // If there is a savedInstanceState (screen orientations, etc.), we restore the page index.
    if (null != copySavedInstanceState) {
        index = copySavedInstanceState.getInt(STATE_CURRENT_PAGE_INDEX, 0);
    }

    Log.d("check","index : " + index);

    showPage(index);


}


@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);

    int rc = ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE);

    if (rc == PackageManager.PERMISSION_GRANTED) {
        try {
            openRenderer(activity);

        } catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(activity, "Error! " + e.getMessage(), Toast.LENGTH_SHORT).show();
            activity.finish();
        }
    } else {
        requestExternalStoragePermission();
    }
}


@Override
public void onDetach() {
    try {
        closeRenderer();
    } catch (IOException e) {
        e.printStackTrace();
    }
    super.onDetach();
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    if (null != mCurrentPage) {
        outState.putInt(STATE_CURRENT_PAGE_INDEX, mCurrentPage.getIndex());
    }
}

private void requestExternalStoragePermission() {

    final String[] permissions = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE};

    if (!ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
        ActivityCompat.requestPermissions(getActivity(), permissions, EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE);
        return;
    }

    final Activity thisActivity = getActivity();

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ActivityCompat.requestPermissions(thisActivity, permissions,
                    EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE);
        }
    };

}

@Override
public void onRequestPermissionsResult(int requestCode,
                                       @NonNull String[] permissions,
                                       @NonNull int[] grantResults) {
    if (requestCode != EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE) {
        Log.d("TAG", "Got unexpected permission result: " + requestCode);
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        return;
    }

    if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        Log.d("TAG", "Camera permission granted - initialize the camera source");

        // we have permission, so can read SD Card now.
        try {
            openRenderer(getActivity());

        } catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(getActivity(), "Error! " + e.getMessage(), Toast.LENGTH_SHORT).show();
            getActivity().finish();
        }

        return;
    }

    Log.e("TAG", "Permission not granted: results len = " + grantResults.length +
            " Result code = " + (grantResults.length > 0 ? grantResults[0] : "(empty)"));

    DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {

        }
    };

}

/**
 * Sets up a {@link android.graphics.pdf.PdfRenderer} and related resources.
 */
private void openRenderer(Context context) throws IOException {

    File file = new File("/sdcard/Download/test.pdf");
    mPdfRenderer = new PdfRenderer(ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY));


}

/**
 * Closes the {@link android.graphics.pdf.PdfRenderer} and related resources.
 *
 * @throws java.io.IOException When the PDF file cannot be closed.
 */
private void closeRenderer() throws IOException {
    if (null != mCurrentPage) {
        mCurrentPage.close();
    }
    mPdfRenderer.close();

}

/**
 * Shows the specified page of PDF to the screen.
 *
 * @param index The page index.
 */
private void showPage(int index) {
    if (mPdfRenderer.getPageCount() <= index) {
        return;
    }
    // Make sure to close the current page before opening another one.
    if (null != mCurrentPage) {
        mCurrentPage.close();
    }
    // Use `openPage` to open a specific page in PDF.
    mCurrentPage = mPdfRenderer.openPage(index);
    // Important: the destination bitmap must be ARGB (not RGB).
    Bitmap bitmap = Bitmap.createBitmap(mCurrentPage.getWidth(), mCurrentPage.getHeight(),
            Bitmap.Config.ARGB_8888);
    // Here, we render the page onto the Bitmap.
    // To render a portion of the page, use the second and third parameter. Pass nulls to get
    // the default result.
    // Pass either RENDER_MODE_FOR_DISPLAY or RENDER_MODE_FOR_PRINT for the last parameter.
    mCurrentPage.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
    // We are ready to show the Bitmap to user.
    mImageView.setImageBitmap(bitmap);
    updateUi();
}

/**
 * Updates the state of 2 control buttons in response to the current page index.
 */
private void updateUi() {
    int index = mCurrentPage.getIndex();
    int pageCount = mPdfRenderer.getPageCount();
    mButtonPrevious.setEnabled(0 != index);
    mButtonNext.setEnabled(index + 1 < pageCount);
    getActivity().setTitle(getString(R.string.app_name_with_index, index + 1, pageCount));
}

/**
 * Gets the number of pages in the PDF. This method is marked as public for testing.
 *
 * @return The number of pages.
 */
public int getPageCount() {
    return mPdfRenderer.getPageCount();
}

@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.previous: {
            // Move to the previous page
            showPage(mCurrentPage.getIndex() - 1);
            break;
        }
        case R.id.next: {
            // Move to the next page
            Log.d("name", mCurrentPage.getIndex() + "");
            showPage(mCurrentPage.getIndex() + 1);
            break;

        }
        case R.id.image: {
            Log.d("name", mCurrentPage.getIndex() + "");
            break;
        }


    }
}

}

嗨,这是我的代码。

我试着解决。但它进展不顺利。

问题是这个。

当我开展此应用时。 Android请求权限,我批准。

但当时android执行onViewCreated(View view,Bundle savedInstanceState)&#39;之前的方法&#39; openRenderer(活动)&#39;已经完成了。 所以&#39; showPage(索引)&#39;提出一些错误。然后(我的意思是已经批准,并再次执行)应用程序运行良好。

但我想解决这个问题。

我认为

第一次,onAttach(活动活动)执行 经许可批准后, onAttach(活动活动)和 onViewCreated(视图视图,Bundle savedInstanceState) 方法似乎同时运行。

请建议我。

谢谢。

1 个答案:

答案 0 :(得分:0)

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {

    super.onViewCreated(view, savedInstanceState);
    // Retain view references.
    Log.d("timeStamp", "onViewCreated ");
    mImageView = (ImageView) view.findViewById(R.id.image);
    mButtonPrevious = (Button) view.findViewById(R.id.previous);
    mButtonNext = (Button) view.findViewById(R.id.next);
    // Bind events.
    mButtonPrevious.setOnClickListener(this);
    mButtonNext.setOnClickListener(this);
    mImageView.setOnClickListener(this);


    Log.d("timeStamp", "onViewCreated 할당완료");


    while (true) {
        int rc = getActivity().checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);

        if (rc == PackageManager.PERMISSION_GRANTED){
            try {
                openRenderer(getActivity());

            } catch (IOException e) {
                e.printStackTrace();
                Toast.makeText(getActivity(), "Error! " + e.getMessage(), Toast.LENGTH_SHORT).show();
                getActivity().finish();
            }
            break;
        }


        Log.d("timeStamp", "얼마나 기다리는겨");
    }


    // Show the first page by default.
    int index = 0;
    // If there is a savedInstanceState (screen orientations, etc.), we restore the page index.
    if (null != savedInstanceState) {
        index = savedInstanceState.getInt(STATE_CURRENT_PAGE_INDEX, 0);
    }

    Log.d("timeStamp", "showPage 호출 직전");

    showPage(index);


}

}

我避免这个问题来添加此代码。

 while (true) {
    int rc = getActivity().checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);

    if (rc == PackageManager.PERMISSION_GRANTED){
        try {
            openRenderer(getActivity());

        } catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(getActivity(), "Error! " + e.getMessage(), Toast.LENGTH_SHORT).show();
            getActivity().finish();
        }
        break;
    }


    Log.d("timeStamp", "얼마나 기다리는겨");
}

但是。这不是最好的。

我认为这是因为onAttach方法调用requestPermissions方法并结束。 onViewCreated在用户回答request和onRequestPermissionsResult之前启动(所以openRenderer启动)。

所以我暂停showPage方法直到PERMISSION_GRANTED。