2017-02-26 117 views
2

在我的代碼中我使用了一個意圖拍照,但我真的不明白我在哪裏可以得到這個圖像。我在這個小應用程序中的目標是拍攝一張照片,然後用這張照片更新ImageView(並打開帶有其他意圖的Google地圖)。這兩個功能通過2個按鈕激活。如何從圖庫(或繪圖)獲取圖像以更新ImageView?

MainActivity.java:

import android.net.Uri; 
import android.os.Bundle; 
import android.provider.MediaStore; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.EditText; 

public class MainActivity extends AppCompatActivity { 


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

    static final int REQUEST_IMAGE_CAPTURE = 1; 

    public void activerCamera(View view) { 
     Intent prendrePhoto = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     if (prendrePhoto.resolveActivity(getPackageManager()) != null) { 
      startActivityForResult(prendrePhoto, REQUEST_IMAGE_CAPTURE); 
     } 
    } 

    public void googleMaps(View view) { 
     EditText nameField = (EditText) findViewById(R.id.name_field); 
     String name = nameField.getText().toString(); 
     Intent intent = new Intent(Intent.ACTION_VIEW); 
     intent.setData(Uri.parse("geo:" + name)); 
     if (intent.resolveActivity(getPackageManager()) != null) { 
      startActivity(intent); 
     } 
    } 
} 

我activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    tools:context="com.example.android.photoapp.MainActivity"> 


    <EditText 
     android:id="@+id/name_field" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:hint="Exemple : 47.6, -122.3" 
     android:layout_gravity="center_horizontal" 
     android:layout_marginTop="24dp"/> 

    <Button 
     android:layout_marginTop="24dp" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Aller sur google maps" 
     android:layout_gravity="center_horizontal" 
     android:textAllCaps="true" 
     android:onClick="googleMaps"/> 
    <Button 
     android:layout_marginTop="24dp" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Prendre une photo" 
     android:layout_gravity="center_horizontal" 
     android:textAllCaps="true" 
     android:onClick="activerCamera"/> 

    <ImageView 
     android:id="@+id/imageFondEcran" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
</LinearLayout> 

我在很多論壇看到和教程像這樣(下)的一些方法,但我不能讓它工作正確

@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (data != null) { 
     Uri photoUri = data.getData(); 
     // Do something with the photo based on Uri 
     Bitmap selectedImage = MediaStore.Images.Media.getBitmap(this.getContentResolver(), photoUri); 
     // Load the selected image into a preview 
     ImageView ivPreview = (ImageView) findViewById(R.id.ivPreview); 
     ivPreview.setImageBitmap(selectedImage); 
    } } 
+0

爲什麼你的班級沒有'onActivityResult'方法?你絕對需要這個。 –

+0

你是怎麼做到的?這與'startActivityForResult'不一樣?我應該在哪裏設置該方法'onActivityResult'? – Hix

+0

我只是說你叫'startActivityForResult',所以你在哪裏試着「捕捉」那個結果? –

回答

2

我在很多論壇和教程看到一些像這樣的方法(下),但我不能讓它工作正常

由於該代碼是錯誤的大多數相機應用程序,我不驚訝,它不適合你。

您正在提出ACTION_IMAGE_CAPTURE請求,但不包括EXTRA_OUTPUT來說明圖像應該放在哪裏。因此,您的照片應爲Bitmap,通過致電getParcelableExtra("data")獲得Intent發送至onActivityResult()

所以,變化:

if (data != null) { 
    Uri photoUri = data.getData(); 
    // Do something with the photo based on Uri 
    Bitmap selectedImage = MediaStore.Images.Media.getBitmap(this.getContentResolver(), photoUri); 
    // Load the selected image into a preview 
    ImageView ivPreview = (ImageView) findViewById(R.id.ivPreview); 
    ivPreview.setImageBitmap(selectedImage); 
} 

到:

if (requestCode==REQUEST_IMAGE_CAPTURE && resultCode==RESULT_OK) { 
    Bitmap selectedImage = (Bitmap)data.getParcelableExtra("data"); 
    // Load the selected image into a preview 
    ImageView ivPreview = (ImageView) findViewById(R.id.ivPreview); 
    ivPreview.setImageBitmap(selectedImage); 
} 
+0

Woaw我真的很感動。它完全工作。我不明白爲什麼onActivityResult工作,因爲沒有地方我們稱之爲。你能解釋一下嗎? (並感謝您的快速回答!我從小時候開始工作......)謝謝! – Hix

+0

@Hix:「我不明白爲什麼onActivityResult工作,因爲沒有我們稱之爲的地方」 - 在你的活動中有很多回調被框架調用,而不是你。 'onCreate()'就是一個例子。 'onActivityResult()'是另一個。在調用'startActivityForResult()'後,'onActivityResult()'將作爲你的活動的一部分返回到前臺調用。任何體面的Android應用程序開發書籍或課程都涵蓋了這些類型的回調。 – CommonsWare

+0

好的,我明白了,它有點混亂。你知道任何描述這些回調的網站嗎?我諮詢了developper.android.com文檔,但我無法理解,就像我看到的那樣。 – Hix

0

試試這個:

在你的活動,你可以調用這個讓你繪製資源作爲可繪製對象:

Drawable d = getResources().getDrawable(R.drawable.yourimage); 

如果你w螞蟻,以顯示您繪製在ImageView的,你可以這樣做:

ImageView i = new ImageView(this); 
i.setImageResource(R.drawable.yourimage); 

或XML文件:

<ImageView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/smiley"/> 

祝你好運!

+0

向下的人回答我的問題,但我會明確地嘗試它!非常感謝 ! – Hix