2015-01-06 2 views
0

모바일 사진 라이브러리에 액세스하고 선택 후 사진을 업로드하는 응용 프로그램을 개발 중입니다. 응용 프로그램은 카메라로 찍은 사진을 제외한 모든 사진을 완벽하게로드합니다. 누군가 나를 도와 주려고합니까?사진 라이브러리에 액세스 - 안드로이드 응용 프로그램

MainActivity.java

import android.content.Intent; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.Bundle; 
import android.support.v7.app.ActionBarActivity; 
import android.view.View; 
import android.widget.ImageView; 

import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.io.InputStream; 


public class MainActivity extends ActionBarActivity { 

ImageView img; 
private Bitmap bitmap; 

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

    img = (ImageView) findViewById(R.id.imageView); 
    img.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      abrirFoto(); 
     } 

    }); 
} 

public void abrirFoto() { 
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
    intent.setType("image/*"); 
    intent.addCategory(Intent.CATEGORY_OPENABLE); 
    startActivityForResult(intent, 1); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    InputStream stream = null; 
    if (requestCode == 1 && resultCode == RESULT_OK) { 
     try { 
      if (bitmap != null) { 
       bitmap.recycle(); 
      } 
      stream = getContentResolver().openInputStream(data.getData()); 
      bitmap = BitmapFactory.decodeStream(stream); 
      img.setImageBitmap(bitmap); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } finally { 
      if (stream != null) 
       try { 
        stream.close(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
     } 

    } 
} 
} 

activity_main.xml

<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" 
android:background="#0B9AE2"> 

<ImageView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/imageView" 
    android:src="@drawable/fotos1" 
    android:layout_centerVertical="true" 
    android:layout_centerHorizontal="true" 
    android:layout_weight = "1"/> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceMedium" 
    android:text="Clique para acessar as fotos do celular" 
    android:id="@+id/textView2" 
    android:layout_centerHorizontal="true" 
    android:layout_weight = "1"/> 

</RelativeLayout> 

답변

0

내가 질문을 읽으면서, 난 당신이 카메라를 사용하여 사진을 캡처하려고 노력하고 설정하려고하는 것을 이해 이미지 뷰로. 내가 틀렸다면 나를 바로 잡아라.

카메라에서 사진을 업로드하려면 다른 의도를 시도해야합니다.

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
startActivityForResult(cameraIntent, CAMERA_REQUEST); 

onActivityResult 메서드에 다음 코드 줄을 추가하십시오. 나는 그것이 바로 당신이 당신의 장치 카메라로 사진을 촬영하고 응용 프로그램에서 그 사진을 사용하려는 얻을 경우

if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) { 
      Bitmap photo = (Bitmap) data.getExtras().get("data"); 
      img.setImageBitmap(photo); 
     } 
+0

내 코드는 메모리에있는 이미지의 모바일 라이브러리에 액세스합니다. 내 질문은 내 휴대 전화에서 사진 라이브러리에 액세스 할 때 내가 선택한 모든 사진은 카메라로 찍은 사진을 제외하고 ImageView를 업데이트한다는 것입니다. 내 코드를 실행하면 완벽하게 이해할 수 있습니다. 고맙습니다. @Prem –

0

, 잘가 많은 예제는 그 목적을 위해 온라인 그러나 여기 내가 당신에게 무언가를 줄 것이다 그 날 글로벌 변수로 사람들을 1 정의 : 내가 여기에 생각하는 코드가 적어도 이해하기 쉬운 생각

Intent i; 
final static int cameraData=0; 
Bitmap bmp; 

2-in을 클릭 방법 또는 당신이 걸릴 사용하고 무엇을 적 방법에 사진 사용 :

i=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
     startActivityForResult(i,cameraData); 

3 호출하여 onActivityResult() 메소드 : 여기

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    // TODO Auto-generated method stub 
    super.onActivityResult(requestCode, resultCode, data); 
    if(resultCode == RESULT_OK){ 
     Bundle extras=data.getExtras(); 
     bmp = (Bitmap) extras.get("data"); 
     iv.setImageBitmap(bmp); 
    } 
} 

당신이 가고, 난 당신을 위해 작동합니다 희망을 찾을 수있는 간단한. 행운을 빕니다

+0

내 코드는 메모리에있는 이미지의 모바일 라이브러리에 액세스합니다. 내 질문은 내 휴대 전화에서 사진 라이브러리에 액세스 할 때 내가 선택한 모든 사진은 카메라로 찍은 사진을 제외하고 ImageView를 업데이트한다는 것입니다. 내 코드를 실행하면 완벽하게 이해할 수 있습니다. 고맙습니다. @ 아흐 마드 알 사니 –

관련 문제