2016-07-03 12 views
1

ich versuche, ein Bild zu Feuerbasis zu laden und wenn ich seine mir laden zeigen, dass die Größe der Datei 0 Bytes ist und zeigen nicht den Inhalt Bildwie zum Hochladen einer Datei mit Feuerbasis Lagerung Feuerbasis

alles scheint in Ordnung , dann, was passiert?

StorageReference storageRef = storage.getReferenceFromUrl("gs://<your-bucket-name>"); 

       if (inputStream!=null) { 

        String pic = "BathroomImage" + +rand.nextInt(1000) + ".jpg"; 
        mountainsRef = storageRef.child(pic); 
        uploadTask = mountainsRef.putStream(inputStream); 


        uploadTask.addOnFailureListener(new OnFailureListener() { 
         @Override 
         public void onFailure(@NonNull Exception e) { 
          Log.d("this is the faiure:","hey im here"); 
         } 
        }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() { 
         @Override 
         public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { 
          taskSnapshot.getMetadata(); 
          Uri downloadUri = taskSnapshot.getDownloadUrl(); 
          bitmap.recycle(); 
          bitmap=null; 
          System.gc(); 

          try { 
           inputStream.close(); 
          } catch (IOException e) { 
           e.printStackTrace(); 
          } 
         } 
        }); 
      } 
     } 

Hier lade ich das Bild von den Daten zu inputsream.

void TakePickphoto(){ 
     Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); // Create intent to Open Image applications like Gallery, Google Photos 
     startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE);// Start the Intent 

public void onActivityResult(final int requestCode, final int resultCode, final Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 


     if (requestCode == RESULT_LOAD_IMAGE && resultCode ==getActivity().RESULT_OK && null != data) { 
      selectedImage = data.getData(); // Get the URI Image from data 

      handler= new Handler(); 
      try { 
       Thread.sleep(50); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
      Runnable runnable = new Runnable() { 
       @Override 
       public void run() { 
        try { 

         inputStream = context.getContentResolver().openInputStream(data.getData()); 
         BitmapFactory.Options options = new BitmapFactory.Options(); 
         options.inSampleSize =4; 
         bitmap = BitmapFactory.decodeStream(inputStream, new Rect(40,40,40,40),options); 

        } catch (FileNotFoundException e) { 
         e.printStackTrace(); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 

        handler.post(new Runnable() { 
         @Override 
         public void run() { 
          ImageView imageView; 
          imageView = (ImageView) view.findViewById(R.id.imageView2); 
          imageView.setImageBitmap(bitmap); 


         } 
        }); 
       } 
      }; 
      new Thread(runnable).start(); 

     } 
    } 
} 

bitte helfen, alles sieht gut aus für mich.

Antwort

0

** Dies speichert das Bild nicht in voller Auflösung ** Um ein Bild in voller Auflösung zu speichern, untersuchen Sie, wie Sie das tun, wenn Sie den TakePictureIntent starten.

hatte ich den gleichen Fehler und löste es meine Aufnahme von Bildern und das Hochladen wie dies durch die Einrichtung:

//takes the picture 
    private void dispatchTakePictureIntent() { 
     Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) { 
      startActivityForResult(takePictureIntent, 1); 
     } 
    } 

    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (requestCode == 1 && resultCode == Activity.RESULT_OK) { 
      //saves the pic locally 
      Bundle extras = data.getExtras(); 
      Bitmap imageBitmap = (Bitmap) extras.get("data"); 
      ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
      imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
      byte[] dataBAOS = baos.toByteArray(); 

      /***************** UPLOADS THE PIC TO FIREBASE*****************/ 
      // Points to the root reference 
      StorageReference storageRef = FirebaseStorage.getInstance().getReferenceFromUrl("your-root-storage-ref"); 
      StorageReference imagesRef = storageRef.child("image"); 

      UploadTask uploadTask = imagesRef.putBytes(dataBAOS); 
      uploadTask.addOnFailureListener(new OnFailureListener() { 
       @Override 
       public void onFailure(@NonNull Exception exception) { 
        // Handle unsuccessful uploads 
       } 
      }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() { 
       @Override 
       public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { 
        // taskSnapshot.getMetadata() contains file metadata such as size, content-type, and download URL. 
        Uri downloadUrl = taskSnapshot.getDownloadUrl(); 
       } 
      }); 
     } 
    } 

Auf diese Weise wird es komprimieren und Ihr Bild in Ihrer Firebase Storage mit dieser Datei-Struktur speichern:

root -> Bild

0

Sie die InputStream direkt von den Uri zur putStream Methode übergeben können. Möglicherweise möchten Sie die Größe des Bildes selbst ändern, bevor Sie es hochladen, was ein wenig mehr Arbeit erfordern würde, aber auf diese Weise wird auf der Client-Seite sehr wenig Speicher benötigt.

if (requestCode == IMAGE_PICKER_SELECT && resultCode == Activity.RESULT_OK) { 
     Uri imageUri = data.getData(); 
     try { 
      ContentResolver contentResolver = getActivity().getContentResolver(); 
      StorageMetadata storageMetadata = new StorageMetadata.Builder() 
        .setContentType(contentResolver.getType(imageUri)) 
        .build(); 
      FirebaseStorage.getInstance().getReference() 
        .child("users") 
        .child(FirebaseAuth.getInstance().getCurrentUser().getUid()) 
        .child(UUID.randomUUID().toString()) 
        .putStream(contentResolver.openInputStream(imageUri), storageMetadata) 
        .addOnSuccessListener(getActivity(), new OnSuccessListener<UploadTask.TaskSnapshot>() { 
         @Override 
         public void onSuccess(UploadTask.TaskSnapshot task) { 
          Uri downloadUrl = task.getDownloadUrl();        
          Toast.makeText(getActivity(), R.string.image_successfully_uploaded, Toast.LENGTH_LONG).show(); 
         } 
        }) 
        .addOnFailureListener(getActivity(), new OnFailureListener() { 
         @Override 
         public void onFailure(@NonNull Exception e) { 
          Toast.makeText(getActivity(), R.string.error_uploading_image, Toast.LENGTH_LONG).show(); 
         } 
        }); 
     } catch (IOException e) { 

     } 

    } else { 
     super.onActivityResult(requestCode, resultCode, data); 
    }