Mostrare una foto in una ImageView

di il
3 risposte

Mostrare una foto in una ImageView

Ciao a tutti è da tanto che sto avendo problema che mi sembra abbastanza stupido solo che non capisco cosa sbaglio. Praticamente l'app dovrebbe fare una foto e mostrarla in una ImageView, il punto è che funziona tutto, non ci sono errori eppure l'anteprima nell'ImageView non c'è.
Questo è il codice:

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class home extends Fragment {
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState){
		MioDatabaseHelper helper;
		
	    
	    
		View v =  inflater.inflate(R.layout.home, container, false);
		Button fotocamera = (Button) v.findViewById(R.id.avvio_fotocamera);
		
		
	
		fotocamera.setOnClickListener(new View.OnClickListener() {
		
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				
				
				
				SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd-HHmmss");
				String fileName = dateFormat.format(new Date()) + ".jpg";
				File noncelafacciopiu = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), fileName);
				Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
				i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(noncelafacciopiu));
				startActivityForResult(i, 1);
			}
			
			
			
			
		});
		
		
		
	return v;
}
	public void onActivityResult(int requestCode, int resultCode,Intent intent)

    {
        if (requestCode == 100) 
         {
            if (resultCode == Activity.RESULT_OK) 
            {
                if (intent == null) 
                {
                    // The picture was taken but not returned
                    Toast.makeText(
                            
                            null, "The picture was taken and is located here: ", resultCode
                                    )
                            .show();
                }
                else 
                {
                    // The picture was returned
                    Bundle extras = intent.getExtras();
                    Bitmap img=(Bitmap) extras.get("data");    
                    ImageView imageView1 = (ImageView) getActivity().findViewById(R.id.imageView);
                    imageView1.setImageBitmap(img);
                }
            }
        }

3 Risposte

  • Re: Mostrare una foto in una ImageView

    Il motivo è semplice: hai usato 2 "request code" diversi:
    nel metodo "startActivityForResult" usi il request code 1, mentre nell'"onActivityResult" usi 100.
    
    startActivityForResult(i, 1);
    ....
    if (requestCode == 100) 
  • Re: Mostrare una foto in una ImageView

    Ho corretto l'errore solo che ora mi da un errore, resultcode e requestcode che riceve sono diversi, mi spiegherò meglio con il log dell'app:
    08-05 09:20:45.017: E/AndroidRuntime(2879): FATAL EXCEPTION: main
    08-05 09:20:45.017: E/AndroidRuntime(2879): java.lang.RuntimeException: Unable to resume activity {tecno.cosaindosso/tecno.cosaindosso.MainActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=android:fragment:0, request=20, result=-1, data=null} to activity {tecno.cosaindosso/tecno.cosaindosso.MainActivity}: java.lang.NullPointerException
    08-05 09:20:45.017: E/AndroidRuntime(2879): 	at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3014)
    08-05 09:20:45.017: E/AndroidRuntime(2879): 	at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3055)
    08-05 09:20:45.017: E/AndroidRuntime(2879): 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392)
    08-05 09:20:45.017: E/AndroidRuntime(2879): 	at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4032)
    08-05 09:20:45.017: E/AndroidRuntime(2879): 	at android.app.ActivityThread.access$700(ActivityThread.java:151)
    08-05 09:20:45.017: E/AndroidRuntime(2879): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1337)
    08-05 09:20:45.017: E/AndroidRuntime(2879): 	at android.os.Handler.dispatchMessage(Handler.java:99)
    08-05 09:20:45.017: E/AndroidRuntime(2879): 	at android.os.Looper.loop(Looper.java:155)
    08-05 09:20:45.017: E/AndroidRuntime(2879): 	at android.app.ActivityThread.main(ActivityThread.java:5454)
    08-05 09:20:45.017: E/AndroidRuntime(2879): 	at java.lang.reflect.Method.invokeNative(Native Method)
    08-05 09:20:45.017: E/AndroidRuntime(2879): 	at java.lang.reflect.Method.invoke(Method.java:511)
    08-05 09:20:45.017: E/AndroidRuntime(2879): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
    08-05 09:20:45.017: E/AndroidRuntime(2879): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
    08-05 09:20:45.017: E/AndroidRuntime(2879): 	at dalvik.system.NativeStart.main(Native Method)
    08-05 09:20:45.017: E/AndroidRuntime(2879): Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=android:fragment:0, request=20, result=-1, data=null} to activity {tecno.cosaindosso/tecno.cosaindosso.MainActivity}: java.lang.NullPointerException
    08-05 09:20:45.017: E/AndroidRuntime(2879): 	at android.app.ActivityThread.deliverResults(ActivityThread.java:3622)
    08-05 09:20:45.017: E/AndroidRuntime(2879): 	at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2988)
    08-05 09:20:45.017: E/AndroidRuntime(2879): 	... 13 more
    08-05 09:20:45.017: E/AndroidRuntime(2879): Caused by: java.lang.NullPointerException
    08-05 09:20:45.017: E/AndroidRuntime(2879): 	at tecno.cosaindosso.home.onActivityResult(home.java:68)
    08-05 09:20:45.017: E/AndroidRuntime(2879): 	at android.app.Activity.dispatchActivityResult(Activity.java:5279)
    08-05 09:20:45.017: E/AndroidRuntime(2879): 	at android.app.ActivityThread.deliverResults(ActivityThread.java:3618)
    08-05 09:20:45.017: E/AndroidRuntime(2879): 	... 14 more
    
  • Re: Mostrare una foto in una ImageView

    Qual è la riga 68 della classe home?
Devi accedere o registrarti per scrivere nel forum
3 risposte