0
votes

Good morning, I'm creating activity which take picture then store it in file. But I had this error :

BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: .jpg (No such file or directory)

Also I want to know how to store this picutre file into my room db I have a class "Photo". Thank you. Here is my code:

takepictureActivity

public class PrendrePhoto extends AppCompatActivity {

private ImageView imageView;
private EditText titrImg2;
private Button take;
private String pathPic;

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

    imageView = (ImageView) findViewById(R.id.imageTaken);
    titrImg2 = findViewById(R.id.titreImg2);

    take = findViewById(R.id.take);
    take.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            takePicture();
        }
    });
}

private void takePicture() {
    Intent takepic = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takepic.resolveActivity(getPackageManager()) != null) {
        File pic = null;
        pic = creerPhotoFile();
        if (pic != null) {
            pathPic = pic.getPath();
            System.out.println("pic créer");
            System.out.println(pathPic);
            startActivityForResult(takepic, 1);
        }else {
            System.out.println("pic null");
        }

    }
}

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

    if (resultCode == RESULT_OK) {
        if (requestCode == 1) {
           Bitmap photo = BitmapFactory.decodeFile(pathPic);
            imageView.setImageBitmap(photo);

        }
    }

    //Hna je dois crée filePhoto

}

private File creerPhotoFile() {
    ZonedDateTime now = ZonedDateTime.now();
    String d = ("" + now.getDayOfMonth() + "/" + now.getMonthValue() + 
   "/"  + now.getYear());
    String heure = "" + now.getHour() + " : " + now.getMinute();
    String titre = titrImg2.getText().toString();
    File temp = new File("temp");
    temp.mkdirs();
    System.out.println(temp.getPath());
    File image = null;
   /*   try {
        image = File.createTempFile(titre, ".jpg",temp);//Even with this 
       it didn't work 
    } catch (IOException e) {
        e.printStackTrace();
    }*/

   image = new File(titre + ".jpg");

    return image;
   }
   }

Photo.java

   @Entity
 public class Photo implements Serializable
 {
   @PrimaryKey(autoGenerate = true)

 private int idP;
 private String titre;
 private String path ;//this path to get to help to display this picture
 private String dateHeure ;


 public Photo(String titre, String dateHeure) {
    this.titre = titre;
    this.dateHeure = dateHeure;
 }
 }
1
It looks like pathPic has the value ".jpg", and there's no file by that name.President James K. Polk
but initialize it to pathPic = pic.getPath();bhdev

1 Answers

0
votes

Even after doing all the logic of creating a unique file path you are using

String titre = titrImg2.getText().toString();

Which is (as per the name suggest) image title, which is coming form UI.

image = new File(titre + ".jpg");

return image;

I think you need to look into this.

As far as saving images in room goes, it is not recommended. But if you have to do it then you can use BLOB. Images are usually stored as a BLOB and room does provide this datatype. BLOB Documentation

Implementation can be done like:

@ColumnInfo(typeAffinity = ColumnInfo.BLOB)
private byte[] image;
}