为什么即使裁剪成功,裁剪图像时我总是会得到Null指针异常

时间:2019-03-02 06:09:31

标签: android

这是我的PostActivity.java

package com.example.owner.kp;

import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog; import android.content.ContentResolver; import android.content.Intent; import android.graphics.Bitmap; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.webkit.MimeTypeMap; import android.widget.EditText; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast;

import com.google.android.gms.tasks.Continuation; import com.google.android.gms.tasks.OnCompleteListener; import com.google.android.gms.tasks.OnFailureListener; import com.google.android.gms.tasks.Task; import com.google.firebase.auth.FirebaseAuth; import com.google.firebase.database.DatabaseReference; import com.google.firebase.database.FirebaseDatabase; import com.google.firebase.storage.FirebaseStorage; import com.google.firebase.storage.StorageReference; import com.google.firebase.storage.StorageTask; import com.theartofdev.edmodo.cropper.CropImage;

import java.util.HashMap;

public class PostActivity extends AppCompatActivity {

    Uri imageUri;
    String myUrl = "";
    StorageTask uploadTask;
    StorageReference storageReference;

    ImageView close, image_added;
    TextView post;
    EditText deskripsi;


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

        close = findViewById( R.id.close );
        close = findViewById( R.id.image_added );
        post = findViewById( R.id.post );
        deskripsi = findViewById( R.id.deskripsi );

        storageReference = FirebaseStorage.getInstance().getReference("posts");

        close.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity( new Intent( PostActivity.this, UserIdxActivity.class ) );
                finish();
            }
        } );

        post.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                uploadImage();
            }
        } );

        CropImage.activity()
                .setAspectRatio( 1,1 )
                .start( PostActivity.this );
    }

    private String getFileExtension(Uri uri)
    {
        ContentResolver contentResolver = getContentResolver();
        MimeTypeMap mime = MimeTypeMap.getSingleton();
        return mime.getExtensionFromMimeType( contentResolver.getType( uri ) );
    }

    private void uploadImage()
    {
        final ProgressDialog progressDialog = new ProgressDialog( this );
        progressDialog.setMessage( "Posting" );
        progressDialog.show();

        if (imageUri != null)
        {
            final StorageReference filereference = storageReference.child( System.currentTimeMillis()
                    + "." + getFileExtension( imageUri ));

            uploadTask = filereference.putFile( imageUri );
            uploadTask.continueWithTask( new Continuation() {
                @Override
                public Object then(@NonNull Task task) throws Exception {
                    if (!task.isSuccessful())
                    {
                        throw task.getException();
                    }

                    return filereference.getDownloadUrl();
                }
            } ).addOnCompleteListener( new OnCompleteListener<Uri>() {
                @Override
                public void onComplete(@NonNull Task<Uri> task) {
                    if (task.isSuccessful())
                    {
                        Uri downloadUri = task.getResult();
                        myUrl = downloadUri.toString();

                        DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Posts");

                        String postid = reference.push().getKey();

                        HashMap<String, Object> hashMap = new HashMap<>(  );
                        hashMap.put( "postid", postid );
                        hashMap.put( "postimage", myUrl );
                        hashMap.put( "deskripsi", deskripsi.getText().toString() );
                        hashMap.put( "publisher", FirebaseAuth.getInstance().getCurrentUser().getUid() );

                        reference.child( postid ).setValue( hashMap );

                        progressDialog.dismiss();
                        startActivity( new Intent( PostActivity.this, UserIdxActivity.class ) );
                        finish();
                    }
                    else
                    {
                        Toast.makeText( PostActivity.this, "Gagal", Toast.LENGTH_SHORT ).show();
                    }
                }
            } ).addOnFailureListener( new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Toast.makeText( PostActivity.this, ""+e.getMessage(), Toast.LENGTH_SHORT ).show();
                }
            } );
        }
        else
        {
            Toast.makeText( this, "Tidak ada gambar terpilih!", Toast.LENGTH_SHORT ).show();
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult( requestCode, resultCode, data );

        if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK && data!=null && data.getData()!=null)
        {
            CropImage.ActivityResult result = CropImage.getActivityResult(data);
            imageUri = result.getUri();

            image_added.setImageURI( imageUri );
        }
        else
        {

            Toast.makeText( this, "Terjadi Kesalahan", Toast.LENGTH_SHORT ).show();
            startActivity( new Intent( PostActivity.this, UserIdxActivity.class ) );
            finish();
        }
    } }

0 个答案:

没有答案