2016-12-05 12 views
0

パブリッククラスPdfRendererBasicFragmentを行っていませんが、これは私のコードであり、フラグメントはView.OnClickListener {onRequestPermissionsResult()()onViewCreated前に終了

/** 
* 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; 
     } 


    } 
} 

}

HIを実装して延びています。

修正しようとしています。しかしそれはうまくいっていない。

問題はこれです。

このアプリをいつ実行するのですか? Androidリクエストの許可とI承認。

しかし、その時点でアンドロイドは 'openRenderer(activity)'が実行される前にonViewCreated(ビューの表示、Bundle savedInstanceState)メソッドを実行します。 'showPage(index)'はエラーを発生させます。その後(すでに承認されていることを意味し、再度実行する)アプリはうまくいきます。

しかし、私はそれを修正したいと思います。

は私が初めてで

を考えて、ONATTACH(アクティビティ活動が) を実行し、許可後に承認され、 ONATTACH(活動活性)と onViewCreated(ビュービュー、バンドルsavedInstanceState) 方法はで動作するように思えます同時。

助けてください。

ありがとうございます。

+0

私はshowPage(index)を探します。 openRendererの後(アクティビティ)。 –

+0

が動作していません。 –

+0

ターンアウトです。 onRequestPermissionsResultが呼び出されませんでした。私は理由を調べる。 ActivityCompat.requestPermissions(thisActivity、permissions、 EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE)を使用したため、 –

答えて

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メソッドを呼び出して終了するために発生したと考えました。 onRequestPermissionsResultが呼び出される前にonViewCreated startが呼び出され(so openRenderer start)

したがって、PERMISSION_GRANTEDまでshowPageメソッドを中断します。

関連する問題