2017-09-21 3 views

答えて

0

画像がフルスクリーンの場合は、独自のスクリーンショットを作成するだけです。

public static Bitmap takeScreenshotOfView(Activity context, Bitmap.CompressFormat compressFormat){ 
     Bitmap screenshot = null; 

    try { 
    // create bitmap screen capture 
    View v1 = context.getWindow().getDecorView().getRootView(); 
    v1.setDrawingCacheEnabled(true); 
    screenshot = Bitmap.createBitmap(v1.getDrawingCache()); 
    v1.setDrawingCacheEnabled(false); 

    File imageFile = new File(context.getFilesDir() + File.separator + "A35_temp" + File.separator + "screenshot_temp"); 

    FileOutputStream outputStream = new FileOutputStream(imageFile); 
    int quality = 100; 

    screenshot.compress(compressFormat, quality, outputStream); 
    outputStream.flush(); 
    outputStream.close(); 

} catch (Throwable e) { 
    // Several error may come out with file handling or OOM 
    e.printStackTrace(); 
} 

    return screenshot; 
} 

そして、ちょうど同じようにそれを使用します:

Bitmap screenshot = ImageHelper.takeScreenshotOfView(this, Bitmap.CompressFormat.JPEG); 
したいか、私はこのようImageHelperをという名前のクラスに入れて簡単なコードを使用することができればあなたのためにこれを行います偉大なライブラリがたくさんあります

ImageViewの領域をキャプチャするだけであれば、ビューで取得するか、両方のイメージビューの親を取得してそれらを含めることができます。フルスクリーンを指定することもできますが、imageViewの開始点と終了点から開始することができます。個々の領域のスクリーンショットを管理するために書いたサンプルコードです。

private void convertCardToBitmap(boolean sendOnComplete){ 
    if(!mIsForStore) { 
     Toast.makeText(this, getString(R.string.downloading_to_gallery), Toast.LENGTH_LONG).show(); 

    } 

    CardView cardView = (CardView)findViewById(R.id.my_image_view_id); 

    cardView.setDrawingCacheEnabled(true); 

    cardView.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)); 
    //cardView.measure((int)height, (int)width); 
    cardView.layout(0, 0, cardView.getMeasuredWidth(), cardView.getMeasuredHeight()); 
    //cardView.layout(0, 0, (int)width, (int)height); 
    cardView.buildDrawingCache(true); 

    Bitmap bitmap = Bitmap.createBitmap(cardView.getDrawingCache()); 

    saveBitmapToGallery(bitmap, sendOnComplete); 

} 
private void saveBitmapToGallery(Bitmap cardToSave, boolean sendFileWhenSaved){ 
    OutputStream output; 
    // Find the SD Card path 
    File filepath = Environment.getExternalStorageDirectory(); 

    // Create a new folder in SD Card 
    File dir = new File(filepath.getAbsolutePath() + "/MyAppPathDir/"); 
    dir.mkdirs(); 

    String imageName = mSelectedPerson.getFirstName() + "_" + mSelectedPerson.getLastName() + "_" + FileNameHelper.getNowTimeStamp(); 

    // Create a name for the saved image 
    File file = new File(dir, imageName + ".jpg"); 

    try { 
     int count = 1; 
     //if file exists add 1 on the end and loop until finding a name that doesn't exist. 
     while(file.exists()){ 
      file = new File(dir, imageName + count + ".jpg"); 
      count++; 

     } 

     output = new FileOutputStream(file); 

     // Compress into png format image from 0% - 100% 
     cardToSave.compress(Bitmap.CompressFormat.JPEG, 100, output); 
     output.flush(); 
     output.close(); 

     addImageToGallery(file.getAbsolutePath()); 

     if(sendFileWhenSaved){ 
      Intent intent = getIntent(); 
      intent.putExtra(Globals.INTENT_KEYS.KEY_FILE_TO_SHARE, file.getAbsolutePath()); 
      intent.putExtra(Globals.INTENT_KEYS.KEY_SELECTED_IMAGE, mSelectedCardModel); 
      setResult(RESULT_OK, intent); 
      finish(); 

     } else if (mIsForStore) { 
      uploadImageToS3(file.getAbsolutePath()); 

     } else{ 
      Toast.makeText(this, getString(R.string.saved_to_gallery), Toast.LENGTH_LONG).show(); 
      finish(); 

     } 

    } catch (Exception e) { 
     Toast.makeText(this, getString(R.string.error_failed_save_to_gallery) + e.getMessage(), Toast.LENGTH_LONG).show(); 

    } 
} 
private void uploadImageToS3(String filePath){ 
    final File newImageFile = new File(filePath); 
    showProgressDialog(TAG, getString(R.string.loading_please_wait)); 

    //For auth route 
    BasicAWSCredentials credentials = new BasicAWSCredentials(CognitoManager.getS3ClientID(), CognitoManager.getS3ClientSecret()); 

    AmazonS3Client s3 = new AmazonS3Client(credentials); 
    TransferUtility transferUtility = new TransferUtility(s3, this); 
    TransferObserver observer = transferUtility.upload(CognitoManager.getS3BucketName(), newImageFile.getName(), newImageFile); 
    observer.setTransferListener(new TransferListener() { 
     @Override 
     public void onStateChanged(int id, TransferState state) { 
      if(state.compareTo(TransferState.COMPLETED) == 0){ 
       String imgURLOfUploadComplete = "https://s3.amazonaws.com/" + CognitoManager.getS3BucketName() + "/" + newImageFile.getName(); 
       hideProgressDialog(TAG); 
       Intent intent = new Intent(); 
       intent.putExtra(Globals.INTENT_KEYS.KEY_IMAGE_URL, imgURLOfUploadComplete); 
       setResult(Activity.RESULT_OK, intent); 
       if(newImageFile.exists()){ 
        newImageFile.delete(); 

       } 
       finish(); 

      } 

     } 
     @Override 
     public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) { 
      if(bytesTotal != 0) { 
       //For viewing progress 
       int percentage = (int) (bytesCurrent/bytesTotal * 100); 
      } 
     } 
     @Override 
     public void onError(int id, Exception ex) { 
      A35Log.e(TAG, getString(R.string.error_uploading_s3_part1) + id + getString(R.string.error_uploading_s3_part2) + ex.getMessage()); 
      hideProgressDialog(TAG); 
      showDialogMessage(getString(error), getString(R.string.error_failed_create_image_alert_id) + error); 

     } 

    }); 

} 
public void addImageToGallery(final String filePath) { 
    ContentValues values = new ContentValues(); 

    values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis()); 
    values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg"); 
    values.put(MediaStore.MediaColumns.DATA, filePath); 

    getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); 
} 

ホープが役に立ちます:)。 setDrawCacheEnabledを起動すると、画面上で奇妙な動作が発生し、場合によっては制約を削除するように見えるため、再描画が必要になることがありますが、警告が表示されます。この理由から、私はしばしば、イメージに変換しているコンテンツを含む新しい空白の画面を開き、作業を終了して閉じます。

これはあなたのためにどのように動作するかを見て、そこからどこに行くのかを決めます。私が試したほとんどの図書館も同じ問題を抱えていたので、私は自分自身で書いたのです。

関連する問題