web-dev-qa-db-fra.com

Android: Erreur lors de l'envoi d'une image dans différentes versions d'Android

J'ai des problèmes avec l'image de téléchargement dans différentes versions d'Android. J'ai besoin d'envoyer des images pour le serveur php, donc j'utilise un webservice. J'ai testé avec les versions Froyo e Jelly Beans, elles fonctionnent mais le KitKat ne fonctionne pas. J'étais en train de lire à propos de MediaStore et j'ai vu différentes manières, et je ne sais pas si c'est vrai ou faux. 

J'ai débogué mon projet et je pouvais voir, dans KitKat le chemin est NULL, Logcat me dit "Pointeur Java Null" juste dans KitKat. Comment puis-je faire une application fonctionne pour toutes les versions.

UploadImage.Java

package br.gov.rj.barraemexposicao;

import Android.app.ProgressDialog;
import Android.content.ActivityNotFoundException;
import Android.content.Intent;
import Android.database.Cursor;
import Android.net.Uri;
import Android.provider.MediaStore;
import Android.support.v7.app.ActionBarActivity;
import Android.os.Bundle;
import Android.util.Log;
import Android.view.Menu;
import Android.view.MenuItem;
import Android.view.View;
import Android.widget.Button;
import Android.widget.ImageView;
import Android.widget.TextView;
import Android.widget.Toast;

import Java.io.DataOutputStream;
import Java.io.File;
import Java.io.FileInputStream;
import Java.net.HttpURLConnection;
import Java.net.MalformedURLException;
import Java.net.URL;


public class EnviaFoto extends ActionBarActivity {

    final int PICK_FILE_RESULT_CODE = 1;
    final int CAMERA_IMAGE =2;

    TextView messageText;
    Button uploadButton;
    Button btnTiraFoto;
    Button selectImage;
    ImageView imgFoto;

    int serverResponseCode = 0;
    ProgressDialog dialog = null;

    String upLoadServerUri = "http://www.kweekdesign.com.br/test/recebe/UploadToServer.php";

    String filePath = "";


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

        selectImage = (Button) findViewById(R.id.selectImage);
        imgFoto = (ImageView) findViewById(R.id.imgFoto);
        messageText  = (TextView)findViewById(R.id.messageText);
        uploadButton = (Button)findViewById(R.id.uploadButton);
        btnTiraFoto = (Button) findViewById(R.id.btnTiraFoto);

        upLoadServerUri = "http://www.kweekdesign.com.br/test/recebe/UploadToServer.php";

        addListeners();
    }

    public String uriToPath(Uri uri) {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }

    @Override
    protected void onActivityResult (int requestCode, int resultCode, Intent data){
        if (data != null && (requestCode == PICK_FILE_RESULT_CODE || requestCode == CAMERA_IMAGE)){

            filePath = uriToPath(data.getData());
            imgFoto.setImageURI(data.getData());
            uploadButton.setVisibility(1);

        }
    }

    public int uploadFile(String sourceFileUri) {
        String fileName = sourceFileUri;

        HttpURLConnection conn = null;
        DataOutputStream dos = null;
        String lineEnd = "\r\n";
        String twoHyphens = "--";
        String boundary = "*****";
        int bytesRead, bytesAvailable, bufferSize;
        byte[] buffer;
        int maxBufferSize = 1 * 1024 * 1024;
        File sourceFile = new File(sourceFileUri);

        if (!sourceFile.isFile()) {

            dialog.dismiss();

            Log.e("uploadFile", "Source File not exist :" + filePath);

            runOnUiThread(new Runnable() {
                public void run() {
                    messageText.setText("Source File not exist :"+filePath);
                }
            });

            return 0;

        }
        else
        {
            try {

                // open a URL connection to the Servlet
                FileInputStream fileInputStream = new FileInputStream(sourceFile);
                URL url = new URL(upLoadServerUri);

                // Open a HTTP  connection to  the URL
                conn = (HttpURLConnection) url.openConnection();
                conn.setDoInput(true); // Allow Inputs
                conn.setDoOutput(true); // Allow Outputs
                conn.setUseCaches(false); // Don't use a Cached Copy
                conn.setRequestMethod("POST");
                conn.setRequestProperty("Connection", "Keep-Alive");
                conn.setRequestProperty("ENCTYPE", "multipart/form-data");
                conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
                conn.setRequestProperty("uploaded_file", fileName);

                dos = new DataOutputStream(conn.getOutputStream());

                dos.writeBytes(twoHyphens + boundary + lineEnd);
                dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\""
                        + fileName + "\"" + lineEnd);

                dos.writeBytes(lineEnd);

                // create a buffer of  maximum size
                bytesAvailable = fileInputStream.available();

                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                buffer = new byte[bufferSize];

                // read file and write it into form...
                bytesRead = fileInputStream.read(buffer, 0, bufferSize);

                while (bytesRead > 0) {

                    dos.write(buffer, 0, bufferSize);
                    bytesAvailable = fileInputStream.available();
                    bufferSize = Math.min(bytesAvailable, maxBufferSize);
                    bytesRead = fileInputStream.read(buffer, 0, bufferSize);

                }

                // send multipart form data necesssary after file data...
                dos.writeBytes(lineEnd);
                dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

                // Responses from the server (code and message)
                serverResponseCode = conn.getResponseCode();
                String serverResponseMessage = conn.getResponseMessage();

                Log.i("uploadFile", "HTTP Response is : "
                        + serverResponseMessage + ": " + serverResponseCode);

                if(serverResponseCode == 200){

                    runOnUiThread(new Runnable() {
                        public void run() {

                            String msg = "Envio feito com sucesso!";

                            messageText.setText(msg);
                            Toast.makeText(EnviaFoto.this, "File Upload Complete.",
                                    Toast.LENGTH_SHORT).show();
                        }
                    });
                }

                //close the streams //
                fileInputStream.close();
                dos.flush();
                dos.close();

            } catch (MalformedURLException ex) {

                dialog.dismiss();
                ex.printStackTrace();

                runOnUiThread(new Runnable() {
                    public void run() {
                        messageText.setText("MalformedURLException Exception : check script url.");
                        Toast.makeText(EnviaFoto.this, "MalformedURLException",
                                Toast.LENGTH_SHORT).show();
                    }
                });

                Log.e("Upload file to server", "error: " + ex.getMessage(), ex);
            } catch (Exception e) {

                dialog.dismiss();
                e.printStackTrace();

                runOnUiThread(new Runnable() {
                    public void run() {
                        messageText.setText("Got Exception : see logcat ");
                        Toast.makeText(EnviaFoto.this, "Got Exception : see logcat ",
                                Toast.LENGTH_SHORT).show();
                    }
                });
                Log.e("Upload file to server Exception", "Exception : "
                        + e.getMessage(), e);
            }
            dialog.dismiss();
            return serverResponseCode;

        } // End else block
    }

    private void addListeners(){

        selectImage.setOnClickListener(new View.OnClickListener(){
            public void onClick(View view){
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                Intent chooser = Intent.createChooser(intent,"Escolha a foto");
                intent.setType("image/*");
                try {
                    startActivityForResult(chooser, PICK_FILE_RESULT_CODE );
                }catch (ActivityNotFoundException e){
                    Log.e("tag", e.getMessage());
                }
            }
        });

        uploadButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                dialog = ProgressDialog.show(EnviaFoto.this, "", "Enviando foto...", true);

                new Thread(new Runnable() {
                    public String uriToPath(Uri uri) {
                        String[] projection = { MediaStore.Images.Media.DATA };
                        Cursor cursor = managedQuery(uri, projection, null, null, null);
                        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                        cursor.moveToFirst();
                        return cursor.getString(column_index);
                    }


                    protected void onActivityResult (int requestCode, int resultCode, Intent data){
                        if (data != null && requestCode == PICK_FILE_RESULT_CODE){

                            filePath = uriToPath(data.getData());
                            imgFoto.setImageURI(data.getData());
                            uploadButton.setVisibility(1);

                        }
                    } public void run() {
                        runOnUiThread(new Runnable() {
                            public void run() {
                                messageText.setText("Enviando foto...");
                            }
                        });

                        uploadFile(filePath);

                    }
                }).start();
            }
        });

        btnTiraFoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE), CAMERA_IMAGE);
            }
        });


    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.envia_foto, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

Manifeste

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:Android="http://schemas.Android.com/apk/res/Android"
    package="br.gov.rj.barraemexposicao" >

    <uses-permission Android:name="Android.permission.INTERNET" />
    <uses-permission Android:name="Android.permission.READ_EXTERNAL_STORAGE" />

    <uses-sdk
        Android:minSdkVersion="8"
        Android:targetSdkVersion="19" />

    <application
        Android:allowBackup="true"
        Android:icon="@drawable/ic_launcher"
        Android:label="@string/app_name"
        Android:theme="@style/AppTheme" >
        <activity
            Android:name="br.gov.rj.barraemexposicao.EnviaFoto"
            Android:label="@string/app_name"
            Android:screenOrientation="portrait" >
            <intent-filter>

                <category Android:name="Android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            Android:name="br.gov.rj.barraemexposicao.Enquete"
            Android:label="@string/title_activity_enquete" >
            <intent-filter>
                <action Android:name="Android.intent.action.MAIN" />

                <category Android:name="Android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

LogCat

07-04 08:46:54.084  13565-13565/br.gov.rj.barraemexposicao I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build: AU_LINUX_Android_LNX.LA.3.5.1_RB1.04.04.02.048.018_msm8226_LNX.LA.3.5.1_RB1__release_AU ()
    OpenGL ES Shader Compiler Version: E031.24.00.08
    Build Date: 03/07/14 Fri
    Local Branch:
    Remote Branch: quic/LNX.LA.3.5.1_RB1.1
    Local Patches: NONE
    Reconstruct Branch: AU_LINUX_Android_LNX.LA.3.5.1_RB1.04.04.02.048.018 + f2fd134 +  NOTHING
07-04 08:46:54.157  13565-13565/br.gov.rj.barraemexposicao D/OpenGLRenderer﹕ Enabling debug mode 0
07-04 08:47:03.105  13565-13565/br.gov.rj.barraemexposicao D/AndroidRuntime﹕ Shutting down VM
07-04 08:47:03.121  13565-13565/br.gov.rj.barraemexposicao E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: br.gov.rj.barraemexposicao, PID: 13565
    Java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=-1, data=Intent { act=inline-data (has extras) }} to activity {br.gov.rj.barraemexposicao/br.gov.rj.barraemexposicao.EnviaFoto}: Java.lang.NullPointerException: Attempt to invoke virtual method 'Java.lang.String Android.net.Uri.getScheme()' on a null object reference
            at Android.app.ActivityThread.deliverResults(ActivityThread.Java:3432)
            at Android.app.ActivityThread.handleSendResult(ActivityThread.Java:3475)
            at Android.app.ActivityThread.access$1300(ActivityThread.Java:139)
            at Android.app.ActivityThread$H.handleMessage(ActivityThread.Java:1258)
            at Android.os.Handler.dispatchMessage(Handler.Java:102)
            at Android.os.Looper.loop(Looper.Java:136)
            at Android.app.ActivityThread.main(ActivityThread.Java:5086)
            at Java.lang.reflect.Method.invoke(Native Method)
            at com.Android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.Java:785)
            at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:601)
     Caused by: Java.lang.NullPointerException: Attempt to invoke virtual method 'Java.lang.String Android.net.Uri.getScheme()' on a null object reference
            at Android.content.ContentResolver.acquireUnstableProvider(ContentResolver.Java:1420)
            at Android.content.ContentResolver.query(ContentResolver.Java:445)
            at Android.content.ContentResolver.query(ContentResolver.Java:404)
            at br.gov.rj.barraemexposicao.EnviaFoto.uriToPath(EnviaFoto.Java:80)
            at br.gov.rj.barraemexposicao.EnviaFoto.onActivityResult(EnviaFoto.Java:97)
            at Android.app.Activity.dispatchActivityResult(Activity.Java:5446)
            at Android.app.ActivityThread.deliverResults(ActivityThread.Java:3428)
            at Android.app.ActivityThread.handleSendResult(ActivityThread.Java:3475)
            at Android.app.ActivityThread.access$1300(ActivityThread.Java:139)
            at Android.app.ActivityThread$H.handleMessage(ActivityThread.Java:1258)
            at Android.os.Handler.dispatchMessage(Handler.Java:102)
            at Android.os.Looper.loop(Looper.Java:136)
            at Android.app.ActivityThread.main(ActivityThread.Java:5086)
            at Java.lang.reflect.Method.invoke(Native Method)
            at com.Android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.Java:785)
            at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:601)
07-04 08:47:05.066  13565-13565/br.gov.rj.barraemexposicao I/Process﹕ Sending signal. PID: 13565 SIG: 9
07-04 08:51:33.298  15518-15518/br.gov.rj.barraemexposicao I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build: AU_LINUX_Android_LNX.LA.3.5.1_RB1.04.04.02.048.018_msm8226_LNX.LA.3.5.1_RB1__release_AU ()
    OpenGL ES Shader Compiler Version: E031.24.00.08
    Build Date: 03/07/14 Fri
    Local Branch:
    Remote Branch: quic/LNX.LA.3.5.1_RB1.1
    Local Patches: NONE
    Reconstruct Branch: AU_LINUX_Android_LNX.LA.3.5.1_RB1.04.04.02.048.018 + f2fd134 +  NOTHING
07-04 08:51:33.393  15518-15518/br.gov.rj.barraemexposicao D/OpenGLRenderer﹕ Enabling debug mode 0
07-04 08:52:08.015  15518-15530/br.gov.rj.barraemexposicao W/CursorWrapperInner﹕ Cursor finalized without prior close()
07-04 08:52:10.068  15518-16522/br.gov.rj.barraemexposicao E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-34287
    Process: br.gov.rj.barraemexposicao, PID: 15518
    Java.lang.NullPointerException: Attempt to invoke virtual method 'char[] Java.lang.String.toCharArray()' on a null object reference
            at Java.io.File.fixSlashes(File.Java:185)
            at Java.io.File.<init>(File.Java:134)
            at br.gov.rj.barraemexposicao.EnviaFoto.uploadFile(EnviaFoto.Java:102)
            at br.gov.rj.barraemexposicao.EnviaFoto$6$1.run(EnviaFoto.Java:277)
            at Java.lang.Thread.run(Thread.Java:811)
07-04 08:52:10.815  15518-15518/br.gov.rj.barraemexposicao E/WindowManager﹕ Android.view.WindowLeaked: Activity br.gov.rj.barraemexposicao.EnviaFoto has leaked window com.Android.internal.policy.impl.PhoneWindow$DecorView{65280820 V.E..... R......D 0,0-684,192} that was originally added here
            at Android.view.ViewRootImpl.<init>(ViewRootImpl.Java:359)
            at Android.view.WindowManagerGlobal.addView(WindowManagerGlobal.Java:248)
            at Android.view.WindowManagerImpl.addView(WindowManagerImpl.Java:69)
            at Android.app.Dialog.show(Dialog.Java:286)
            at Android.app.ProgressDialog.show(ProgressDialog.Java:116)
            at Android.app.ProgressDialog.show(ProgressDialog.Java:99)
            at br.gov.rj.barraemexposicao.EnviaFoto$6.onClick(EnviaFoto.Java:250)
            at Android.view.View.performClick(View.Java:4456)
            at Android.view.View$PerformClick.run(View.Java:18465)
            at Android.os.Handler.handleCallback(Handler.Java:733)
            at Android.os.Handler.dispatchMessage(Handler.Java:95)
            at Android.os.Looper.loop(Looper.Java:136)
            at Android.app.ActivityThread.main(ActivityThread.Java:5086)
            at Java.lang.reflect.Method.invoke(Native Method)
            at com.Android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.Java:785)
            at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:601)
07-04 09:03:23.546  18083-18083/br.gov.rj.barraemexposicao I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build: AU_LINUX_Android_LNX.LA.3.5.1_RB1.04.04.02.048.018_msm8226_LNX.LA.3.5.1_RB1__release_AU ()
    OpenGL ES Shader Compiler Version: E031.24.00.08
    Build Date: 03/07/14 Fri
    Local Branch:
    Remote Branch: quic/LNX.LA.3.5.1_RB1.1
    Local Patches: NONE
    Reconstruct Branch: AU_LINUX_Android_LNX.LA.3.5.1_RB1.04.04.02.048.018 + f2fd134 +  NOTHING
07-04 09:03:23.614  18083-18083/br.gov.rj.barraemexposicao D/OpenGLRenderer﹕ Enabling debug mode 0
07-04 09:04:14.226  18083-18096/br.gov.rj.barraemexposicao W/CursorWrapperInner﹕ Cursor finalized without prior close()
07-04 09:04:14.428  18083-18083/br.gov.rj.barraemexposicao I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build: AU_LINUX_Android_LNX.LA.3.5.1_RB1.04.04.02.048.018_msm8226_LNX.LA.3.5.1_RB1__release_AU ()
    OpenGL ES Shader Compiler Version: E031.24.00.08
    Build Date: 03/07/14 Fri
    Local Branch:
    Remote Branch: quic/LNX.LA.3.5.1_RB1.1
    Local Patches: NONE
    Reconstruct Branch: AU_LINUX_Android_LNX.LA.3.5.1_RB1.04.04.02.048.018 + f2fd134 +  NOTHING
07-04 09:04:17.318  18083-19131/br.gov.rj.barraemexposicao E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-34498
    Process: br.gov.rj.barraemexposicao, PID: 18083
    Java.lang.NullPointerException: Attempt to invoke virtual method 'char[] Java.lang.String.toCharArray()' on a null object reference
            at Java.io.File.fixSlashes(File.Java:185)
            at Java.io.File.<init>(File.Java:134)
            at br.gov.rj.barraemexposicao.EnviaFoto.uploadFile(EnviaFoto.Java:100)
            at br.gov.rj.barraemexposicao.EnviaFoto$6$1.run(EnviaFoto.Java:275)
            at Java.lang.Thread.run(Thread.Java:811)
07-04 09:04:18.179  18083-18083/br.gov.rj.barraemexposicao E/WindowManager﹕ Android.view.WindowLeaked: Activity br.gov.rj.barraemexposicao.EnviaFoto has leaked window com.Android.internal.policy.impl.PhoneWindow$DecorView{65280100 V.E..... R......D 0,0-684,192} that was originally added here
            at Android.view.ViewRootImpl.<init>(ViewRootImpl.Java:359)
            at Android.view.WindowManagerGlobal.addView(WindowManagerGlobal.Java:248)
            at Android.view.WindowManagerImpl.addView(WindowManagerImpl.Java:69)
            at Android.app.Dialog.show(Dialog.Java:286)
            at Android.app.ProgressDialog.show(ProgressDialog.Java:116)
            at Android.app.ProgressDialog.show(ProgressDialog.Java:99)
            at br.gov.rj.barraemexposicao.EnviaFoto$6.onClick(EnviaFoto.Java:248)
            at Android.view.View.performClick(View.Java:4456)
            at Android.view.View$PerformClick.run(View.Java:18465)
            at Android.os.Handler.handleCallback(Handler.Java:733)
            at Android.os.Handler.dispatchMessage(Handler.Java:95)
            at Android.os.Looper.loop(Looper.Java:136)
            at Android.app.ActivityThread.main(ActivityThread.Java:5086)
            at Java.lang.reflect.Method.invoke(Native Method)
            at com.Android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.Java:785)
            at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:601)

Mis à jour après le dialogue d'instance

07-04 09:54:17.341 26763-26763/br.gov.rj.barraemexposicao D/AndroidRuntime﹕ Shutting down VM 07-04 09:54:17.364 26763-26763/br.gov.rj.barraemexposicao E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: br.gov.rj.barraemexposicao, PID: 26763 Java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{br.gov.rj.barraemexposicao/br.gov.rj.barraemexposicao.EnviaFoto}: Java.lang.InstantiationException: br.gov.rj.barraemexposicao.EnviaFoto at Android.app.ActivityThread.performLaunchActivity(ActivityThread.Java:2124) at Android.app.ActivityThread.handleLaunchActivity(ActivityThread.Java:2257) at Android.app.ActivityThread.access$800(ActivityThread.Java:139) at Android.app.ActivityThread$H.handleMessage(ActivityThread.Java:1210) at Android.os.Handler.dispatchMessage(Handler.Java:102) at Android.os.Looper.loop(Looper.Java:136) at Android.app.ActivityThread.main(ActivityThread.Java:5086) at Java.lang.reflect.Method.invoke(Native Method) at com.Android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.Java:785) at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:601) Caused by: Java.lang.InstantiationException: br.gov.rj.barraemexposicao.EnviaFoto at Java.lang.Class.newInstance(Class.Java:1561) at Android.app.Instrumentation.newActivity(Instrumentation.Java:1084) at Android.app.ActivityThread.performLaunchActivity(ActivityThread.Java:2115) at Android.app.ActivityThread.handleLaunchActivity(ActivityThread.Java:2257) at Android.app.ActivityThread.access$800(ActivityThread.Java:139) at Android.app.ActivityThread$H.handleMessage(ActivityThread.Java:1210) at Android.os.Handler.dispatchMessage(Handler.Java:102) at Android.os.Looper.loop(Looper.Java:136) at Android.app.ActivityThread.main(ActivityThread.Java:5086) at Java.lang.reflect.Method.invoke(Native Method) at com.Android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.Java:785) at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:601)
12
TMoraes

D'accord, concernant cette exception 

Java.lang.NullPointerException: Attempt to invoke virtual method 'char[] Java.lang.String.toCharArray()' on a null object reference

Vous pouvez obtenir cette exception en utilisant l'application Photos, mais pas assez souvent dans l'application Gallery. Cela peut être important à savoir car dans la dernière version 5.0 d'Android, Lollipop, ils ont supprimé Gallery maintenant ils ont uniquement Photos application (différents fournisseurs peuvent avoir leurs propres applications intégrées pour photos et bien sûr, l'utilisateur peut utiliser toutes les applications externes également)

Et pour cette raison, vous obtenez `filePath = null '. Maintenant la question est pourquoi est-il nul?.

Donc, il peut y avoir deux raisons .. 

1st est votre méthode uriToPath(Uri uri) ne fonctionne pas correctement et 2nd est votre Uri que vous obtenez de data.getData() n'est pas correct.

Votre méthode uriToPath(Uri uri) semble être correcte, le problème réside donc dans votre Uri. Vous avez peut-être remarqué que si vous récupérez des images depuis l'application Gallery, il y a moins de chances que vous obteniez null dans filePath , mais si vous utilisez l'application Photos, les chances augmentent.

Cela se produit parce que votre application Photos affiche des photos qui ne figurent peut-être pas sur votre appareil local. Ces photos se trouvent en fait sur le serveur de Google. Donc, lorsque vous demandez Uri en sélectionnant une photo, cela donne le Uri qui est lié à ce serveur. 

L'Uri pourrait ressembler à ceci content://com.google.Android.apps.photos.content/0/https%3A%2F%2Flh3.googleusercontent.com%2FL-3Jm0TaSqnuKkitli5mK0-d

& son pas de votre périphérique local. Uri de périphérique local sera comme ceci

content://media/external/images/media/49518

En raison de cette différence, votre uriToPath(Uri uri) renvoie null.

Vérifiez donc d'abord la Uriet récupérez en conséquence l'image depuis un périphérique ou un serveur local.

Vous pouvez utiliser API DocumentsProvider pour ne pas avoir à vous soucier des fichiers locaux et du serveur.

private Bitmap getBitmapFromUri(Uri uri) throws IOException {
        ParcelFileDescriptor parcelFileDescriptor =
             getContentResolver().openFileDescriptor(uri, "r");
        FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
        Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
        parcelFileDescriptor.close();
        return image;
}

Mais si vous avez des photos locales dans l'application Photos, vous obtiendrez le fichier local Uri en les sélectionnant, ce qui vous donnera le chemin d'accès au fichier correct.

Vous pouvez également télécharger les photos (qui se trouvent sur le serveur Google) dans l'application Photos et l'utiliser ultérieurement.

47
AB1209

Vous pouvez essayer ce code, pour api> 19, l'image de récupération diffère

       txtlogochange.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View view) {
                   Intent intent = new Intent();
                   intent.setType("image/*");
                   intent.setAction(Intent.ACTION_GET_CONTENT);
                   startActivityForResult(Intent.createChooser(intent, "Select Picture"), 1);
               }
           });

   @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == this.RESULT_OK) {
            if (requestCode == 1) {
                if (Build.VERSION.SDK_INT < 19) {
                    Uri selectedImage = data.getData();
                    // System.out.println("selectedImage "+selectedImage);
                    String[] filePathColumn = {MediaStore.Images.Media.DATA};
                    Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                    cursor.moveToFirst();
                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    smallImagePath = cursor.getString(columnIndex);
                    cursor.close();
                    System.out.println("smallImagePath" + smallImagePath);
                    logoImg.setImageBitmap(BitmapFactory.decodeFile(smallImagePath));
                    encodeImage();
                }
                else {
                    try {
                        InputStream imInputStream = getContentResolver().openInputStream(data.getData());
                        Bitmap bitmap = BitmapFactory.decodeStream(imInputStream);
                        smallImagePath = saveGalaryImageOnLitkat(bitmap);
                        logoImg.setImageBitmap(BitmapFactory.decodeFile(smallImagePath));
                        encodeImage();
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                    // finishAndSetResult(RESULT_OK, picturePath, false);
                }
            }
        }
    }
    private File temp_path;
    private final int COMPRESS = 100;
    private String saveGalaryImageOnLitkat(Bitmap bitmap) {
        try {
            File cacheDir;
            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
                cacheDir = new File(Environment.getExternalStorageDirectory(), getResources().getString(R.string.app_name));
            else
                cacheDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
            if (!cacheDir.exists())
                cacheDir.mkdirs();
            String filename = System.currentTimeMillis() + ".jpg";
            File file = new File(cacheDir, filename);
            temp_path = file.getAbsoluteFile();
            // if(!file.exists())
            file.createNewFile();
            FileOutputStream out = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG, COMPRESS, out);
            return file.getAbsolutePath();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;

    }
3
arun-r

Utilisez la méthode ci-dessous dans onActivityResult pour que chaque périphérique fonctionne correctement.

Uri selectedImageUri  =null;
try {
                            Path = data.getExtras().getString("dataImg");

                        } catch (Exception e) {
                        }
                        try {
                            if (Path == null) {

                                Path = data.getData().toString();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        try {
                            if (Path == null) {
                                Path = data.getExtras().getString("data");
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        try {
                            if (Path == null) {
                                Uri newPhotoUri = null;
                                newPhotoUri = data.getData();
                                String[] projection = { MediaStore.Images.Media.DATA };
                                CursorLoader loader = new CursorLoader(this,
                                        newPhotoUri, projection, null, null,
                                        null);
                                Cursor cursor = loader.loadInBackground();

                                int column_index_data = cursor
                                        .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                                cursor.moveToFirst();
                                Path = cursor.getString(column_index_data);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        try {

                            if (Path == null) {
                                selectedImageUri = data.getData();
                                if (selectedImageUri == null) {
                                    Bitmap photo = (Bitmap) data.getExtras()
                                            .get("data");
                                    selectedImageUri = getImageUri(
                                            Survey_Elabel.this, photo);
                                }

                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

Méthode utilisée pour obtenir l'URI.

public Uri getImageUri(Context inContext, Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
    return Uri.parse(path);
}
0
Ahmad Arslan

Lorsque vous passez d'activité à application caméra, onSaveInstanceState n'est pas appelé, nous devons enregistrer notre méthode de chemin d'image/Uri dans la méthode onPause () par sharedPreference. Sur la méthode restoreInstanceState, nous devons extraire notre chemin d'accès d'image à partir des préférences partagées.

0