3

キャプチャした画像をftpサーバーにアップロードする必要があります。カメラから画像をキャプチャしていますが、その画像の画像名とパスを取得したいのですが、次のコードを使用してimagepathを取得しています。

int ACTION_TAKE_PICTURE = 1;
String selectedImagePath;
Uri mCapturedImageURI;

Button loadButton;
ImageView img;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_new_ftpsdemo);
     img = (ImageView)findViewById(R.id.image);

    ContentValues values = new ContentValues();
    values.put(MediaStore.Images.Media.TITLE, "yahoo.jpg");
    mCapturedImageURI  = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
    Intent intentPicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intentPicture.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
    startActivityForResult(intentPicture,ACTION_TAKE_PICTURE);

}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode == ACTION_TAKE_PICTURE){
        selectedImagePath = getRealPathFromURI(mCapturedImageURI);
        Log.v("selectedImagePath", selectedImagePath);
        img.setImageBitmap( BitmapFactory.decodeFile(selectedImagePath));
    }
}


public String getRealPathFromURI(Uri contentUri)
    {
        try
        {
            String[] proj = {MediaStore.Images.Media.DATA};
            Cursor cursor = managedQuery(contentUri, proj, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        }
        catch (Exception e)
        {
            return contentUri.getPath();
        }
    }

しかし、私はこのようなイメージパスを取得しています:

/mnt/sdcard/DCIM/Camera/1352443194885.jpg

名前「yahoo.jpg」を保存しているので。これは非常に単純な質問かもしれませんが、画像名とパスを同じにすることができません。そのため、FTPサーバーに画像をアップロードできません。

4

4 に答える 4

1

これを確認してください...これを onActivityResult に入れてください

           Uri selectedImage = intent.getData();

           String[] filePathColumn = {MediaStore.Images.Media.DATA};
           Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
           cursor.moveToFirst();
           int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
           String filePath = cursor.getString(columnIndex);
           Log.v("log","filePath is : "+filePath); 
于 2012-11-09T09:24:07.383 に答える
0

次のコードで問題を解決しました。うまくいきます。

このリンクを参照しました:こちら

ファイルパスとimageNameグローバル変数を作成するだけです

MyCameraActivity.java

public class MyCameraActivity extends Activity {
private Camera mCamera;
private CameraPreview mCameraPreview;
public static String imageFilePath;
public static String imageName;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mCamera = getCameraInstance();
    mCameraPreview = new CameraPreview(this, mCamera);
    FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
    preview.addView(mCameraPreview);

    Button captureButton = (Button) findViewById(R.id.button_capture);
    captureButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mCamera.takePicture(null, null, mPicture);
        }
    });
}

/**
 * Helper method to access the camera returns null if it cannot get the
 * camera or does not exist
 * 
 * @return
 */
private Camera getCameraInstance() {
    Camera camera = null;
    try {
        camera = Camera.open();
    } catch (Exception e) {
        // cannot get camera or does not exist
    }
    return camera;
}

PictureCallback mPicture = new PictureCallback() {
    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
        File pictureFile = getOutputMediaFile();
        if (pictureFile == null) {
            return;
        }
        try {
            FileOutputStream fos = new FileOutputStream(pictureFile);
            fos.write(data);
            fos.close();
        } catch (FileNotFoundException e) {

        } catch (IOException e) {
        }
    }
};

private static File getOutputMediaFile() {
    File filePath = new File(
            Environment
                    .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
            "MyCameraApp");

    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d("MyCameraApp", "failed to create directory");
            return null;
        }
    }
    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
            .format(new Date());
imageName = timeStamp +".jpg"; // name of captured image
    File mediaFile;
    mediaFile = new File(mediaStorageDir.getPath() + File.separator
            + imageName);
    imageFilePath = mediaFile.toString(); // you can get path of image saved
    return mediaFile;
}

}

CameraPreview.java:

public class CameraPreview extends SurfaceView implements
    SurfaceHolder.Callback {
private SurfaceHolder mSurfaceHolder;
private Camera mCamera;

// Constructor that obtains context and camera
@SuppressWarnings("deprecation")
public CameraPreview(Context context, Camera camera) {
    super(context);
    this.mCamera = camera;
    this.mSurfaceHolder = this.getHolder();
    this.mSurfaceHolder.addCallback(this);
    this.mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
    try {
        mCamera.setPreviewDisplay(surfaceHolder);
        mCamera.startPreview();
    } catch (IOException e) {
        // left blank for now
    }
}

@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
    mCamera.stopPreview();
    mCamera.release();
}

@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int format,
        int width, int height) {
    // start preview with new settings
    try {
        mCamera.setPreviewDisplay(surfaceHolder);
        mCamera.startPreview();
    } catch (Exception e) {
        // intentionally left blank for a test
    }
}

}

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<FrameLayout
    android:id="@+id/camera_preview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1" />
<Button
    android:id="@+id/button_capture"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="Capture" />
</LinearLayout>

androidmanifest.xml では、次の権限が必要です。

<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

これで、最近キャプチャした画像の imageName と imagepath を取得し、この画像を ftp サーバーに簡単にアップロードできます。

ハッピーコーディング。

于 2012-11-09T13:53:34.347 に答える
0

これを使用して、カメラのインテントを開始します。

おー。はUri targetURIグローバル宣言です。

Intent getCameraImage = new Intent("android.media.action.IMAGE_CAPTURE");

File cameraFolder;

if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
    cameraFolder = new File(android.os.Environment.getExternalStorageDirectory(),"your_app_name/camera");
else
    cameraFolder= StatusUpdate.this.getCacheDir();
if(!cameraFolder.exists())
    cameraFolder.mkdirs();

File photo = new File(Environment.getExternalStorageDirectory(), "your_app_name/camera/camera_snap.jpg");
getCameraImage.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
targetURI = Uri.fromFile(photo);

startActivityForResult(getCameraImage, 1);

そしてでonActivityResult()

getContentResolver().notifyChange(targetURI, null);

ContentResolver cr = getContentResolver();

try {       
    // SET THE IMAGE FROM THE CAMERA TO THE IMAGEVIEW
    bmpImageCamera = android.provider.MediaStore.Images.Media.getBitmap(cr, targetURI);

    // SET THE IMAGE FROM THE GALLERY TO THE IMAGEVIEW
    imgvwSelectedImage.setImageBitmap(bmpImageCamera);

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

このコードは、選択した名前のフォルダーを作成します。ここでフォルダ名を変更できます: newFile(android.os.Environment.getExternalStorageDirectory(),"your_app_name/camera");

camera_snap.jpgまた、これは Intent を呼び出してカメラ画像を取得するたびに上書きします。

このコードはメモリ不足の例外を考慮していませんが、カメラの画像をアプリに戻す方法を示すだけです。

編集:ほとんど忘れていました。まだ行っていない場合は、この権限を に追加する必要がありますManifest.xml<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

于 2012-11-09T09:05:56.243 に答える
0

パスを作成するときは、「タイトル」を保存するだけです。カメラが保存するために使用する実際のパスとファイル名を指定していません。このため、カメラは、指定した「タイトル」でファイルをデフォルトの場所に保存しています。

コードですべてうまくやっていますが、次のようにして同じファイル名にします。

の代わりに:

ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "yahoo.jpg");
mCapturedImageURI  =  getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

これを使って:

    StringBuilder path = new StringBuilder();
    path.append(Environment.getExternalStorageDirectory());
    path.append(// any location say "/Pictures/" //); // Do check if the folder is present. Else create one.
    path.append("yahoo");
        path.append(".jpg");
    File file = new File(path.toString());
    mCapturedImageURI = Uri.fromFile(file);
于 2012-11-09T09:00:38.937 に答える