2017-06-24 14 views
-4

からのEditTextに画像を添付し、ノートでは、カメラやギャラリーは、私は本当にこのようにそれを行うにはどのよう見当がつかないギャラリーやカメラ

image

から他の機能を写真を添付することができます上部に示されているように、私はそれについてのチュートリアルは見つけていません。私は助けが必要です、ありがとう。私は、この問題の範囲は、ビット幅の広いかもしれないが、私はのように最高の私ができるようにそれを要約してみます

image2

+0

誰かの助け:((( –

+1

まあ、おそらくあなたは、あなたのポストのフォーマットが明確に仕事を必要とする最初以来、キューの中の編集を受け入れることができます。 – DaveNOTDavid

答えて

0

初心者です。

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

次へ]を次のように

ユーザーのサードパーティ製のギャラリーアプリ、と言うから画像をレンダリングするためには、最初にあなたのマニフェストに記憶許可を設定することにより、最初の自分のデバイスのストレージにアクセスする必要があるだろうhere in the docsのUIスレッドを実行する前に、Android 6.0/Marshmallow以上の実行時(Android 6.0以降のインストール時)に、アクセス権の付与(ユーザーのデバイスストレージへのアクセスは危険なアクセス権とみなされます)に対処する必要があります。その後、ボタンをクリックしてギャラリーアプリケーションを開き、onActivityResult()内のすべてのカーソルを介してストレージデータを使用して、選択したイメージのURIパスにビットマップを持つImageViewをレンダリングします。ここで

がちょうどそののサンプルアクティビティの:

public class MainActivity extends AppCompatActivity { 

    // Constant that's used as a parameter to assist with the permission requesting process. 
    private static final int PERMISSION_CODE = 100; 

    // Int constant that's used to handle the result back when an image is selected from the 
    // device's gallery. 
    private static final int RESULT_LOAD_IMAGE = 1; 

    private ImageView mImageView; 

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

     // Requests permission for devices with versions Marshmallow (M)/API 23 or above. 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
      if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) 
        != PackageManager.PERMISSION_GRANTED) { 

       requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 
         PERMISSION_CODE); 

       return; 
      } 
     } 

     // The following invoking method either executes for versions older than M, or until the 
     // user accepts the in-app permission for the next sessions. 
     runUi(); 
    } 

    // Displays a permission dialog when requested for devices M and above. 
    @Override 
    public void onRequestPermissionsResult(int requestCode, String[] permissions, 
              int[] grantResults) { 
     if (requestCode == PERMISSION_CODE) { 

      // User accepts the permission(s). 
      if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

       // Invoker for rendering UI. 
       runUi(); 
      } else { // User denies the permission. 
       Toast.makeText(this, "Please come back and then grant permissions!", 
         Toast.LENGTH_SHORT).show(); 

       // Runs a thread for a slight delay prior to shutting down the app. 
       Thread mthread = new Thread() { 
        @Override 
        public void run() { 
         try { 
          sleep(1500); 
          System.exit(0); 
         } catch (InterruptedException e) { 
          e.printStackTrace(); 
         } 
        } 
       }; 

       mthread.start(); 
      } 
     } 
    } 

    private void runUi() { 
     mImageView = (ImageView) findViewById(R.id.image_view); 

     // Sets the image button clickable with the following functionality. 
     findViewById(R.id.change_img_btn).setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 

       // Instantiates an Intent object for accessing the device's storage. 
       Intent intent = new Intent(
         Intent.ACTION_PICK, 
         MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 

       // Triggers the image gallery. 
       startActivityForResult(intent, RESULT_LOAD_IMAGE); 
      } 
     }); 
    } 

    /** 
    * Invoked once a third-party app (such as Gallery) is dismissed from its purpose via an 
    * implicit intent. 
    * 
    * @param requestCode is the code constant of the intent's purpose. 
    * @param resultCode is the result code constant of the intent. 
    * @param data is the actual intent. 
    */ 
    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 

     // Runs the following code should the code constants and intent match that of selecting an 
     // image from the device's gallery. 
     if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && data != null) { 

      // References the device's storage URI for images from the intent parameter. 
      Uri selectedImageUri = data.getData(); 

      // Initializes a temporary string list of the image file path as the column to render 
      // the image immediately. 
      String[] projection = { MediaStore.Images.Media.DATA }; 

      // References and queries the database with the following parameters, and then moves to 
      // the first row index. 
      Cursor cursor = getContentResolver().query(
        selectedImageUri, // Provider content URI to query 
        projection,   // Columns to include in the resulting Cursor 
        null,    // No selection clause 
        null,    // No selection arguments 
        null);    // Default sort order 
      cursor.moveToFirst(); 

      // Retrieves and assigns the file path as a string value, and then sets the image's 
      // bitmap to render it. 
      String imgFilePath = cursor.getString(cursor.getColumnIndex(projection[0])); 
      mImageView.setImageBitmap(BitmapFactory.decodeFile(imgFilePath)); 

      // Closes the cursor to release all of its resources. 
      cursor.close(); 
     } 
    } 
} 
関連する問題