首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在目录中存储文件有问题吗?

在目录中存储文件有问题吗?
EN

Stack Overflow用户
提问于 2016-02-15 10:11:07
回答 2查看 61关注 0票数 0

我想把照片藏起来但是..。

  1. 文件被创建,但JPEG是空的。
  2. 我想使用JPEG文件对其进行编码并将其发送到服务器,因为位图是我想要使用的低定义。

那么,我如何解决这两个问题呢?

代码语言:javascript
复制
public void takePicture() {
    Toast.makeText(MainActivity.this,"Entra no takePicture",Toast.LENGTH_LONG).show();
    Log.d("entra", "Take Picture");
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // Ensure that there's a camera activity to handle the intent
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        // Create the File where the photo should go
        photoFile = null;
        try {
            Log.d("entra","Vai criar file");
            photoFile = createImageFile();
        } catch (IOException ex) {
            // Error occurred while creating the File
            Log.d("Erro",ex.getMessage());
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {

            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
        }
    }
}



@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Toast.makeText(MainActivity.this,"Entra no onActivityResult()",Toast.LENGTH_LONG).show();
    if (requestCode ==  REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        image_BMP = (Bitmap) extras.get("data");
        bitmap.setImageBitmap(image_BMP);
        Log.d("Onactivity result()"," Passa aqui");
        myphoto= ImageUtility.getBytes(Bitmap.createBitmap(image_BMP));

    }
    Toast.makeText(MainActivity.this,"sai do onActivityResult()",Toast.LENGTH_LONG).show();
}

public static File createImageFile() throws IOException {
    // Create an image file name

    Log.d("entra", "Create File");
    String timeStamp = new   SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES);
    File pic = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = "file:" + pic.getAbsolutePath();
    Log.d("entra", "Create File");

    return pic;
}
EN

回答 2

Stack Overflow用户

发布于 2016-02-15 10:18:28

这就是我所用的,它起作用了:

代码语言:javascript
复制
        Uri image = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };
        Cursor cursor = getContentResolver().query(image,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String filePath = cursor.getString(columnIndex);
        cursor.close();

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);
        imageView.setBitmap(bitmap);

在onActivityResult内部使用,而不是:

代码语言:javascript
复制
     Bundle extras = data.getExtras();
    image_BMP = (Bitmap) extras.get("data");
    bitmap.setImageBitmap(image_BMP);
    Log.d("Onactivity result()"," Passa aqui");
    myphoto= ImageUtility.getBytes(Bitmap.createBitmap(image_BMP));

你不必自己创建文件。相机会处理好的。你可以删除这些东西的代码。

编辑:

要传递自定义图像位置,可以在调用startActivityForResult()之前使用它

代码语言:javascript
复制
 Uri fileUri = Uri.fromFile(yourFile);
 intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
票数 0
EN

Stack Overflow用户

发布于 2016-02-15 10:33:04

代码语言:javascript
复制
intentCamera.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(photoFile));

在startActivityforresult之前添加这一行并存储您的路径,因为有时活动重新启动,您将得到一个空指针,

代码语言:javascript
复制
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);           
        outState.putString("imagePath", mCurrentPhotoPath);
    }


    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        // get the file path
        mCurrentPhotoPath = savedInstanceState.getString("imagePath");
    }

然后,您可以从路径中获取文件,也可以上传文件,也可以使用

代码语言:javascript
复制
Bitmap bitmap = BitmapFactory.decodeFile(new File(filePath))

以后按你的意愿处理位图。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35406539

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档