2017-02-02 5 views
1

私は、ギャラリーやGoogleフォトから画像を選択し、それをトリミングして自分のアプリの背景にするようアプリを作ろうとしています。私が直面している問題は、Googleの写真を使って画像を切り抜こうとすると保存されますが、アプリの背景は変わらず、クラッシュやエラーは受け付けませんが、完璧に動作するようです。ここでGoogleフォトを使用してクロップインテントを使用しない

は、写真を選択するための私のコードです:ここでは

Intent i = new Intent(Intent.ACTION_GET_CONTENT, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
          startActivityForResult(i, SELECT_PICTURE); 

case SELECT_PICTURE: { 
      if (resultCode == RESULT_OK && null != data) { 
       mHandler.post(new Runnable() { 
        @Override 
        public void run() { 

         Uri selectedImageUri = data.getData(); 
         InputStream imageStream; 
         Bitmap selectedImage; 
         try { 
          cropCapturedImage(getImageUrlWithAuthority(getApplicationContext(),selectedImageUri)); 
         } catch (ActivityNotFoundException aNFE) { 
          //display an error message if user device doesn't support 
          showToast(getString(R.string.error_crop_not_supported)); 
          try { 
           String[] filePathColumn = {MediaStore.Images.Media.DATA}; 
           Cursor cursor = getContentResolver().query(getImageUrlWithAuthority(getApplicationContext(),selectedImageUri), filePathColumn, null, null, null); 
           cursor.moveToFirst(); 
           imageStream = getContentResolver().openInputStream(getImageUrlWithAuthority(getApplicationContext(),selectedImageUri)); 
           selectedImage = BitmapFactory.decodeStream(imageStream); 

           ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
           selectedImage.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
           byte[] b = baos.toByteArray(); 
           String encodedImage = Base64.encodeToString(b, Base64.DEFAULT); 
           //SharePreference to store image 
           PrefManager.putString(Constant.IMAGE_DATA, encodedImage); 
           cursor.close(); 
           //set gallery image 
           setChatBackground(); 
          } catch (FileNotFoundException e) { 
           e.printStackTrace(); 
          } 
         } 
        } 
       }); 
      } 

は私cropIntentコードです:

public void cropCapturedImage(Uri picUri) { 
    Intent cropIntent = new Intent("com.android.camera.action.CROP"); 
    cropIntent.setDataAndType(picUri, "image/*"); 
    cropIntent.putExtra("crop", "true"); 
    cropIntent.putExtra("aspectX", 9); 
    cropIntent.putExtra("aspectY", 14); 
    cropIntent.putExtra("outputX", 256); 
    cropIntent.putExtra("outputY", 256); 
    cropIntent.putExtra("return-data", true); 
    startActivityForResult(cropIntent, CROP_PICTURE); 
} 

ase CROP_PICTURE: { 
      Uri uri = data.getData(); 
      String[] filePathColumn = {MediaStore.Images.Media.DATA}; 
      Cursor cursor = getContentResolver().query(uri, filePathColumn, null, null, null); 
      if(cursor.moveToFirst()){ 
       int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
       String yourRealPath = cursor.getString(columnIndex); 
      } else { 
       //boooo, cursor doesn't have rows ... 
      } 
      cursor.close(); 
      String v= data.toString(); 

      if (resultCode == RESULT_OK && null != data) { 
       mHandler.post(new Runnable() { 
        @Override 
        public void run() { 
         try { 
          Bundle extras = data.getExtras(); 
          Bitmap thePic = extras.getParcelable("data"); 
          ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
          thePic.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
          byte[] b = baos.toByteArray(); 
          String encodedImage = Base64.encodeToString(b, Base64.DEFAULT); 
          //SharePreference to store image 
          PrefManager.putString(Constant.IMAGE_DATA, encodedImage); 
          //set gallery image 
          setChatBackground(); 
         } catch (NullPointerException e) { 
          Log.d(TAG, e.getLocalizedMessage()); 
         } 
        } 
       }); 
      } 
+0

[意図com.android.camera.action.CROPを処理するために見つかりませんアクティビティー](http://stackoverflow.com/questions/41890891/no-activity-の可能な重複 – W4R10CK

答えて

1

いいえ、のAndroidはcom.android.camera.action.CROPの行動と意思のメソッドを呼び出すstartActivity()クロップテント

ありません。彼らは画像を切り抜くためにこれをやっています。

これは本当に悪い考えです。

Source here - CommonsBlog

enter image description here

+0

私はivilani作物画像ライブラリを試してみて、cropCapturedImage()関数コードをそのサンプルアプリで提供されているものに置き換え、背景画像を設定するためのインテントを渡しました。これにより、画像も切り取られて保存されますが、アプリケーションの背景に変更はありません。これは、私がクロップインテントを使用してGoogleの写真に直面している問題に似ています。ありがとう。 –

0

はあなたのselectedImageUri最初のログを記録するようにしてください。

私は、Googleの写真がオンラインファイルにURLを返すと思われますが、ギャラリーアプリはローカルファイルパスを返します。

+0

Malmyginそれは最初の問題で、私はGoogleの写真から写真を選択できませんでした。一時ファイルに画像を書き込んで画像URIを取得することで解決しました。権威をもって今問題は作物の意図である。ギャラリーアプリとGoogleの写真は異なる作物意図データを提供しています。どのように私はそれを克服するのですか?ありがとう。 –

+0

さて、作物の意図を使用しないでください。インテントは、写真の選択と@ w4r10ckで提案されたライブラリの1つを使ってクロップを適用した後にのみ使用してください。 –

関連する問題