function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
PanchoPancho 

Problem displaying jpeg attachment in Android ImageView image is black

Hi Everyone,

I have an app that uploads and downloads jpeg pictures as attachements to custom salesforce.com objects.

The uploading and storing seems to work fine.

Even the downloading seems to work fine, but for some reason my pictures are coming up black in my Android ImageView.

 

I am converting to and from Base64.

Also my file sizes of these pics are relatively small.  Height 100dp and Width 178dp.  Filesize about 14k.

Below is a sample test activity that I created which has the same issue, but less code to look at.

Any help is greatly appreciated.

 

Thanks!

 

import java.io.UnsupportedEncodingException;
import org.json.JSONArray;
import com.salesforce.androidsdk.app.ForceApp;
import com.salesforce.androidsdk.rest.ClientManager.LoginOptions;
import com.salesforce.androidsdk.rest.RestClient.AsyncRequestCallback;
import com.salesforce.androidsdk.rest.RestClient;
import com.salesforce.androidsdk.rest.RestRequest;
import com.salesforce.androidsdk.rest.RestResponse;
import com.salesforce.androidsdk.ui.NativeMainActivity;
import android.os.Bundle;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

public class ImageTest extends NativeMainActivity {
	
	private RestClient client;
	ProgressDialog progressBar;
	int numPics=0;
	String rName="";
	Bitmap bitmap=null;
	Bitmap bitmapTest=null;
	private byte[] buffer=null;
	public static final String IMAGE_STORE = "StorePic";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_image_test);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.image_test, menu);
		return true;
	}

	@Override
	protected LoginOptions getLoginOptions() {
    	LoginOptions loginOptions = new LoginOptions(
    			null, // login host is chosen by user through the server picker 
    			ForceApp.APP.getPasscodeHash(),
    			getString(R.string.oauth_callback_url),
    			getString(R.string.oauth_client_id),
    			new String[] {"api"});
    	return loginOptions;
	}

	@Override
	protected void onResume(RestClient client) {
        this.client = client; 
		// Show everything
        findViewById(R.id.image_page).setVisibility(View.VISIBLE);		
        try {
			startLoadPicInfo();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	} //end on resume	
    
    protected void startLoadPicInfo() throws UnsupportedEncodingException {

	    String sfRequest = "SELECT Name, Id, Body, ParentId, BodyLength FROM Attachment "+
	    		"WHERE (ParentId='a1R50000000I95M') AND (Name='StorePic.jpg') ORDER BY CREATEDDATE DESC LIMIT 1";
			try {
				sendPicRequest(sfRequest);
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    }	
	
	private void sendPicRequest(String soql) throws UnsupportedEncodingException {
		RestRequest restRequest = RestRequest.getRequestForQuery(getString(R.string.api_version), soql);
		client.sendAsync(restRequest, new AsyncRequestCallback() {
			@Override
			public void onSuccess(RestRequest request, RestResponse result) {
				try {
					JSONArray records = result.asJSONObject().getJSONArray("records");
					numPics=records.length();
					if (records.length()>0) {
						for (int i = 0; i < records.length(); i++) {
							buffer = Base64.decode(records.getJSONObject(i).getString("Body").getBytes(), Base64.DEFAULT);
							bitmapTest = BitmapFactory.decodeByteArray(buffer , 0, buffer.length);
							records.getJSONObject(i).getString("Name");
							}   //end for loop - loading results
				 	 }  //end if
					} catch (Exception e) {
						onError(e);
						e.printStackTrace();
					}
					
					if (numPics>0) {		     
			        ImageView sPic = (ImageView)findViewById(R.id.imageView1);
					sPic.setImageBitmap(bitmapTest);
					sPic.bringToFront();
					}
			}
			
			@Override
			public void onError(Exception exception) {
                Toast.makeText(ImageTest.this,
                               ImageTest.this.getString(ForceApp.APP.getSalesforceR().stringGenericError(), exception.toString()),
                               Toast.LENGTH_LONG).show();
			}
		});
	}  //end sendPicRequest()   
}  //end ImageTest

 Here is the activity_image_test.xml

 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:id="@+id/image_page"
    tools:context=".ImageTest" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ab_solid_example" />

</RelativeLayout>

 

PanchoPancho

After investigating further. I have discovered this.

 

I think I am only retrieving the first part of the image body.

 

I can upload and store a picture as an attachment fine in Android using the SDK and JSON approach.

But, it doesnt seem to work if I want to read that same pic attachment from Salesforce using the SDK and JSON.

 

I noticed in the REST API there is a different query for retrieving a BLOB.  Is that my problem, because I am using a regular query (RestRequest.getRequestForQuery) and it has limits on the size of the result?

 

Any help is greatly appreciated.

deni kurniawandeni kurniawan
Hi Pancho
do you have a tutorial Android apps fo upload attachment in salesforce?
I'm making an application but, I still difficulties in uploading an attachments
please share you'r knowledge to me